blob: 684c2c5fb1849e762f8df8615a6a1257fcf11ae2 [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
Geoff Langd42cf4e2013-06-05 16:09:17 -04002113bool Renderer11::getRGB565TextureSupport() const
2114{
2115 return false;
2116}
2117
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002118bool Renderer11::getLuminanceTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002119{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002120 return false;
2121}
2122
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002123bool Renderer11::getLuminanceAlphaTextureSupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002124{
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002125 return false;
2126}
2127
2128bool Renderer11::getTextureFilterAnisotropySupport() const
2129{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002130 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002131}
2132
2133float Renderer11::getTextureMaxAnisotropy() const
2134{
shannon.woods@transgaming.com1abd7972013-02-28 23:06:58 +00002135 switch (mFeatureLevel)
2136 {
2137 case D3D_FEATURE_LEVEL_11_0:
2138 return D3D11_MAX_MAXANISOTROPY;
2139 case D3D_FEATURE_LEVEL_10_1:
2140 case D3D_FEATURE_LEVEL_10_0:
2141 return D3D10_MAX_MAXANISOTROPY;
2142 default: UNREACHABLE();
2143 return 0;
2144 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002145}
2146
shannonwoods@chromium.org89200d92013-05-30 00:07:50 +00002147bool Renderer11::getEventQuerySupport() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002148{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00002149 return true;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002150}
2151
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002152Range Renderer11::getViewportBounds() const
2153{
2154 switch (mFeatureLevel)
2155 {
2156 case D3D_FEATURE_LEVEL_11_0:
2157 return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX);
2158 case D3D_FEATURE_LEVEL_10_1:
2159 case D3D_FEATURE_LEVEL_10_0:
2160 return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX);
2161 default: UNREACHABLE();
2162 return Range(0, 0);
2163 }
2164}
2165
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002166unsigned int Renderer11::getMaxVertexTextureImageUnits() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002167{
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +00002168 META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2169 switch (mFeatureLevel)
2170 {
2171 case D3D_FEATURE_LEVEL_11_0:
2172 case D3D_FEATURE_LEVEL_10_1:
2173 case D3D_FEATURE_LEVEL_10_0:
2174 return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4;
2175 default: UNREACHABLE();
2176 return 0;
2177 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002178}
2179
shannon.woods@transgaming.com76cd88c2013-01-25 21:54:36 +00002180unsigned int Renderer11::getMaxCombinedTextureImageUnits() const
2181{
2182 return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits();
2183}
2184
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002185unsigned int Renderer11::getReservedVertexUniformVectors() const
2186{
2187 return 0; // Driver uniforms are stored in a separate constant buffer
2188}
2189
2190unsigned int Renderer11::getReservedFragmentUniformVectors() const
2191{
2192 return 0; // Driver uniforms are stored in a separate constant buffer
2193}
2194
2195unsigned int Renderer11::getMaxVertexUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002196{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002197 META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2198 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2199 return MAX_VERTEX_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002200}
2201
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002202unsigned int Renderer11::getMaxFragmentUniformVectors() const
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002203{
shannon.woods@transgaming.com4e482042013-01-25 21:54:18 +00002204 META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT);
2205 ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0);
2206 return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11;
shannon.woods@transgaming.com254317d2013-01-25 21:54:09 +00002207}
2208
shannon.woods@transgaming.comd8136cb2013-02-28 23:14:44 +00002209unsigned int Renderer11::getMaxVaryingVectors() const
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002210{
2211 META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT);
shannonwoods@chromium.org74b86cf2013-05-30 00:02:58 +00002212 META_ASSERT(D3D11_VS_OUTPUT_REGISTER_COUNT <= D3D11_PS_INPUT_REGISTER_COUNT);
2213 META_ASSERT(D3D10_VS_OUTPUT_REGISTER_COUNT <= D3D10_PS_INPUT_REGISTER_COUNT);
shannon.woods@transgaming.com28d268e2013-01-25 21:54:26 +00002214 switch (mFeatureLevel)
2215 {
2216 case D3D_FEATURE_LEVEL_11_0:
2217 return D3D11_VS_OUTPUT_REGISTER_COUNT;
2218 case D3D_FEATURE_LEVEL_10_1:
2219 case D3D_FEATURE_LEVEL_10_0:
2220 return D3D10_VS_OUTPUT_REGISTER_COUNT;
2221 default: UNREACHABLE();
2222 return 0;
2223 }
2224}
2225
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002226unsigned int Renderer11::getMaxVertexShaderUniformBuffers() const
2227{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002228 META_ASSERT(gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2229 gl::IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2230
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002231 switch (mFeatureLevel)
2232 {
2233 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002234 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002235 case D3D_FEATURE_LEVEL_10_1:
2236 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002237 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedVertexUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002238 default: UNREACHABLE();
2239 return 0;
2240 }
2241}
2242
2243unsigned int Renderer11::getMaxFragmentShaderUniformBuffers() const
2244{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002245 META_ASSERT(gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT &&
2246 gl::IMPLEMENTATION_MAX_FRAGMENT_SHADER_UNIFORM_BUFFERS >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT);
2247
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002248 switch (mFeatureLevel)
2249 {
2250 case D3D_FEATURE_LEVEL_11_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002251 return D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002252 case D3D_FEATURE_LEVEL_10_1:
2253 case D3D_FEATURE_LEVEL_10_0:
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002254 return D3D10_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT - getReservedFragmentUniformBuffers();
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002255 default: UNREACHABLE();
2256 return 0;
2257 }
2258}
2259
shannonwoods@chromium.org2b544222013-05-30 00:11:12 +00002260unsigned int Renderer11::getReservedVertexUniformBuffers() const
2261{
2262 // we reserve one buffer for the application uniforms, and one for driver uniforms
2263 return 2;
2264}
2265
2266unsigned int Renderer11::getReservedFragmentUniformBuffers() const
2267{
2268 // we reserve one buffer for the application uniforms, and one for driver uniforms
2269 return 2;
2270}
2271
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002272unsigned int Renderer11::getMaxTransformFeedbackBuffers() const
2273{
shannon.woods%transgaming.com@gtempaccount.com34089352013-04-13 03:36:57 +00002274 META_ASSERT(gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D11_SO_BUFFER_SLOT_COUNT &&
2275 gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS >= D3D10_SO_BUFFER_SLOT_COUNT);
2276
shannon.woods%transgaming.com@gtempaccount.com3f72ce32013-04-13 03:36:43 +00002277 switch (mFeatureLevel)
2278 {
2279 case D3D_FEATURE_LEVEL_11_0:
2280 return D3D11_SO_BUFFER_SLOT_COUNT;
2281 case D3D_FEATURE_LEVEL_10_1:
2282 case D3D_FEATURE_LEVEL_10_0:
2283 return D3D10_SO_BUFFER_SLOT_COUNT;
2284 default: UNREACHABLE();
2285 return 0;
2286 }
2287}
2288
shannonwoods@chromium.org33e798f2013-05-30 00:05:05 +00002289unsigned int Renderer11::getMaxUniformBufferSize() const
2290{
2291 // Each component is a 4-element vector of 4-byte units (floats)
2292 const unsigned int bytesPerComponent = 4 * sizeof(float);
2293
2294 switch (mFeatureLevel)
2295 {
2296 case D3D_FEATURE_LEVEL_11_0:
2297 return D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2298 case D3D_FEATURE_LEVEL_10_1:
2299 case D3D_FEATURE_LEVEL_10_0:
2300 return D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * bytesPerComponent;
2301 default: UNREACHABLE();
2302 return 0;
2303 }
2304}
2305
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002306bool Renderer11::getNonPower2TextureSupport() const
2307{
shannon.woods@transgaming.com03951cf2013-01-25 21:57:01 +00002308 switch (mFeatureLevel)
2309 {
2310 case D3D_FEATURE_LEVEL_11_0:
2311 case D3D_FEATURE_LEVEL_10_1:
2312 case D3D_FEATURE_LEVEL_10_0:
2313 return true;
2314 default: UNREACHABLE();
2315 return false;
2316 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002317}
2318
2319bool Renderer11::getOcclusionQuerySupport() const
2320{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00002321 switch (mFeatureLevel)
2322 {
2323 case D3D_FEATURE_LEVEL_11_0:
2324 case D3D_FEATURE_LEVEL_10_1:
2325 case D3D_FEATURE_LEVEL_10_0:
2326 return true;
2327 default: UNREACHABLE();
2328 return false;
2329 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002330}
2331
2332bool Renderer11::getInstancingSupport() const
2333{
daniel@transgaming.comfe324642013-02-01 03:20:11 +00002334 switch (mFeatureLevel)
2335 {
2336 case D3D_FEATURE_LEVEL_11_0:
2337 case D3D_FEATURE_LEVEL_10_1:
2338 case D3D_FEATURE_LEVEL_10_0:
2339 return true;
2340 default: UNREACHABLE();
2341 return false;
2342 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002343}
2344
2345bool Renderer11::getShareHandleSupport() const
2346{
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002347 // We only currently support share handles with BGRA surfaces, because
2348 // chrome needs BGRA. Once chrome fixes this, we should always support them.
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002349 // PIX doesn't seem to support using share handles, so disable them.
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +00002350 return getBGRATextureSupport() && !gl::perfActive();
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002351}
2352
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002353bool Renderer11::getDerivativeInstructionSupport() const
2354{
shannon.woods@transgaming.com09fd9452013-02-28 23:02:28 +00002355 switch (mFeatureLevel)
2356 {
2357 case D3D_FEATURE_LEVEL_11_0:
2358 case D3D_FEATURE_LEVEL_10_1:
2359 case D3D_FEATURE_LEVEL_10_0:
2360 return true;
2361 default: UNREACHABLE();
2362 return false;
2363 }
daniel@transgaming.com7629bb62013-01-11 04:12:28 +00002364}
2365
shannon.woods@transgaming.com8d2f0862013-02-28 23:09:19 +00002366bool Renderer11::getPostSubBufferSupport() const
2367{
2368 // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2.
2369 return false;
2370}
2371
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002372int Renderer11::getMajorShaderModel() const
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002373{
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002374 switch (mFeatureLevel)
2375 {
2376 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002377 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4
daniel@transgaming.com9549bea2012-11-28 20:57:23 +00002378 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4
2379 default: UNREACHABLE(); return 0;
2380 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002381}
2382
daniel@transgaming.comca1ac1f2013-01-11 04:13:05 +00002383int Renderer11::getMinorShaderModel() const
2384{
2385 switch (mFeatureLevel)
2386 {
2387 case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0
2388 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1
2389 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0
2390 default: UNREACHABLE(); return 0;
2391 }
2392}
2393
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002394float Renderer11::getMaxPointSize() const
2395{
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00002396 // choose a reasonable maximum. we enforce this in the shader.
2397 // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size)
2398 return 1024.0f;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002399}
2400
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002401int Renderer11::getMaxViewportDimension() const
2402{
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002403 // Maximum viewport size must be at least as large as the largest render buffer (or larger).
2404 // In our case return the maximum texture size, which is the maximum render buffer size.
2405 META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX);
2406 META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX);
2407
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002408 switch (mFeatureLevel)
2409 {
2410 case D3D_FEATURE_LEVEL_11_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002411 return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002412 case D3D_FEATURE_LEVEL_10_1:
2413 case D3D_FEATURE_LEVEL_10_0:
shannon.woods@transgaming.comfd86c2c2013-02-28 23:13:07 +00002414 return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
shannon.woods@transgaming.com8ce2f8f2013-02-28 23:07:10 +00002415 default: UNREACHABLE();
2416 return 0;
2417 }
2418}
2419
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002420int Renderer11::getMaxTextureWidth() const
2421{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002422 switch (mFeatureLevel)
2423 {
2424 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2425 case D3D_FEATURE_LEVEL_10_1:
2426 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2427 default: UNREACHABLE(); return 0;
2428 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002429}
2430
2431int Renderer11::getMaxTextureHeight() const
2432{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002433 switch (mFeatureLevel)
2434 {
2435 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384
2436 case D3D_FEATURE_LEVEL_10_1:
2437 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192
2438 default: UNREACHABLE(); return 0;
2439 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002440}
2441
shannon.woods%transgaming.com@gtempaccount.comc1fdf6b2013-04-13 03:44:41 +00002442int Renderer11::getMaxTextureDepth() const
2443{
2444 switch (mFeatureLevel)
2445 {
2446 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2447 case D3D_FEATURE_LEVEL_10_1:
2448 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE3D_U_V_OR_W_DIMENSION; // 2048
2449 default: UNREACHABLE(); return 0;
2450 }
2451}
2452
shannon.woods%transgaming.com@gtempaccount.coma98a8112013-04-13 03:45:57 +00002453int Renderer11::getMaxTextureArrayLayers() const
2454{
2455 switch (mFeatureLevel)
2456 {
2457 case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 2048
2458 case D3D_FEATURE_LEVEL_10_1:
2459 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION; // 512
2460 default: UNREACHABLE(); return 0;
2461 }
2462}
2463
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002464bool Renderer11::get32BitIndexSupport() const
2465{
daniel@transgaming.com25072f62012-11-28 19:31:32 +00002466 switch (mFeatureLevel)
2467 {
2468 case D3D_FEATURE_LEVEL_11_0:
2469 case D3D_FEATURE_LEVEL_10_1:
2470 case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true
2471 default: UNREACHABLE(); return false;
2472 }
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002473}
2474
2475int Renderer11::getMinSwapInterval() const
2476{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002477 return 0;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002478}
2479
2480int Renderer11::getMaxSwapInterval() const
2481{
daniel@transgaming.com8c7b1a92012-11-28 19:34:06 +00002482 return 4;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002483}
2484
2485int Renderer11::getMaxSupportedSamples() const
2486{
shannon.woods@transgaming.comdf2fd572013-02-28 23:05:40 +00002487 return mMaxSupportedSamples;
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00002488}
2489
Geoff Lang0e120e32013-05-29 10:23:55 -04002490GLsizei Renderer11::getMaxSupportedFormatSamples(GLint internalFormat) const
2491{
2492 DXGI_FORMAT format = gl_d3d11::GetTexFormat(internalFormat, getCurrentClientVersion());
2493 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2494 return (iter != mMultisampleSupportMap.end()) ? iter->second.maxSupportedSamples : 0;
2495}
2496
shannon.woods@transgaming.com88fbd0f2013-02-28 23:05:46 +00002497int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const
2498{
2499 if (requested == 0)
2500 {
2501 return 0;
2502 }
2503
2504 MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format);
2505 if (iter != mMultisampleSupportMap.end())
2506 {
2507 const MultisampleSupportInfo& info = iter->second;
2508 for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
2509 {
2510 if (info.qualityLevels[i] > 0)
2511 {
2512 return i + 1;
2513 }
2514 }
2515 }
2516
2517 return -1;
2518}
2519
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002520unsigned int Renderer11::getMaxRenderTargets() const
2521{
shannon.woods%transgaming.com@gtempaccount.comf30ccc22013-04-13 03:28:36 +00002522 META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2523 META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS);
2524
shannon.woods%transgaming.com@gtempaccount.comb290ac12013-04-13 03:28:15 +00002525 switch (mFeatureLevel)
2526 {
2527 case D3D_FEATURE_LEVEL_11_0:
2528 return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2529 case D3D_FEATURE_LEVEL_10_1:
2530 case D3D_FEATURE_LEVEL_10_0:
2531 return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8
2532 default:
2533 UNREACHABLE();
2534 return 1;
2535 }
2536}
2537
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002538bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002539{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002540 if (source && dest)
2541 {
2542 TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance());
2543 TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance());
2544
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002545 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002546 return true;
2547 }
2548
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002549 return false;
2550}
2551
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002552bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source)
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002553{
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002554 if (source && dest)
2555 {
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002556 TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance());
2557 TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002558
daniel@transgaming.come9cf5e72013-01-11 04:06:55 +00002559 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
daniel@transgaming.comb1c208f2013-01-11 04:06:49 +00002560 return true;
2561 }
2562
daniel@transgaming.comad6aee72012-11-28 19:33:42 +00002563 return false;
2564}
2565
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002566bool Renderer11::copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source)
2567{
2568 if (source && dest)
2569 {
2570 TextureStorage11_3D *source11 = TextureStorage11_3D::makeTextureStorage11_3D(source->getStorageInstance());
2571 TextureStorage11_3D *dest11 = TextureStorage11_3D::makeTextureStorage11_3D(dest->getStorageInstance());
2572
2573 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2574 return true;
2575 }
2576
2577 return false;
2578}
2579
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002580bool Renderer11::copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source)
2581{
2582 if (source && dest)
2583 {
2584 TextureStorage11_2DArray *source11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(source->getStorageInstance());
2585 TextureStorage11_2DArray *dest11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(dest->getStorageInstance());
2586
2587 mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture());
2588 return true;
2589 }
2590
2591 return false;
2592}
2593
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002594bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002595 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002596{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002597 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002598 if (!colorbuffer)
2599 {
2600 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002601 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002602 }
2603
2604 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2605 if (!sourceRenderTarget)
2606 {
2607 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002608 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002609 }
2610
2611 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2612 if (!source)
2613 {
2614 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002615 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002616 }
2617
2618 TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance());
2619 if (!storage11)
2620 {
2621 source->Release();
2622 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002623 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002624 }
2625
2626 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level));
2627 if (!destRenderTarget)
2628 {
2629 source->Release();
2630 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002631 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002632 }
2633
2634 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2635 if (!dest)
2636 {
2637 source->Release();
2638 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002639 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002640 }
2641
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002642 gl::Box sourceArea;
2643 sourceArea.x = sourceRect.x;
2644 sourceArea.y = sourceRect.y;
2645 sourceArea.z = 0;
2646 sourceArea.width = sourceRect.width;
2647 sourceArea.height = sourceRect.height;
2648 sourceArea.depth = 1;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002649
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002650 gl::Box destArea;
2651 destArea.x = xoffset;
2652 destArea.y = yoffset;
2653 destArea.z = 0;
2654 destArea.width = sourceRect.width;
2655 destArea.height = sourceRect.height;
2656 destArea.depth = 1;
2657
2658 bool ret = copyTexture(source, sourceArea, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1,
2659 dest, destArea, destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1, destFormat);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002660
2661 source->Release();
2662 dest->Release();
2663
2664 return ret;
daniel@transgaming.com38380882012-11-28 19:36:39 +00002665}
2666
shannon.woods@transgaming.com664916b2013-01-25 21:50:32 +00002667bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
daniel@transgaming.com87705f82012-12-20 21:10:45 +00002668 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level)
daniel@transgaming.com38380882012-11-28 19:36:39 +00002669{
shannon.woods%transgaming.com@gtempaccount.com89ae1132013-04-13 03:28:43 +00002670 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002671 if (!colorbuffer)
2672 {
2673 ERR("Failed to retrieve the color buffer from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002674 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002675 }
2676
2677 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2678 if (!sourceRenderTarget)
2679 {
2680 ERR("Failed to retrieve the render target from the frame buffer.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002681 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002682 }
2683
2684 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2685 if (!source)
2686 {
2687 ERR("Failed to retrieve the render target view from the render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002688 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002689 }
2690
2691 TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance());
2692 if (!storage11)
2693 {
2694 source->Release();
2695 ERR("Failed to retrieve the texture storage from the destination.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002696 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002697 }
2698
Nicolas Capensb13f8662013-06-04 13:30:19 -04002699 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetFace(target, level));
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002700 if (!destRenderTarget)
2701 {
2702 source->Release();
2703 ERR("Failed to retrieve the render target from the destination storage.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002704 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002705 }
2706
2707 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2708 if (!dest)
2709 {
2710 source->Release();
2711 ERR("Failed to retrieve the render target view from the destination render target.");
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00002712 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002713 }
2714
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002715 gl::Box sourceArea;
2716 sourceArea.x = sourceRect.x;
2717 sourceArea.y = sourceRect.y;
2718 sourceArea.z = 0;
2719 sourceArea.width = sourceRect.width;
2720 sourceArea.height = sourceRect.height;
2721 sourceArea.depth = 1;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002722
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002723 gl::Box destArea;
2724 destArea.x = xoffset;
2725 destArea.y = yoffset;
2726 destArea.z = 0;
2727 destArea.width = sourceRect.width;
2728 destArea.height = sourceRect.height;
2729 destArea.depth = 1;
2730
2731 bool ret = copyTexture(source, sourceArea, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1,
2732 dest, destArea, destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1, destFormat);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002733
2734 source->Release();
2735 dest->Release();
2736
2737 return ret;
2738}
2739
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002740bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2741 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level)
2742{
2743 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2744 if (!colorbuffer)
2745 {
2746 ERR("Failed to retrieve the color buffer from the frame buffer.");
2747 return gl::error(GL_OUT_OF_MEMORY, false);
2748 }
2749
2750 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2751 if (!sourceRenderTarget)
2752 {
2753 ERR("Failed to retrieve the render target from the frame buffer.");
2754 return gl::error(GL_OUT_OF_MEMORY, false);
2755 }
2756
2757 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2758 if (!source)
2759 {
2760 ERR("Failed to retrieve the render target view from the render target.");
2761 return gl::error(GL_OUT_OF_MEMORY, false);
2762 }
2763
2764 TextureStorage11_3D *storage11 = TextureStorage11_3D::makeTextureStorage11_3D(storage->getStorageInstance());
2765 if (!storage11)
2766 {
2767 source->Release();
2768 ERR("Failed to retrieve the texture storage from the destination.");
2769 return gl::error(GL_OUT_OF_MEMORY, false);
2770 }
2771
2772 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2773 if (!destRenderTarget)
2774 {
2775 source->Release();
2776 ERR("Failed to retrieve the render target from the destination storage.");
2777 return gl::error(GL_OUT_OF_MEMORY, false);
2778 }
2779
2780 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2781 if (!dest)
2782 {
2783 source->Release();
2784 ERR("Failed to retrieve the render target view from the destination render target.");
2785 return gl::error(GL_OUT_OF_MEMORY, false);
2786 }
2787
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002788 gl::Box sourceArea;
2789 sourceArea.x = sourceRect.x;
2790 sourceArea.y = sourceRect.y;
2791 sourceArea.z = 0;
2792 sourceArea.width = sourceRect.width;
2793 sourceArea.height = sourceRect.height;
2794 sourceArea.depth = 1;
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002795
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002796 gl::Box destArea;
2797 destArea.x = xoffset;
2798 destArea.y = yoffset;
2799 destArea.z = 0;
2800 destArea.width = sourceRect.width;
2801 destArea.height = sourceRect.height;
2802 destArea.depth = 1;
2803
2804 bool ret = copyTexture(source, sourceArea, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1,
2805 dest, destArea, destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1, destFormat);
shannon.woods%transgaming.com@gtempaccount.com414e82a2013-04-13 03:44:05 +00002806
2807 source->Release();
2808 dest->Release();
2809
2810 return ret;
2811}
2812
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002813bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
2814 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level)
2815{
2816 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
2817 if (!colorbuffer)
2818 {
2819 ERR("Failed to retrieve the color buffer from the frame buffer.");
2820 return gl::error(GL_OUT_OF_MEMORY, false);
2821 }
2822
2823 RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
2824 if (!sourceRenderTarget)
2825 {
2826 ERR("Failed to retrieve the render target from the frame buffer.");
2827 return gl::error(GL_OUT_OF_MEMORY, false);
2828 }
2829
2830 ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView();
2831 if (!source)
2832 {
2833 ERR("Failed to retrieve the render target view from the render target.");
2834 return gl::error(GL_OUT_OF_MEMORY, false);
2835 }
2836
2837 TextureStorage11_2DArray *storage11 = TextureStorage11_2DArray::makeTextureStorage11_2DArray(storage->getStorageInstance());
2838 if (!storage11)
2839 {
2840 source->Release();
2841 ERR("Failed to retrieve the texture storage from the destination.");
2842 return gl::error(GL_OUT_OF_MEMORY, false);
2843 }
2844
2845 RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTargetLayer(level, zOffset));
2846 if (!destRenderTarget)
2847 {
2848 source->Release();
2849 ERR("Failed to retrieve the render target from the destination storage.");
2850 return gl::error(GL_OUT_OF_MEMORY, false);
2851 }
2852
2853 ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView();
2854 if (!dest)
2855 {
2856 source->Release();
2857 ERR("Failed to retrieve the render target view from the destination render target.");
2858 return gl::error(GL_OUT_OF_MEMORY, false);
2859 }
2860
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002861 gl::Box sourceArea;
2862 sourceArea.x = sourceRect.x;
2863 sourceArea.y = sourceRect.y;
2864 sourceArea.z = 0;
2865 sourceArea.width = sourceRect.width;
2866 sourceArea.height = sourceRect.height;
2867 sourceArea.depth = 1;
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002868
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002869 gl::Box destArea;
2870 destArea.x = xoffset;
2871 destArea.y = yoffset;
2872 destArea.z = 0;
2873 destArea.width = sourceRect.width;
2874 destArea.height = sourceRect.height;
2875 destArea.depth = 1;
2876
2877 bool ret = copyTexture(source, sourceArea, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), 1,
2878 dest, destArea, destRenderTarget->getWidth(), destRenderTarget->getHeight(), 1, destFormat);
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00002879
2880 source->Release();
2881 dest->Release();
2882
2883 return ret;
2884}
2885
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002886bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Box &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight, unsigned int sourceDepth,
2887 ID3D11RenderTargetView *dest, const gl::Box &destArea, unsigned int destWidth, unsigned int destHeight, unsigned int destDepth,
2888 GLenum destFormat)
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002889{
2890 HRESULT result;
2891
2892 if (!mCopyResourcesInitialized)
2893 {
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002894 ASSERT(!mCopyVB && !mCopySampler && !mCopy2DIL && !mCopy2DVS && !mCopyRGBA2DPS && !mCopyRGB2DPS && !mCopyLum2DPS && !mCopyLumAlpha2DPS &&
2895 !mCopy3DIL && !mCopy3DVS && !mCopy3DGS && !mCopyRGBA3DPS && !mCopyRGB3DPS && !mCopyLum3DPS && !mCopyLumAlpha3DPS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002896
2897 D3D11_BUFFER_DESC vbDesc;
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002898 vbDesc.ByteWidth = std::max(sizeof(d3d11::PositionLayerTexCoord3DVertex) * 6 * getMaxTextureDepth(),
2899 sizeof(d3d11::PositionTexCoordVertex) * 4);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002900 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
2901 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
2902 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
2903 vbDesc.MiscFlags = 0;
2904 vbDesc.StructureByteStride = 0;
2905
2906 result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB);
2907 ASSERT(SUCCEEDED(result));
2908 d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer");
2909
2910 D3D11_SAMPLER_DESC samplerDesc;
2911 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
2912 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
2913 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
2914 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
2915 samplerDesc.MipLODBias = 0.0f;
2916 samplerDesc.MaxAnisotropy = 0;
2917 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
2918 samplerDesc.BorderColor[0] = 0.0f;
2919 samplerDesc.BorderColor[1] = 0.0f;
2920 samplerDesc.BorderColor[2] = 0.0f;
2921 samplerDesc.BorderColor[3] = 0.0f;
2922 samplerDesc.MinLOD = 0.0f;
2923 samplerDesc.MaxLOD = 0.0f;
2924
2925 result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler);
2926 ASSERT(SUCCEEDED(result));
2927 d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler");
2928
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002929 // Create 2D copy resources
2930 D3D11_INPUT_ELEMENT_DESC quad2DLayout[] =
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002931 {
2932 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2933 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2934 };
2935
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002936 result = mDevice->CreateInputLayout(quad2DLayout, ArraySize(quad2DLayout), g_VS_Passthrough2D, sizeof(g_VS_Passthrough2D), &mCopy2DIL);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002937 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002938 d3d11::SetDebugName(mCopy2DIL, "Renderer11 copy 2D texture input layout");
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002939
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002940 result = mDevice->CreateVertexShader(g_VS_Passthrough2D, sizeof(g_VS_Passthrough2D), NULL, &mCopy2DVS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002941 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002942 d3d11::SetDebugName(mCopy2DVS, "Renderer11 copy 2D texture vertex shader");
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002943
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002944 result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA2D, sizeof(g_PS_PassthroughRGBA2D), NULL, &mCopyRGBA2DPS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002945 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002946 d3d11::SetDebugName(mCopyRGBA2DPS, "Renderer11 copy 2D texture RGBA pixel shader");
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002947
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002948 result = mDevice->CreatePixelShader(g_PS_PassthroughRGB2D, sizeof(g_PS_PassthroughRGB2D), NULL, &mCopyRGB2DPS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002949 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002950 d3d11::SetDebugName(mCopyRGB2DPS, "Renderer11 copy 2D texture RGB pixel shader");
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002951
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002952 result = mDevice->CreatePixelShader(g_PS_PassthroughLum2D, sizeof(g_PS_PassthroughLum2D), NULL, &mCopyLum2DPS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002953 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002954 d3d11::SetDebugName(mCopyLum2DPS, "Renderer11 copy 2D texture luminance pixel shader");
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002955
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002956 result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha2D, sizeof(g_PS_PassthroughLumAlpha2D), NULL, &mCopyLumAlpha2DPS);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002957 ASSERT(SUCCEEDED(result));
shannonwoods@chromium.orgad8a0d32013-05-30 00:03:28 +00002958 d3d11::SetDebugName(mCopyLumAlpha2DPS, "Renderer11 2D copy texture luminance alpha pixel shader");
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002959
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00002960 // Create 3D copy resources
2961 D3D11_INPUT_ELEMENT_DESC quad3DLayout[] =
2962 {
2963 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2964 { "LAYER", 0, DXGI_FORMAT_R32_UINT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2965 { "TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
2966 };
2967
2968 result = mDevice->CreateInputLayout(quad3DLayout, ArraySize(quad3DLayout), g_VS_Passthrough3D, sizeof(g_VS_Passthrough3D), &mCopy3DIL);
2969 ASSERT(SUCCEEDED(result));
2970 d3d11::SetDebugName(mCopy3DIL, "Renderer11 copy 3D texture input layout");
2971
2972 result = mDevice->CreateVertexShader(g_VS_Passthrough3D, sizeof(g_VS_Passthrough3D), NULL, &mCopy3DVS);
2973 ASSERT(SUCCEEDED(result));
2974 d3d11::SetDebugName(mCopy3DVS, "Renderer11 copy 3D texture vertex shader");
2975
2976 result = mDevice->CreateGeometryShader(g_GS_Passthrough3D, sizeof(g_GS_Passthrough3D), NULL, &mCopy3DGS);
2977 ASSERT(SUCCEEDED(result));
2978 d3d11::SetDebugName(mCopy3DGS, "Renderer11 copy 3D texture geometry shader");
2979
2980 result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA3D, sizeof(g_PS_PassthroughRGBA3D), NULL, &mCopyRGBA3DPS);
2981 ASSERT(SUCCEEDED(result));
2982 d3d11::SetDebugName(mCopyRGBA3DPS, "Renderer11 copy 3D texture RGBA pixel shader");
2983
2984 result = mDevice->CreatePixelShader(g_PS_PassthroughRGB3D, sizeof(g_PS_PassthroughRGB3D), NULL, &mCopyRGB3DPS);
2985 ASSERT(SUCCEEDED(result));
2986 d3d11::SetDebugName(mCopyRGB3DPS, "Renderer11 copy 3D texture RGB pixel shader");
2987
2988 result = mDevice->CreatePixelShader(g_PS_PassthroughLum3D, sizeof(g_PS_PassthroughLum3D), NULL, &mCopyLum3DPS);
2989 ASSERT(SUCCEEDED(result));
2990 d3d11::SetDebugName(mCopyLum3DPS, "Renderer11 copy 3D texture luminance pixel shader");
2991
2992 result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha3D, sizeof(g_PS_PassthroughLumAlpha3D), NULL, &mCopyLumAlpha3DPS);
2993 ASSERT(SUCCEEDED(result));
2994 d3d11::SetDebugName(mCopyLumAlpha3DPS, "Renderer11 3D copy texture luminance alpha pixel shader");
2995
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00002996 mCopyResourcesInitialized = true;
2997 }
2998
2999 // Verify the source and destination area sizes
3000 if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) ||
3001 sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) ||
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003002 sourceArea.z < 0 || sourceArea.z + sourceArea.depth > static_cast<int>(sourceDepth) ||
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003003 destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) ||
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003004 destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight) ||
3005 destArea.z < 0 || destArea.z + destArea.depth > static_cast<int>(destDepth))
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003006 {
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00003007 return gl::error(GL_INVALID_VALUE, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003008 }
3009
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003010 bool use3DCopy = sourceArea.depth > 1;
3011
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003012 // Set vertices
3013 D3D11_MAPPED_SUBRESOURCE mappedResource;
3014 result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
3015 if (FAILED(result))
3016 {
3017 ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result);
shannon.woods@transgaming.com779aa262013-02-28 23:04:58 +00003018 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003019 }
3020
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003021 // Create a quad in homogeneous coordinates
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00003022 float x1 = (destArea.x / float(destWidth)) * 2.0f - 1.0f;
shannon.woods@transgaming.comcfdfd462013-02-28 23:13:01 +00003023 float y1 = ((destHeight - destArea.y - destArea.height) / float(destHeight)) * 2.0f - 1.0f;
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00003024 float x2 = ((destArea.x + destArea.width) / float(destWidth)) * 2.0f - 1.0f;
shannon.woods@transgaming.comcfdfd462013-02-28 23:13:01 +00003025 float y2 = ((destHeight - destArea.y) / float(destHeight)) * 2.0f - 1.0f;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003026
3027 float u1 = sourceArea.x / float(sourceWidth);
3028 float v1 = sourceArea.y / float(sourceHeight);
3029 float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth);
3030 float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight);
3031
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003032 UINT stride = 0;
3033 UINT startIdx = 0;
3034 UINT drawCount = 0;
3035 D3D11_PRIMITIVE_TOPOLOGY topology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED;
3036
3037 if (use3DCopy)
3038 {
3039 d3d11::PositionLayerTexCoord3DVertex *vertices = static_cast<d3d11::PositionLayerTexCoord3DVertex*>(mappedResource.pData);
3040
3041 for (unsigned int i = 0; i < destDepth; i++)
3042 {
3043 float readDepth = ((i * 2) + 0.5f) / (sourceDepth - 1);
3044
3045 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 0], x1, y1, i, u1, v2, readDepth);
3046 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 1], x1, y2, i, u1, v1, readDepth);
3047 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 2], x2, y1, i, u2, v2, readDepth);
3048
3049 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 3], x1, y2, i, u1, v1, readDepth);
3050 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 4], x2, y2, i, u2, v1, readDepth);
3051 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 5], x2, y1, i, u2, v2, readDepth);
3052 }
3053
3054 stride = sizeof(d3d11::PositionLayerTexCoord3DVertex);
3055 drawCount = destDepth * 6;
3056 topology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
3057 }
3058 else
3059 {
3060 d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData);
3061
3062 d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2);
3063 d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1);
3064 d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2);
3065 d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1);
3066
3067 stride = sizeof(d3d11::PositionTexCoordVertex);
3068 drawCount = 4;
3069 topology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
3070 }
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003071
3072 mDeviceContext->Unmap(mCopyVB, 0);
3073
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003074 mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx);
3075
3076 // Apply state
shannon.woods@transgaming.comc25537e2013-02-28 23:06:03 +00003077 mDeviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003078 mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
3079 mDeviceContext->RSSetState(NULL);
3080
3081 // Apply shaders
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003082 ID3D11InputLayout *il = NULL;
3083 ID3D11VertexShader *vs = NULL;
3084 ID3D11GeometryShader *gs = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003085 ID3D11PixelShader *ps = NULL;
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003086
3087 if (use3DCopy)
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003088 {
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003089 il = mCopy3DIL;
3090 vs = mCopy3DVS;
3091 gs = mCopy3DGS;
3092
3093 switch(destFormat)
3094 {
3095 case GL_RGBA: ps = mCopyRGBA3DPS; break;
3096 case GL_RGB: ps = mCopyRGB3DPS; break;
3097 case GL_ALPHA: ps = mCopyRGBA3DPS; break;
3098 case GL_BGRA_EXT: ps = mCopyRGBA3DPS; break;
3099 case GL_LUMINANCE: ps = mCopyLum3DPS; break;
3100 case GL_LUMINANCE_ALPHA: ps = mCopyLumAlpha3DPS; break;
3101 default: UNREACHABLE(); ps = NULL; break;
3102 }
3103 }
3104 else
3105 {
3106 il = mCopy2DIL;
3107 vs = mCopy2DVS;
3108 gs = NULL;
3109
3110 switch(destFormat)
3111 {
3112 case GL_RGBA: ps = mCopyRGBA2DPS; break;
3113 case GL_RGB: ps = mCopyRGB2DPS; break;
3114 case GL_ALPHA: ps = mCopyRGBA2DPS; break;
3115 case GL_BGRA_EXT: ps = mCopyRGBA2DPS; break;
3116 case GL_LUMINANCE: ps = mCopyLum2DPS; break;
3117 case GL_LUMINANCE_ALPHA: ps = mCopyLumAlpha2DPS; break;
3118 default: UNREACHABLE(); ps = NULL; break;
3119 }
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003120 }
3121
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003122 mDeviceContext->IASetInputLayout(il);
3123 mDeviceContext->IASetPrimitiveTopology(topology);
3124 mDeviceContext->VSSetShader(vs, NULL, 0);
3125
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003126 mDeviceContext->PSSetShader(ps, NULL, 0);
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003127 mDeviceContext->GSSetShader(gs, NULL, 0);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003128
3129 // Unset the currently bound shader resource to avoid conflicts
3130 static ID3D11ShaderResourceView *const nullSRV = NULL;
3131 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
3132
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00003133 // Apply render target
3134 setOneTimeRenderTarget(dest);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003135
3136 // Set the viewport
3137 D3D11_VIEWPORT viewport;
3138 viewport.TopLeftX = 0;
3139 viewport.TopLeftY = 0;
3140 viewport.Width = destWidth;
3141 viewport.Height = destHeight;
3142 viewport.MinDepth = 0.0f;
3143 viewport.MaxDepth = 1.0f;
3144 mDeviceContext->RSSetViewports(1, &viewport);
3145
3146 // Apply textures
3147 mDeviceContext->PSSetShaderResources(0, 1, &source);
3148 mDeviceContext->PSSetSamplers(0, 1, &mCopySampler);
3149
3150 // Draw the quad
shannonwoods@chromium.org7b61d5c2013-05-30 00:04:12 +00003151 mDeviceContext->Draw(drawCount, 0);
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003152
3153 // Unbind textures and render targets and vertex buffer
3154 mDeviceContext->PSSetShaderResources(0, 1, &nullSRV);
3155
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00003156 unapplyRenderTargets();
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003157
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00003158 UINT zero = 0;
3159 ID3D11Buffer *const nullBuffer = NULL;
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00003160 mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
3161
3162 markAllStateDirty();
3163
3164 return true;
daniel@transgaming.com38380882012-11-28 19:36:39 +00003165}
3166
shannon.woods%transgaming.com@gtempaccount.comba2744f2013-04-13 03:33:38 +00003167void Renderer11::unapplyRenderTargets()
3168{
3169 setOneTimeRenderTarget(NULL);
3170}
3171
3172void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView)
3173{
3174 ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL};
3175
3176 rtvArray[0] = renderTargetView;
3177
3178 mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL);
3179
3180 // Do not preserve the serial for this one-time-use render target
3181 for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++)
3182 {
3183 mAppliedRenderTargetSerials[rtIndex] = 0;
3184 }
3185}
3186
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003187RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth)
3188{
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00003189 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00003190 RenderTarget11 *renderTarget = NULL;
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00003191
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00003192 if (depth)
3193 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00003194 // Note: depth stencil may be NULL for 0 sized surfaces
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00003195 renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(),
3196 swapChain11->getDepthStencilTexture(), NULL,
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00003197 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00003198 }
3199 else
3200 {
shannon.woods@transgaming.com8c6d9df2013-02-28 23:08:57 +00003201 // Note: render target may be NULL for 0 sized surfaces
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00003202 renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(),
shannon.woods@transgaming.com7e232852013-02-28 23:06:15 +00003203 swapChain11->getOffscreenTexture(),
shannon.woods@transgaming.com183408d2013-01-25 21:50:07 +00003204 swapChain11->getRenderTargetShaderResource(),
shannonwoods@chromium.org7faf3ec2013-05-30 00:03:45 +00003205 swapChain11->getWidth(), swapChain11->getHeight(), 1);
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00003206 }
3207 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003208}
3209
3210RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth)
3211{
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +00003212 RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth);
3213 return renderTarget;
daniel@transgaming.comf2423652012-11-28 20:53:50 +00003214}
3215
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003216ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type)
daniel@transgaming.com55318902012-11-28 20:58:58 +00003217{
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003218 ShaderExecutable11 *executable = NULL;
3219
3220 switch (type)
3221 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003222 case rx::SHADER_VERTEX:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003223 {
3224 ID3D11VertexShader *vshader = NULL;
3225 HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader);
3226 ASSERT(SUCCEEDED(result));
3227
3228 if (vshader)
3229 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003230 executable = new ShaderExecutable11(function, length, vshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003231 }
3232 }
3233 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003234 case rx::SHADER_PIXEL:
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003235 {
3236 ID3D11PixelShader *pshader = NULL;
3237 HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader);
3238 ASSERT(SUCCEEDED(result));
3239
3240 if (pshader)
3241 {
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +00003242 executable = new ShaderExecutable11(function, length, pshader);
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003243 }
3244 }
3245 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00003246 case rx::SHADER_GEOMETRY:
3247 {
3248 ID3D11GeometryShader *gshader = NULL;
3249 HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader);
3250 ASSERT(SUCCEEDED(result));
3251
3252 if (gshader)
3253 {
3254 executable = new ShaderExecutable11(function, length, gshader);
3255 }
3256 }
3257 break;
daniel@transgaming.coma2f9fbe2012-11-28 21:03:40 +00003258 default:
3259 UNREACHABLE();
3260 break;
3261 }
3262
3263 return executable;
daniel@transgaming.com55318902012-11-28 20:58:58 +00003264}
3265
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003266ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type)
daniel@transgaming.coma9c71422012-11-28 20:58:45 +00003267{
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003268 const char *profile = NULL;
3269
3270 switch (type)
3271 {
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003272 case rx::SHADER_VERTEX:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003273 profile = "vs_4_0";
3274 break;
shannon.woods@transgaming.com69ff7762013-01-25 21:55:24 +00003275 case rx::SHADER_PIXEL:
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003276 profile = "ps_4_0";
3277 break;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +00003278 case rx::SHADER_GEOMETRY:
3279 profile = "gs_4_0";
3280 break;
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003281 default:
3282 UNREACHABLE();
3283 return NULL;
3284 }
3285
shannon.woods@transgaming.comd3d42082013-02-28 23:14:31 +00003286 ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003287 if (!binary)
3288 return NULL;
3289
daniel@transgaming.com2275f912012-12-20 21:13:22 +00003290 ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type);
daniel@transgaming.com071ee6a2012-11-28 21:03:21 +00003291 binary->Release();
3292
3293 return executable;
3294}
3295
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00003296VertexBuffer *Renderer11::createVertexBuffer()
3297{
daniel@transgaming.com2c4d0702012-12-20 21:08:51 +00003298 return new VertexBuffer11(this);
daniel@transgaming.com3f255b42012-12-20 21:07:35 +00003299}
3300
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00003301IndexBuffer *Renderer11::createIndexBuffer()
3302{
daniel@transgaming.com11c2af52012-12-20 21:10:01 +00003303 return new IndexBuffer11(this);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003304}
3305
shannon.woods@transgaming.com4e52b632013-02-28 23:08:01 +00003306BufferStorage *Renderer11::createBufferStorage()
3307{
3308 return new BufferStorage11(this);
3309}
3310
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003311QueryImpl *Renderer11::createQuery(GLenum type)
3312{
shannon.woods@transgaming.com8b7606a2013-02-28 23:03:47 +00003313 return new Query11(this, type);
shannon.woods@transgaming.comcfe787e2013-02-28 23:03:35 +00003314}
3315
3316FenceImpl *Renderer11::createFence()
3317{
shannon.woods@transgaming.combe58aa02013-02-28 23:03:55 +00003318 return new Fence11(this);
daniel@transgaming.com0b6d7742012-12-20 21:09:47 +00003319}
3320
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003321bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003322{
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003323 ASSERT(colorbuffer != NULL);
3324
3325 RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget());
3326 if (renderTarget)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003327 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003328 *subresourceIndex = renderTarget->getSubresourceIndex();
3329
3330 ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView();
3331 if (colorBufferRTV)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003332 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003333 ID3D11Resource *textureResource = NULL;
3334 colorBufferRTV->GetResource(&textureResource);
3335 colorBufferRTV->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003336
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003337 if (textureResource)
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003338 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003339 HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource);
3340 textureResource->Release();
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003341
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003342 if (SUCCEEDED(result))
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003343 {
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003344 return true;
3345 }
3346 else
3347 {
3348 ERR("Failed to extract the ID3D11Texture2D from the render target resource, "
3349 "HRESULT: 0x%X.", result);
shannon.woods@transgaming.comfdeacb82013-01-25 21:52:34 +00003350 }
3351 }
3352 }
3353 }
3354
3355 return false;
3356}
3357
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003358bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003359 bool blitRenderTarget, bool blitDepthStencil)
3360{
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003361 if (blitRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003362 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003363 gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003364
3365 if (!readBuffer)
3366 {
3367 ERR("Failed to retrieve the read buffer from the read framebuffer.");
3368 return gl::error(GL_OUT_OF_MEMORY, false);
3369 }
3370
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003371 RenderTarget *readRenderTarget = readBuffer->getRenderTarget();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003372
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003373 for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++)
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003374 {
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003375 if (drawTarget->isEnabledColorAttachment(colorAttachment))
3376 {
3377 gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment);
3378
3379 if (!drawBuffer)
3380 {
3381 ERR("Failed to retrieve the draw buffer from the draw framebuffer.");
3382 return gl::error(GL_OUT_OF_MEMORY, false);
3383 }
3384
3385 RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget();
3386
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003387 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, false))
shannon.woods%transgaming.com@gtempaccount.comf6863e02013-04-13 03:34:00 +00003388 {
3389 return false;
3390 }
3391 }
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003392 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003393 }
3394
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003395 if (blitDepthStencil)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003396 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003397 gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer();
3398 gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer();
3399
3400 if (!readBuffer)
3401 {
3402 ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer.");
3403 return gl::error(GL_OUT_OF_MEMORY, false);
3404 }
3405
3406 if (!drawBuffer)
3407 {
3408 ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer.");
3409 return gl::error(GL_OUT_OF_MEMORY, false);
3410 }
3411
3412 RenderTarget *readRenderTarget = readBuffer->getDepthStencil();
3413 RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil();
3414
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003415 if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, true))
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003416 {
3417 return false;
3418 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003419 }
3420
3421 return true;
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003422}
3423
3424void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
3425 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels)
3426{
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003427 ID3D11Texture2D *colorBufferTexture = NULL;
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003428 unsigned int subresourceIndex = 0;
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003429
shannon.woods%transgaming.com@gtempaccount.com00e3f0c2013-04-13 03:31:02 +00003430 gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer();
3431
3432 if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture))
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003433 {
3434 gl::Rectangle area;
3435 area.x = x;
3436 area.y = y;
3437 area.width = width;
3438 area.height = height;
3439
daniel@transgaming.com2eb7ab72013-01-11 04:10:21 +00003440 readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch,
3441 packReverseRowOrder, packAlignment, pixels);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003442
3443 colorBufferTexture->Release();
3444 colorBufferTexture = NULL;
3445 }
daniel@transgaming.com6c872172012-11-28 19:39:33 +00003446}
3447
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003448Image *Renderer11::createImage()
3449{
daniel@transgaming.coma8aac672012-12-20 21:08:00 +00003450 return new Image11();
daniel@transgaming.com244e1832012-12-20 20:52:35 +00003451}
3452
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003453void Renderer11::generateMipmap(Image *dest, Image *src)
3454{
shannon.woods@transgaming.com2b132f42013-01-25 21:52:47 +00003455 Image11 *dest11 = Image11::makeImage11(dest);
3456 Image11 *src11 = Image11::makeImage11(src);
3457 Image11::generateMipmap(dest11, src11);
daniel@transgaming.comf721fdb2012-12-20 20:53:11 +00003458}
3459
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003460TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain)
3461{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003462 SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain);
3463 return new TextureStorage11_2D(this, swapChain11);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003464}
3465
3466TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
3467{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003468 return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003469}
3470
3471TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
3472{
daniel@transgaming.com36670db2013-01-11 04:08:22 +00003473 return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size);
daniel@transgaming.com413d2712012-12-20 21:11:04 +00003474}
3475
shannon.woods%transgaming.com@gtempaccount.com2058d642013-04-13 03:42:50 +00003476TextureStorage *Renderer11::createTextureStorage3D(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3477{
3478 return new TextureStorage11_3D(this, levels, internalformat, usage, width, height, depth);
3479}
3480
shannon.woods%transgaming.com@gtempaccount.com6c86bd52013-04-13 03:45:45 +00003481TextureStorage *Renderer11::createTextureStorage2DArray(int levels, GLenum internalformat, GLenum usage, GLsizei width, GLsizei height, GLsizei depth)
3482{
3483 return new TextureStorage11_2DArray(this, levels, internalformat, usage, width, height, depth);
3484}
3485
shannonwoods@chromium.orgb36e29f2013-05-30 00:16:30 +00003486static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType, GLuint clientVersion)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003487{
shannonwoods@chromium.orgb36e29f2013-05-30 00:16:30 +00003488 GLint sourceInternalFormat = d3d11_gl::GetInternalFormat(sourceFormat);
3489 GLenum sourceGLFormat = gl::GetFormat(sourceInternalFormat, clientVersion);
3490 GLenum sourceGLType = gl::GetType(sourceInternalFormat, clientVersion);
3491
3492 if (sourceGLFormat == destFormat && sourceGLType == destType)
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003493 {
shannonwoods@chromium.orgb36e29f2013-05-30 00:16:30 +00003494 return gl::GetPixelBytes(sourceInternalFormat, clientVersion);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003495 }
3496 else
3497 {
3498 return 0;
3499 }
3500}
3501
3502static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x,
3503 unsigned int y, int inputPitch, gl::Color *outColor)
3504{
3505 switch (format)
3506 {
3507 case DXGI_FORMAT_R8G8B8A8_UNORM:
shannon.woods%transgaming.com@gtempaccount.com8dce6512013-04-13 03:42:19 +00003508 case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003509 {
3510 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
shannon.woods@transgaming.com25950ea2013-02-28 23:04:15 +00003511 outColor->red = (rgba & 0x000000FF) * (1.0f / 0x000000FF);
3512 outColor->green = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00);
3513 outColor->blue = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000);
3514 outColor->alpha = (rgba & 0xFF000000) * (1.0f / 0xFF000000);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003515 }
3516 break;
3517
3518 case DXGI_FORMAT_A8_UNORM:
3519 {
3520 outColor->red = 0.0f;
3521 outColor->green = 0.0f;
3522 outColor->blue = 0.0f;
3523 outColor->alpha = *(data + x + y * inputPitch) / 255.0f;
3524 }
3525 break;
3526
3527 case DXGI_FORMAT_R32G32B32A32_FLOAT:
3528 {
3529 outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0);
3530 outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1);
3531 outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2);
3532 outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3);
3533 }
3534 break;
3535
3536 case DXGI_FORMAT_R32G32B32_FLOAT:
3537 {
3538 outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0);
3539 outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1);
3540 outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2);
3541 outColor->alpha = 1.0f;
3542 }
3543 break;
3544
3545 case DXGI_FORMAT_R16G16B16A16_FLOAT:
3546 {
3547 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0));
3548 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1));
3549 outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2));
3550 outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3));
3551 }
3552 break;
3553
3554 case DXGI_FORMAT_B8G8R8A8_UNORM:
3555 {
3556 unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
shannon.woods@transgaming.com25950ea2013-02-28 23:04:15 +00003557 outColor->red = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000);
3558 outColor->blue = (bgra & 0x000000FF) * (1.0f / 0x000000FF);
3559 outColor->green = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00);
3560 outColor->alpha = (bgra & 0xFF000000) * (1.0f / 0xFF000000);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003561 }
3562 break;
3563
3564 case DXGI_FORMAT_R8_UNORM:
3565 {
3566 outColor->red = *(data + x + y * inputPitch) / 255.0f;
3567 outColor->green = 0.0f;
3568 outColor->blue = 0.0f;
3569 outColor->alpha = 1.0f;
3570 }
3571 break;
3572
3573 case DXGI_FORMAT_R8G8_UNORM:
3574 {
3575 unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch);
3576
3577 outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00);
3578 outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF);
3579 outColor->blue = 0.0f;
3580 outColor->alpha = 1.0f;
3581 }
3582 break;
3583
3584 case DXGI_FORMAT_R16_FLOAT:
3585 {
3586 outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch));
3587 outColor->green = 0.0f;
3588 outColor->blue = 0.0f;
3589 outColor->alpha = 1.0f;
3590 }
3591 break;
3592
3593 case DXGI_FORMAT_R16G16_FLOAT:
3594 {
3595 outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0));
3596 outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1));
3597 outColor->blue = 0.0f;
3598 outColor->alpha = 1.0f;
3599 }
3600 break;
3601
shannon.woods%transgaming.com@gtempaccount.com8dce6512013-04-13 03:42:19 +00003602 case DXGI_FORMAT_R10G10B10A2_UNORM:
3603 {
3604 unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch);
3605 outColor->red = (rgba & 0x000003FF) * (1.0f / 0x000003FF);
3606 outColor->green = (rgba & 0x000FFC00) * (1.0f / 0x000FFC00);
3607 outColor->blue = (rgba & 0x3FF00000) * (1.0f / 0x3FF00000);
3608 outColor->alpha = (rgba & 0xC0000000) * (1.0f / 0xC0000000);
3609 }
3610 break;
3611
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003612 default:
3613 ERR("ReadPixelColor not implemented for DXGI format %u.", format);
3614 UNIMPLEMENTED();
3615 break;
3616 }
3617}
3618
3619static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x,
3620 unsigned int y, int outputPitch, void *outData)
3621{
3622 unsigned char* byteData = reinterpret_cast<unsigned char*>(outData);
3623 unsigned short* shortData = reinterpret_cast<unsigned short*>(outData);
shannonwoods@chromium.org5d4468e2013-05-30 00:13:56 +00003624 unsigned int* intData = reinterpret_cast<unsigned int*>(outData);
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003625
3626 switch (format)
3627 {
3628 case GL_RGBA:
3629 switch (type)
3630 {
3631 case GL_UNSIGNED_BYTE:
3632 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
3633 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3634 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3635 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
3636 break;
3637
shannonwoods@chromium.org5d4468e2013-05-30 00:13:56 +00003638 case GL_UNSIGNED_INT_2_10_10_10_REV:
3639 intData[x + y * outputPitch / sizeof(unsigned int)] = (static_cast<unsigned int>( 3 * color.alpha) << 30) |
3640 (static_cast<unsigned int>(1023 * color.red ) << 20) |
3641 (static_cast<unsigned int>(1023 * color.green) << 10) |
3642 (static_cast<unsigned int>(1023 * color.blue ) << 0);
3643 break;
3644
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003645 default:
3646 ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type);
3647 UNIMPLEMENTED();
3648 break;
3649 }
3650 break;
3651
3652 case GL_BGRA_EXT:
3653 switch (type)
3654 {
3655 case GL_UNSIGNED_BYTE:
3656 byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3657 byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3658 byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f);
3659 byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f);
3660 break;
3661
3662 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
3663 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
3664 // this type is packed as follows:
3665 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3666 // --------------------------------------------------------------------------------
3667 // | 4th | 3rd | 2nd | 1st component |
3668 // --------------------------------------------------------------------------------
3669 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
3670 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3671 (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) |
3672 (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) |
3673 (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) |
3674 (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0);
3675 break;
3676
3677 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
3678 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
3679 // this type is packed as follows:
3680 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
3681 // --------------------------------------------------------------------------------
3682 // | 4th | 3rd | 2nd | 1st component |
3683 // --------------------------------------------------------------------------------
3684 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
3685 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3686 (static_cast<unsigned short>( color.alpha + 0.5f) << 15) |
3687 (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) |
3688 (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) |
3689 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0);
3690 break;
3691
3692 default:
3693 ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type);
3694 UNIMPLEMENTED();
3695 break;
3696 }
3697 break;
3698
3699 case GL_RGB:
3700 switch (type)
3701 {
3702 case GL_UNSIGNED_SHORT_5_6_5:
3703 shortData[x + y * outputPitch / sizeof(unsigned short)] =
3704 (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) |
3705 (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) |
3706 (static_cast<unsigned short>(31 * color.red + 0.5f) << 11);
3707 break;
3708
3709 case GL_UNSIGNED_BYTE:
3710 byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f);
3711 byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f);
3712 byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f);
3713 break;
3714
3715 default:
3716 ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type);
3717 UNIMPLEMENTED();
3718 break;
3719 }
3720 break;
3721
3722 default:
3723 ERR("WritePixelColor not implemented for format 0x%X.", format);
3724 UNIMPLEMENTED();
3725 break;
3726 }
3727}
3728
3729void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area,
3730 GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder,
3731 GLint packAlignment, void *pixels)
3732{
3733 D3D11_TEXTURE2D_DESC textureDesc;
3734 texture->GetDesc(&textureDesc);
3735
3736 D3D11_TEXTURE2D_DESC stagingDesc;
3737 stagingDesc.Width = area.width;
3738 stagingDesc.Height = area.height;
3739 stagingDesc.MipLevels = 1;
3740 stagingDesc.ArraySize = 1;
3741 stagingDesc.Format = textureDesc.Format;
3742 stagingDesc.SampleDesc.Count = 1;
3743 stagingDesc.SampleDesc.Quality = 0;
3744 stagingDesc.Usage = D3D11_USAGE_STAGING;
3745 stagingDesc.BindFlags = 0;
3746 stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
3747 stagingDesc.MiscFlags = 0;
3748
3749 ID3D11Texture2D* stagingTex = NULL;
3750 HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex);
3751 if (FAILED(result))
3752 {
3753 ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result);
3754 return;
3755 }
3756
3757 ID3D11Texture2D* srcTex = NULL;
3758 if (textureDesc.SampleDesc.Count > 1)
3759 {
3760 D3D11_TEXTURE2D_DESC resolveDesc;
3761 resolveDesc.Width = textureDesc.Width;
3762 resolveDesc.Height = textureDesc.Height;
3763 resolveDesc.MipLevels = 1;
3764 resolveDesc.ArraySize = 1;
3765 resolveDesc.Format = textureDesc.Format;
3766 resolveDesc.SampleDesc.Count = 1;
3767 resolveDesc.SampleDesc.Quality = 0;
3768 resolveDesc.Usage = D3D11_USAGE_DEFAULT;
3769 resolveDesc.BindFlags = 0;
3770 resolveDesc.CPUAccessFlags = 0;
3771 resolveDesc.MiscFlags = 0;
3772
3773 result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex);
3774 if (FAILED(result))
3775 {
3776 ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result);
3777 stagingTex->Release();
3778 return;
3779 }
3780
3781 mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format);
3782 subResource = 0;
3783 }
3784 else
3785 {
3786 srcTex = texture;
3787 srcTex->AddRef();
3788 }
3789
3790 D3D11_BOX srcBox;
3791 srcBox.left = area.x;
3792 srcBox.right = area.x + area.width;
3793 srcBox.top = area.y;
3794 srcBox.bottom = area.y + area.height;
3795 srcBox.front = 0;
3796 srcBox.back = 1;
3797
3798 mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox);
3799
3800 srcTex->Release();
3801 srcTex = NULL;
3802
3803 D3D11_MAPPED_SUBRESOURCE mapping;
3804 mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping);
3805
3806 unsigned char *source;
3807 int inputPitch;
3808 if (packReverseRowOrder)
3809 {
3810 source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1);
3811 inputPitch = -static_cast<int>(mapping.RowPitch);
3812 }
3813 else
3814 {
3815 source = static_cast<unsigned char*>(mapping.pData);
3816 inputPitch = static_cast<int>(mapping.RowPitch);
3817 }
3818
shannonwoods@chromium.orgb36e29f2013-05-30 00:16:30 +00003819 unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type, getCurrentClientVersion());
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003820 if (fastPixelSize != 0)
3821 {
3822 unsigned char *dest = static_cast<unsigned char*>(pixels);
3823 for (int j = 0; j < area.height; j++)
3824 {
3825 memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize);
3826 }
3827 }
shannon.woods%transgaming.com@gtempaccount.com676dc8f2013-04-13 03:35:13 +00003828 else if (textureDesc.Format == DXGI_FORMAT_B8G8R8A8_UNORM &&
3829 format == GL_RGBA &&
3830 type == GL_UNSIGNED_BYTE)
3831 {
3832 // Fast path for swapping red with blue
3833 unsigned char *dest = static_cast<unsigned char*>(pixels);
3834
3835 for (int j = 0; j < area.height; j++)
3836 {
3837 for (int i = 0; i < area.width; i++)
3838 {
3839 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
3840 *(unsigned int*)(dest + 4 * i + j * outputPitch) =
3841 (argb & 0xFF00FF00) | // Keep alpha and green
3842 (argb & 0x00FF0000) >> 16 | // Move red to blue
3843 (argb & 0x000000FF) << 16; // Move blue to red
3844 }
3845 }
3846 }
daniel@transgaming.comee42a0a2013-01-11 04:09:15 +00003847 else
3848 {
3849 gl::Color pixelColor;
3850 for (int j = 0; j < area.height; j++)
3851 {
3852 for (int i = 0; i < area.width; i++)
3853 {
3854 readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor);
3855 writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels);
3856 }
3857 }
3858 }
3859
3860 mDeviceContext->Unmap(stagingTex, 0);
3861
3862 stagingTex->Release();
3863 stagingTex = NULL;
3864}
3865
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003866bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget,
3867 RenderTarget *drawRenderTarget, bool wholeBufferCopy)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003868{
3869 ASSERT(readRect.width == drawRect.width && readRect.height == drawRect.height);
3870
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003871 RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget);
3872 if (!readRenderTarget)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003873 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003874 ERR("Failed to retrieve the read render target from the read framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003875 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003876 }
3877
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003878 ID3D11Resource *readTexture = NULL;
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003879 unsigned int readSubresource = 0;
3880 if (readRenderTarget->getSamples() > 0)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003881 {
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003882 ID3D11Resource *unresolvedResource = readRenderTarget11->getTexture();
3883 ID3D11Texture2D *unresolvedTexture = d3d11::DynamicCastComObject<ID3D11Texture2D>(unresolvedResource);
3884 unresolvedResource->Release();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003885
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003886 if (unresolvedTexture)
3887 {
3888 readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex());
3889 readSubresource = 0;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003890
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003891 unresolvedTexture->Release();
3892 }
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003893 }
3894 else
3895 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003896 readTexture = readRenderTarget11->getTexture();
3897 readSubresource = readRenderTarget11->getSubresourceIndex();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003898 }
3899
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003900 if (!readTexture)
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003901 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003902 ERR("Failed to retrieve the read render target view from the read render target.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003903 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003904 }
3905
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003906 RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003907 if (!drawRenderTarget)
3908 {
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003909 readTexture->Release();
3910 ERR("Failed to retrieve the draw render target from the draw framebuffer.");
shannon.woods@transgaming.comea4a0c62013-02-28 23:06:29 +00003911 return gl::error(GL_OUT_OF_MEMORY, false);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003912 }
3913
shannon.woods%transgaming.com@gtempaccount.com27ac40e2013-04-13 03:43:17 +00003914 ID3D11Resource *drawTexture = drawRenderTarget11->getTexture();
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003915 unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003916
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003917 D3D11_BOX readBox;
3918 readBox.left = readRect.x;
3919 readBox.right = readRect.x + readRect.width;
3920 readBox.top = readRect.y;
3921 readBox.bottom = readRect.y + readRect.height;
3922 readBox.front = 0;
3923 readBox.back = 1;
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003924
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003925 // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox
3926 // We also require complete framebuffer copies for depth-stencil blit.
3927 D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox;
3928
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003929 mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, drawRect.x, drawRect.y, 0,
shannon.woods%transgaming.com@gtempaccount.com86df5a42013-04-13 03:34:07 +00003930 readTexture, readSubresource, pSrcBox);
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003931
shannon.woods%transgaming.com@gtempaccount.com1d64b622013-04-13 03:33:53 +00003932 readTexture->Release();
3933 drawTexture->Release();
shannon.woods@transgaming.com1e1deda2013-02-28 23:06:23 +00003934
3935 return true;
3936}
3937
shannon.woods@transgaming.comd67f9ce2013-02-28 23:06:09 +00003938ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource)
3939{
3940 D3D11_TEXTURE2D_DESC textureDesc;
3941 source->GetDesc(&textureDesc);
3942
3943 if (textureDesc.SampleDesc.Count > 1)
3944 {
3945 D3D11_TEXTURE2D_DESC resolveDesc;
3946 resolveDesc.Width = textureDesc.Width;
3947 resolveDesc.Height = textureDesc.Height;
3948 resolveDesc.MipLevels = 1;
3949 resolveDesc.ArraySize = 1;
3950 resolveDesc.Format = textureDesc.Format;
3951 resolveDesc.SampleDesc.Count = 1;
3952 resolveDesc.SampleDesc.Quality = 0;
3953 resolveDesc.Usage = textureDesc.Usage;
3954 resolveDesc.BindFlags = textureDesc.BindFlags;
3955 resolveDesc.CPUAccessFlags = 0;
3956 resolveDesc.MiscFlags = 0;
3957
3958 ID3D11Texture2D *resolveTexture = NULL;
3959 HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture);
3960 if (FAILED(result))
3961 {
3962 ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result);
3963 return NULL;
3964 }
3965
3966 mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format);
3967 return resolveTexture;
3968 }
3969 else
3970 {
3971 source->AddRef();
3972 return source;
3973 }
3974}
3975
shannonwoods@chromium.org6e4f2a62013-05-30 00:15:19 +00003976bool Renderer11::getLUID(LUID *adapterLuid) const
3977{
3978 adapterLuid->HighPart = 0;
3979 adapterLuid->LowPart = 0;
3980
3981 if (!mDxgiAdapter)
3982 {
3983 return false;
3984 }
3985
3986 DXGI_ADAPTER_DESC adapterDesc;
3987 if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc)))
3988 {
3989 return false;
3990 }
3991
3992 *adapterLuid = adapterDesc.AdapterLuid;
3993 return true;
3994}
3995
Geoff Lang61e49a52013-05-29 10:22:58 -04003996Renderer11::MultisampleSupportInfo Renderer11::getMultisampleSupportInfo(DXGI_FORMAT format)
3997{
3998 MultisampleSupportInfo supportInfo = { 0 };
3999
4000 UINT formatSupport;
4001 HRESULT result;
4002
4003 result = mDevice->CheckFormatSupport(format, &formatSupport);
4004 if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET))
4005 {
4006 for (unsigned int i = 1; i <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++)
4007 {
4008 result = mDevice->CheckMultisampleQualityLevels(format, i, &supportInfo.qualityLevels[i - 1]);
4009 if (SUCCEEDED(result) && supportInfo.qualityLevels[i - 1] > 0)
4010 {
4011 supportInfo.maxSupportedSamples = std::max(supportInfo.maxSupportedSamples, i);
4012 }
4013 else
4014 {
4015 supportInfo.qualityLevels[i - 1] = 0;
4016 }
4017 }
4018 }
4019
4020 return supportInfo;
4021}
4022
shannon.woods@transgaming.com9d971ff2013-01-25 21:50:53 +00004023}