daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1 | // |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 2 | // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer. |
| 8 | |
| 9 | #include "common/debug.h" |
daniel@transgaming.com | 5503fd0 | 2012-11-28 19:38:57 +0000 | [diff] [blame] | 10 | #include "libGLESv2/main.h" |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 11 | #include "libGLESv2/utilities.h" |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 12 | #include "libGLESv2/mathutil.h" |
daniel@transgaming.com | 18adad0 | 2012-11-28 21:04:03 +0000 | [diff] [blame] | 13 | #include "libGLESv2/Buffer.h" |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 14 | #include "libGLESv2/Program.h" |
| 15 | #include "libGLESv2/ProgramBinary.h" |
daniel@transgaming.com | 80fc332 | 2012-11-28 21:02:13 +0000 | [diff] [blame] | 16 | #include "libGLESv2/Framebuffer.h" |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 17 | #include "libGLESv2/renderer/Renderer11.h" |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 18 | #include "libGLESv2/renderer/RenderTarget11.h" |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 19 | #include "libGLESv2/renderer/renderer11_utils.h" |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 20 | #include "libGLESv2/renderer/ShaderExecutable11.h" |
daniel@transgaming.com | a60160b | 2012-11-28 19:41:15 +0000 | [diff] [blame] | 21 | #include "libGLESv2/renderer/SwapChain11.h" |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 22 | #include "libGLESv2/renderer/Image11.h" |
daniel@transgaming.com | 2c4d070 | 2012-12-20 21:08:51 +0000 | [diff] [blame] | 23 | #include "libGLESv2/renderer/VertexBuffer11.h" |
daniel@transgaming.com | 11c2af5 | 2012-12-20 21:10:01 +0000 | [diff] [blame] | 24 | #include "libGLESv2/renderer/IndexBuffer11.h" |
shannon.woods@transgaming.com | 4e52b63 | 2013-02-28 23:08:01 +0000 | [diff] [blame] | 25 | #include "libGLESv2/renderer/BufferStorage11.h" |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 26 | #include "libGLESv2/renderer/VertexDataManager.h" |
| 27 | #include "libGLESv2/renderer/IndexDataManager.h" |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 28 | #include "libGLESv2/renderer/TextureStorage11.h" |
shannon.woods@transgaming.com | 8b7606a | 2013-02-28 23:03:47 +0000 | [diff] [blame] | 29 | #include "libGLESv2/renderer/Query11.h" |
shannon.woods@transgaming.com | be58aa0 | 2013-02-28 23:03:55 +0000 | [diff] [blame] | 30 | #include "libGLESv2/renderer/Fence11.h" |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 31 | |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 32 | #include "libGLESv2/renderer/shaders/compiled/passthrough11vs.h" |
| 33 | #include "libGLESv2/renderer/shaders/compiled/passthroughrgba11ps.h" |
| 34 | #include "libGLESv2/renderer/shaders/compiled/passthroughrgb11ps.h" |
| 35 | #include "libGLESv2/renderer/shaders/compiled/passthroughlum11ps.h" |
| 36 | #include "libGLESv2/renderer/shaders/compiled/passthroughlumalpha11ps.h" |
| 37 | |
shannon.woods@transgaming.com | 34f507c | 2013-01-25 21:52:15 +0000 | [diff] [blame] | 38 | #include "libGLESv2/renderer/shaders/compiled/clear11vs.h" |
| 39 | #include "libGLESv2/renderer/shaders/compiled/clear11ps.h" |
| 40 | |
daniel@transgaming.com | ca1ac1f | 2013-01-11 04:13:05 +0000 | [diff] [blame] | 41 | #include <sstream> |
| 42 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 43 | namespace rx |
| 44 | { |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 45 | static const DXGI_FORMAT RenderTargetFormats[] = |
| 46 | { |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame] | 47 | DXGI_FORMAT_B8G8R8A8_UNORM, |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 48 | DXGI_FORMAT_R8G8B8A8_UNORM |
| 49 | }; |
| 50 | |
| 51 | static const DXGI_FORMAT DepthStencilFormats[] = |
| 52 | { |
shannon.woods@transgaming.com | eec5c63 | 2013-02-28 23:04:21 +0000 | [diff] [blame] | 53 | DXGI_FORMAT_UNKNOWN, |
| 54 | DXGI_FORMAT_D24_UNORM_S8_UINT, |
| 55 | DXGI_FORMAT_D16_UNORM |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 56 | }; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 57 | |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 58 | enum |
| 59 | { |
| 60 | MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16 |
| 61 | }; |
| 62 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 63 | Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc) |
| 64 | { |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 65 | mVertexDataManager = NULL; |
| 66 | mIndexDataManager = NULL; |
| 67 | |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 68 | mLineLoopIB = NULL; |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 69 | mTriangleFanIB = NULL; |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 70 | |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 71 | mCopyResourcesInitialized = false; |
| 72 | mCopyVB = NULL; |
| 73 | mCopySampler = NULL; |
| 74 | mCopyIL = NULL; |
| 75 | mCopyVS = NULL; |
| 76 | mCopyRGBAPS = NULL; |
| 77 | mCopyRGBPS = NULL; |
| 78 | mCopyLumPS = NULL; |
| 79 | mCopyLumAlphaPS = NULL; |
| 80 | |
shannon.woods@transgaming.com | 34f507c | 2013-01-25 21:52:15 +0000 | [diff] [blame] | 81 | mClearResourcesInitialized = false; |
| 82 | mClearVB = NULL; |
| 83 | mClearIL = NULL; |
| 84 | mClearVS = NULL; |
| 85 | mClearPS = NULL; |
| 86 | mClearScissorRS = NULL; |
| 87 | mClearNoScissorRS = NULL; |
| 88 | |
daniel@transgaming.com | bdf787f | 2013-02-01 03:20:36 +0000 | [diff] [blame] | 89 | mSyncQuery = NULL; |
| 90 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 91 | mD3d11Module = NULL; |
| 92 | mDxgiModule = NULL; |
| 93 | |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 94 | mDeviceLost = false; |
| 95 | |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 96 | mMaxSupportedSamples = 0; |
| 97 | |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 98 | mDevice = NULL; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 99 | mDeviceContext = NULL; |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 100 | mDxgiAdapter = NULL; |
| 101 | mDxgiFactory = NULL; |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 102 | |
| 103 | mDriverConstantBufferVS = NULL; |
| 104 | mDriverConstantBufferPS = NULL; |
shannon.woods@transgaming.com | bec04bf | 2013-01-25 21:53:52 +0000 | [diff] [blame] | 105 | |
| 106 | mBGRATextureSupport = false; |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 107 | |
| 108 | mIsGeometryShaderActive = false; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | Renderer11::~Renderer11() |
| 112 | { |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 113 | release(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 114 | } |
| 115 | |
daniel@transgaming.com | b64ed28 | 2012-11-28 20:54:02 +0000 | [diff] [blame] | 116 | Renderer11 *Renderer11::makeRenderer11(Renderer *renderer) |
| 117 | { |
apatrick@chromium.org | 8b400b1 | 2013-01-30 21:53:40 +0000 | [diff] [blame] | 118 | ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer)); |
daniel@transgaming.com | b64ed28 | 2012-11-28 20:54:02 +0000 | [diff] [blame] | 119 | return static_cast<rx::Renderer11*>(renderer); |
| 120 | } |
| 121 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 122 | EGLint Renderer11::initialize() |
| 123 | { |
daniel@transgaming.com | 25e16af | 2012-11-28 21:05:57 +0000 | [diff] [blame] | 124 | if (!initializeCompiler()) |
| 125 | { |
| 126 | return EGL_NOT_INITIALIZED; |
| 127 | } |
| 128 | |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 129 | mDxgiModule = LoadLibrary(TEXT("dxgi.dll")); |
| 130 | mD3d11Module = LoadLibrary(TEXT("d3d11.dll")); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 131 | |
| 132 | if (mD3d11Module == NULL || mDxgiModule == NULL) |
| 133 | { |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 134 | ERR("Could not load D3D11 or DXGI library - aborting!\n"); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 135 | return EGL_NOT_INITIALIZED; |
| 136 | } |
| 137 | |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 138 | // create the D3D11 device |
| 139 | ASSERT(mDevice == NULL); |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 140 | PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice"); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 141 | |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 142 | if (D3D11CreateDevice == NULL) |
| 143 | { |
| 144 | ERR("Could not retrieve D3D11CreateDevice address - aborting!\n"); |
| 145 | return EGL_NOT_INITIALIZED; |
| 146 | } |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 147 | |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 148 | D3D_FEATURE_LEVEL featureLevel[] = |
| 149 | { |
| 150 | D3D_FEATURE_LEVEL_11_0, |
| 151 | D3D_FEATURE_LEVEL_10_1, |
| 152 | D3D_FEATURE_LEVEL_10_0, |
| 153 | }; |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 154 | |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 155 | HRESULT result = D3D11CreateDevice(NULL, |
| 156 | D3D_DRIVER_TYPE_HARDWARE, |
| 157 | NULL, |
daniel@transgaming.com | a60160b | 2012-11-28 19:41:15 +0000 | [diff] [blame] | 158 | #if defined(_DEBUG) |
| 159 | D3D11_CREATE_DEVICE_DEBUG, |
| 160 | #else |
| 161 | 0, |
| 162 | #endif |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 163 | featureLevel, |
| 164 | sizeof(featureLevel)/sizeof(featureLevel[0]), |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 165 | D3D11_SDK_VERSION, |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 166 | &mDevice, |
| 167 | &mFeatureLevel, |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 168 | &mDeviceContext); |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 169 | |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 170 | if (!mDevice || FAILED(result)) |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 171 | { |
| 172 | ERR("Could not create D3D11 device - aborting!\n"); |
| 173 | return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer |
| 174 | } |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 175 | |
| 176 | IDXGIDevice *dxgiDevice = NULL; |
| 177 | result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice); |
| 178 | |
| 179 | if (FAILED(result)) |
| 180 | { |
| 181 | ERR("Could not query DXGI device - aborting!\n"); |
| 182 | return EGL_NOT_INITIALIZED; |
| 183 | } |
| 184 | |
| 185 | result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter); |
| 186 | |
| 187 | if (FAILED(result)) |
| 188 | { |
| 189 | ERR("Could not retrieve DXGI adapter - aborting!\n"); |
| 190 | return EGL_NOT_INITIALIZED; |
| 191 | } |
| 192 | |
| 193 | dxgiDevice->Release(); |
| 194 | |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 195 | mDxgiAdapter->GetDesc(&mAdapterDescription); |
| 196 | memset(mDescription, 0, sizeof(mDescription)); |
| 197 | wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1); |
| 198 | |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 199 | result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory); |
| 200 | |
| 201 | if (!mDxgiFactory || FAILED(result)) |
| 202 | { |
| 203 | ERR("Could not create DXGI factory - aborting!\n"); |
| 204 | return EGL_NOT_INITIALIZED; |
| 205 | } |
| 206 | |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 207 | unsigned int maxSupportedSamples = 0; |
| 208 | unsigned int rtFormatCount = sizeof(RenderTargetFormats) / sizeof(DXGI_FORMAT); |
| 209 | unsigned int dsFormatCount = sizeof(DepthStencilFormats) / sizeof(DXGI_FORMAT); |
| 210 | for (unsigned int i = 0; i < rtFormatCount + dsFormatCount; ++i) |
| 211 | { |
| 212 | DXGI_FORMAT format = (i < rtFormatCount) ? RenderTargetFormats[i] : DepthStencilFormats[i - rtFormatCount]; |
| 213 | if (format != DXGI_FORMAT_UNKNOWN) |
| 214 | { |
| 215 | UINT formatSupport; |
| 216 | result = mDevice->CheckFormatSupport(format, &formatSupport); |
| 217 | if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET)) |
| 218 | { |
| 219 | MultisampleSupportInfo supportInfo; |
| 220 | |
| 221 | for (unsigned int j = 1; j <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; j++) |
| 222 | { |
| 223 | result = mDevice->CheckMultisampleQualityLevels(format, j, &supportInfo.qualityLevels[j - 1]); |
| 224 | if (SUCCEEDED(result) && supportInfo.qualityLevels[j - 1] > 0) |
| 225 | { |
| 226 | maxSupportedSamples = std::max(j, maxSupportedSamples); |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | supportInfo.qualityLevels[j - 1] = 0; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | mMultisampleSupportMap.insert(std::make_pair(format, supportInfo)); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | mMaxSupportedSamples = maxSupportedSamples; |
| 239 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 240 | initializeDevice(); |
| 241 | |
shannon.woods@transgaming.com | bec04bf | 2013-01-25 21:53:52 +0000 | [diff] [blame] | 242 | // BGRA texture support is optional in feature levels 10 and 10_1 |
| 243 | UINT formatSupport; |
| 244 | result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport); |
| 245 | if (FAILED(result)) |
| 246 | { |
| 247 | ERR("Error checking BGRA format support: 0x%08X", result); |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET); |
| 252 | mBGRATextureSupport = (formatSupport & flags) == flags; |
| 253 | } |
| 254 | |
shannon.woods@transgaming.com | 9cdced6 | 2013-02-28 23:07:31 +0000 | [diff] [blame] | 255 | // Check floating point texture support |
| 256 | static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE; |
| 257 | static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET; |
| 258 | static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE; |
| 259 | |
| 260 | DXGI_FORMAT float16Formats[] = |
| 261 | { |
| 262 | DXGI_FORMAT_R16_FLOAT, |
| 263 | DXGI_FORMAT_R16G16_FLOAT, |
| 264 | DXGI_FORMAT_R16G16B16A16_FLOAT, |
| 265 | }; |
| 266 | |
| 267 | DXGI_FORMAT float32Formats[] = |
| 268 | { |
| 269 | DXGI_FORMAT_R32_FLOAT, |
| 270 | DXGI_FORMAT_R32G32_FLOAT, |
| 271 | DXGI_FORMAT_R32G32B32_FLOAT, |
| 272 | DXGI_FORMAT_R32G32B32A32_FLOAT, |
| 273 | }; |
| 274 | |
| 275 | mFloat16TextureSupport = true; |
| 276 | mFloat16FilterSupport = true; |
| 277 | mFloat16RenderSupport = true; |
| 278 | for (unsigned int i = 0; i < ArraySize(float16Formats); i++) |
| 279 | { |
| 280 | if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport))) |
| 281 | { |
| 282 | mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags; |
| 283 | mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags; |
| 284 | mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags; |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | mFloat16TextureSupport = false; |
| 289 | mFloat16RenderSupport = false; |
| 290 | mFloat16FilterSupport = false; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | mFloat32TextureSupport = true; |
| 295 | mFloat32FilterSupport = true; |
| 296 | mFloat32RenderSupport = true; |
| 297 | for (unsigned int i = 0; i < ArraySize(float32Formats); i++) |
| 298 | { |
| 299 | if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport))) |
| 300 | { |
| 301 | mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags; |
| 302 | mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags; |
| 303 | mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | mFloat32TextureSupport = false; |
| 308 | mFloat32FilterSupport = false; |
| 309 | mFloat32RenderSupport = false; |
| 310 | } |
| 311 | } |
| 312 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 313 | return EGL_SUCCESS; |
| 314 | } |
| 315 | |
| 316 | // do any one-time device initialization |
| 317 | // NOTE: this is also needed after a device lost/reset |
| 318 | // to reset the scene status and ensure the default states are reset. |
| 319 | void Renderer11::initializeDevice() |
| 320 | { |
daniel@transgaming.com | f8ba109 | 2012-11-28 19:37:53 +0000 | [diff] [blame] | 321 | mStateCache.initialize(mDevice); |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 322 | mInputLayoutCache.initialize(mDevice, mDeviceContext); |
| 323 | |
| 324 | ASSERT(!mVertexDataManager && !mIndexDataManager); |
| 325 | mVertexDataManager = new VertexDataManager(this); |
| 326 | mIndexDataManager = new IndexDataManager(this); |
daniel@transgaming.com | f8ba109 | 2012-11-28 19:37:53 +0000 | [diff] [blame] | 327 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 328 | markAllStateDirty(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 329 | } |
| 330 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 331 | int Renderer11::generateConfigs(ConfigDesc **configDescList) |
| 332 | { |
daniel@transgaming.com | e3e826d | 2012-11-28 19:42:35 +0000 | [diff] [blame] | 333 | unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]); |
| 334 | unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]); |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 335 | (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats]; |
| 336 | int numConfigs = 0; |
| 337 | |
daniel@transgaming.com | e3e826d | 2012-11-28 19:42:35 +0000 | [diff] [blame] | 338 | for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++) |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 339 | { |
daniel@transgaming.com | e3e826d | 2012-11-28 19:42:35 +0000 | [diff] [blame] | 340 | for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++) |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 341 | { |
| 342 | DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex]; |
| 343 | |
| 344 | UINT formatSupport = 0; |
| 345 | HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport); |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 346 | |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 347 | if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET)) |
| 348 | { |
| 349 | DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex]; |
| 350 | |
shannon.woods@transgaming.com | eec5c63 | 2013-02-28 23:04:21 +0000 | [diff] [blame] | 351 | bool depthStencilFormatOK = true; |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 352 | |
shannon.woods@transgaming.com | eec5c63 | 2013-02-28 23:04:21 +0000 | [diff] [blame] | 353 | if (depthStencilFormat != DXGI_FORMAT_UNKNOWN) |
| 354 | { |
| 355 | UINT formatSupport = 0; |
| 356 | result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport); |
| 357 | depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL); |
| 358 | } |
| 359 | |
| 360 | if (depthStencilFormatOK) |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 361 | { |
| 362 | ConfigDesc newConfig; |
| 363 | newConfig.renderTargetFormat = d3d11_gl::ConvertBackBufferFormat(renderTargetFormat); |
| 364 | newConfig.depthStencilFormat = d3d11_gl::ConvertDepthStencilFormat(depthStencilFormat); |
| 365 | newConfig.multiSample = 0; // FIXME: enumerate multi-sampling |
| 366 | newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast |
| 367 | |
| 368 | (*configDescList)[numConfigs++] = newConfig; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | return numConfigs; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | void Renderer11::deleteConfigs(ConfigDesc *configDescList) |
| 378 | { |
| 379 | delete [] (configDescList); |
| 380 | } |
| 381 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 382 | void Renderer11::sync(bool block) |
| 383 | { |
daniel@transgaming.com | bdf787f | 2013-02-01 03:20:36 +0000 | [diff] [blame] | 384 | if (block) |
| 385 | { |
| 386 | HRESULT result; |
| 387 | |
| 388 | if (!mSyncQuery) |
| 389 | { |
| 390 | D3D11_QUERY_DESC queryDesc; |
| 391 | queryDesc.Query = D3D11_QUERY_EVENT; |
| 392 | queryDesc.MiscFlags = 0; |
| 393 | |
| 394 | result = mDevice->CreateQuery(&queryDesc, &mSyncQuery); |
| 395 | ASSERT(SUCCEEDED(result)); |
| 396 | } |
| 397 | |
| 398 | mDeviceContext->End(mSyncQuery); |
| 399 | mDeviceContext->Flush(); |
| 400 | |
| 401 | do |
| 402 | { |
| 403 | result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH); |
| 404 | |
| 405 | // Keep polling, but allow other threads to do something useful first |
| 406 | Sleep(0); |
| 407 | |
| 408 | if (testDeviceLost(true)) |
| 409 | { |
| 410 | return; |
| 411 | } |
| 412 | } |
| 413 | while (result == S_FALSE); |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | mDeviceContext->Flush(); |
| 418 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 419 | } |
| 420 | |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 421 | SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat) |
| 422 | { |
daniel@transgaming.com | a60160b | 2012-11-28 19:41:15 +0000 | [diff] [blame] | 423 | return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat); |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 424 | } |
| 425 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 426 | void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState) |
| 427 | { |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 428 | if (type == gl::SAMPLER_PIXEL) |
| 429 | { |
| 430 | if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS) |
| 431 | { |
| 432 | ERR("Pixel shader sampler index %i is not valid.", index); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0) |
| 437 | { |
| 438 | ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState); |
| 439 | |
| 440 | if (!dxSamplerState) |
| 441 | { |
| 442 | ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default" |
| 443 | "sampler state for pixel shaders at slot %i.", index); |
| 444 | } |
| 445 | |
| 446 | mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState); |
| 447 | |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 448 | mCurPixelSamplerStates[index] = samplerState; |
| 449 | } |
| 450 | |
| 451 | mForceSetPixelSamplerStates[index] = false; |
| 452 | } |
| 453 | else if (type == gl::SAMPLER_VERTEX) |
| 454 | { |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 455 | if (index < 0 || index >= (int)getMaxVertexTextureImageUnits()) |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 456 | { |
| 457 | ERR("Vertex shader sampler index %i is not valid.", index); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0) |
| 462 | { |
| 463 | ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState); |
| 464 | |
| 465 | if (!dxSamplerState) |
| 466 | { |
| 467 | ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default" |
| 468 | "sampler state for vertex shaders at slot %i.", index); |
| 469 | } |
| 470 | |
| 471 | mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState); |
| 472 | |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 473 | mCurVertexSamplerStates[index] = samplerState; |
| 474 | } |
| 475 | |
| 476 | mForceSetVertexSamplerStates[index] = false; |
| 477 | } |
| 478 | else UNREACHABLE(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture) |
| 482 | { |
daniel@transgaming.com | 0785fad | 2013-01-11 04:08:10 +0000 | [diff] [blame] | 483 | ID3D11ShaderResourceView *textureSRV = NULL; |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 484 | unsigned int serial = 0; |
| 485 | bool forceSetTexture = false; |
daniel@transgaming.com | 0785fad | 2013-01-11 04:08:10 +0000 | [diff] [blame] | 486 | |
| 487 | if (texture) |
| 488 | { |
| 489 | TextureStorageInterface *texStorage = texture->getNativeTexture(); |
| 490 | if (texStorage) |
| 491 | { |
| 492 | TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance()); |
| 493 | textureSRV = storage11->getSRV(); |
| 494 | } |
| 495 | |
| 496 | // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly |
| 497 | // missing the shader resource view |
| 498 | ASSERT(textureSRV != NULL); |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 499 | |
| 500 | serial = texture->getTextureSerial(); |
| 501 | forceSetTexture = texture->hasDirtyImages(); |
daniel@transgaming.com | 0785fad | 2013-01-11 04:08:10 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | if (type == gl::SAMPLER_PIXEL) |
| 505 | { |
| 506 | if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS) |
| 507 | { |
| 508 | ERR("Pixel shader sampler index %i is not valid.", index); |
| 509 | return; |
| 510 | } |
| 511 | |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 512 | if (forceSetTexture || mCurPixelTextureSerials[index] != serial) |
| 513 | { |
| 514 | mDeviceContext->PSSetShaderResources(index, 1, &textureSRV); |
| 515 | } |
| 516 | |
| 517 | mCurPixelTextureSerials[index] = serial; |
daniel@transgaming.com | 0785fad | 2013-01-11 04:08:10 +0000 | [diff] [blame] | 518 | } |
| 519 | else if (type == gl::SAMPLER_VERTEX) |
| 520 | { |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 521 | if (index < 0 || index >= (int)getMaxVertexTextureImageUnits()) |
daniel@transgaming.com | 0785fad | 2013-01-11 04:08:10 +0000 | [diff] [blame] | 522 | { |
| 523 | ERR("Vertex shader sampler index %i is not valid.", index); |
| 524 | return; |
| 525 | } |
| 526 | |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 527 | if (forceSetTexture || mCurVertexTextureSerials[index] != serial) |
| 528 | { |
| 529 | mDeviceContext->VSSetShaderResources(index, 1, &textureSRV); |
| 530 | } |
| 531 | |
| 532 | mCurVertexTextureSerials[index] = serial; |
daniel@transgaming.com | 0785fad | 2013-01-11 04:08:10 +0000 | [diff] [blame] | 533 | } |
| 534 | else UNREACHABLE(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 535 | } |
| 536 | |
daniel@transgaming.com | 237bc7e | 2012-11-28 21:01:06 +0000 | [diff] [blame] | 537 | void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 538 | { |
daniel@transgaming.com | 237bc7e | 2012-11-28 21:01:06 +0000 | [diff] [blame] | 539 | if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0) |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 540 | { |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 541 | ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled, |
| 542 | mCurDepthSize); |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 543 | if (!dxRasterState) |
| 544 | { |
daniel@transgaming.com | 0f9b320 | 2013-01-11 04:08:05 +0000 | [diff] [blame] | 545 | ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default" |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 546 | "rasterizer state."); |
| 547 | } |
| 548 | |
| 549 | mDeviceContext->RSSetState(dxRasterState); |
| 550 | |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 551 | mCurRasterState = rasterState; |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | mForceSetRasterState = false; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, |
| 558 | unsigned int sampleMask) |
| 559 | { |
daniel@transgaming.com | f8ba109 | 2012-11-28 19:37:53 +0000 | [diff] [blame] | 560 | if (mForceSetBlendState || |
| 561 | memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 || |
| 562 | memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0 || |
| 563 | sampleMask != mCurSampleMask) |
| 564 | { |
| 565 | ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState); |
| 566 | if (!dxBlendState) |
| 567 | { |
| 568 | ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default " |
| 569 | "blend state."); |
| 570 | } |
| 571 | |
| 572 | const float blendColors[] = { blendColor.red, blendColor.green, blendColor.blue, blendColor.alpha }; |
| 573 | mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask); |
| 574 | |
daniel@transgaming.com | f8ba109 | 2012-11-28 19:37:53 +0000 | [diff] [blame] | 575 | mCurBlendState = blendState; |
| 576 | mCurBlendColor = blendColor; |
| 577 | mCurSampleMask = sampleMask; |
| 578 | } |
| 579 | |
| 580 | mForceSetBlendState = false; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 581 | } |
| 582 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 583 | void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef, |
daniel@transgaming.com | 3a0ef48 | 2012-11-28 21:01:20 +0000 | [diff] [blame] | 584 | int stencilBackRef, bool frontFaceCCW) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 585 | { |
daniel@transgaming.com | 5503fd0 | 2012-11-28 19:38:57 +0000 | [diff] [blame] | 586 | if (mForceSetDepthStencilState || |
| 587 | memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 || |
| 588 | stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef) |
| 589 | { |
| 590 | if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask || |
| 591 | stencilRef != stencilBackRef || |
| 592 | depthStencilState.stencilMask != depthStencilState.stencilBackMask) |
| 593 | { |
| 594 | ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are " |
| 595 | "invalid under WebGL."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 596 | return gl::error(GL_INVALID_OPERATION); |
daniel@transgaming.com | 5503fd0 | 2012-11-28 19:38:57 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState); |
| 600 | if (!dxDepthStencilState) |
| 601 | { |
| 602 | ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, " |
| 603 | "setting the default depth stencil state."); |
| 604 | } |
| 605 | |
| 606 | mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef)); |
| 607 | |
daniel@transgaming.com | 5503fd0 | 2012-11-28 19:38:57 +0000 | [diff] [blame] | 608 | mCurDepthStencilState = depthStencilState; |
| 609 | mCurStencilRef = stencilRef; |
| 610 | mCurStencilBackRef = stencilBackRef; |
| 611 | } |
| 612 | |
| 613 | mForceSetDepthStencilState = false; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 614 | } |
| 615 | |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 616 | void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 617 | { |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 618 | if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 || |
| 619 | enabled != mScissorEnabled) |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 620 | { |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 621 | if (enabled) |
| 622 | { |
| 623 | D3D11_RECT rect; |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 624 | rect.left = std::max(0, scissor.x); |
| 625 | rect.top = std::max(0, scissor.y); |
| 626 | rect.right = scissor.x + std::max(0, scissor.width); |
| 627 | rect.bottom = scissor.y + std::max(0, scissor.height); |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 628 | |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 629 | mDeviceContext->RSSetScissorRects(1, &rect); |
| 630 | } |
| 631 | |
| 632 | if (enabled != mScissorEnabled) |
| 633 | { |
| 634 | mForceSetRasterState = true; |
| 635 | } |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 636 | |
| 637 | mCurScissor = scissor; |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 638 | mScissorEnabled = enabled; |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | mForceSetScissor = false; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 642 | } |
| 643 | |
daniel@transgaming.com | 1298518 | 2012-12-20 20:56:31 +0000 | [diff] [blame] | 644 | bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace, |
shannon.woods@transgaming.com | 0b236e2 | 2013-01-25 21:57:07 +0000 | [diff] [blame] | 645 | bool ignoreViewport) |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 646 | { |
daniel@transgaming.com | 4c4ce23 | 2012-11-28 21:01:40 +0000 | [diff] [blame] | 647 | gl::Rectangle actualViewport = viewport; |
| 648 | float actualZNear = gl::clamp01(zNear); |
| 649 | float actualZFar = gl::clamp01(zFar); |
| 650 | if (ignoreViewport) |
| 651 | { |
| 652 | actualViewport.x = 0; |
| 653 | actualViewport.y = 0; |
| 654 | actualViewport.width = mRenderTargetDesc.width; |
| 655 | actualViewport.height = mRenderTargetDesc.height; |
| 656 | actualZNear = 0.0f; |
| 657 | actualZFar = 1.0f; |
| 658 | } |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 659 | |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 660 | // Get D3D viewport bounds, which depends on the feature level |
| 661 | const Range& viewportBounds = getViewportBounds(); |
| 662 | |
| 663 | // Clamp width and height first to the gl maximum, then clamp further if we extend past the D3D maximum bounds |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 664 | D3D11_VIEWPORT dxViewport; |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 665 | dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end); |
| 666 | dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end); |
| 667 | dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension()); |
| 668 | dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension()); |
| 669 | dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX)); |
| 670 | dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY)); |
daniel@transgaming.com | 4c4ce23 | 2012-11-28 21:01:40 +0000 | [diff] [blame] | 671 | dxViewport.MinDepth = actualZNear; |
| 672 | dxViewport.MaxDepth = actualZFar; |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 673 | |
| 674 | if (dxViewport.Width <= 0 || dxViewport.Height <= 0) |
| 675 | { |
| 676 | return false; // Nothing to render |
| 677 | } |
| 678 | |
daniel@transgaming.com | ed36abd | 2013-01-11 21:15:58 +0000 | [diff] [blame] | 679 | bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 || |
| 680 | actualZNear != mCurNear || actualZFar != mCurFar; |
daniel@transgaming.com | 4c4ce23 | 2012-11-28 21:01:40 +0000 | [diff] [blame] | 681 | |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 682 | if (viewportChanged) |
| 683 | { |
| 684 | mDeviceContext->RSSetViewports(1, &dxViewport); |
| 685 | |
daniel@transgaming.com | 4c4ce23 | 2012-11-28 21:01:40 +0000 | [diff] [blame] | 686 | mCurViewport = actualViewport; |
| 687 | mCurNear = actualZNear; |
| 688 | mCurFar = actualZFar; |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 689 | |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 690 | mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f; |
| 691 | mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f; |
| 692 | mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f); |
| 693 | mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f); |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 694 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 695 | mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f; |
| 696 | mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f; |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 697 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 698 | mVertexConstants.depthRange[0] = actualZNear; |
| 699 | mVertexConstants.depthRange[1] = actualZFar; |
| 700 | mVertexConstants.depthRange[2] = actualZFar - actualZNear; |
daniel@transgaming.com | ed36abd | 2013-01-11 21:15:58 +0000 | [diff] [blame] | 701 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 702 | mPixelConstants.depthRange[0] = actualZNear; |
| 703 | mPixelConstants.depthRange[1] = actualZFar; |
| 704 | mPixelConstants.depthRange[2] = actualZFar - actualZNear; |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | mForceSetViewport = false; |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 708 | return true; |
| 709 | } |
| 710 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 711 | bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count) |
| 712 | { |
daniel@transgaming.com | c52be63 | 2012-11-28 21:04:28 +0000 | [diff] [blame] | 713 | D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 714 | |
daniel@transgaming.com | 0b03b06 | 2012-11-28 21:03:49 +0000 | [diff] [blame] | 715 | switch (mode) |
| 716 | { |
daniel@transgaming.com | c52be63 | 2012-11-28 21:04:28 +0000 | [diff] [blame] | 717 | case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break; |
| 718 | case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break; |
daniel@transgaming.com | 1ef0967 | 2013-01-11 04:10:37 +0000 | [diff] [blame] | 719 | case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break; |
daniel@transgaming.com | c52be63 | 2012-11-28 21:04:28 +0000 | [diff] [blame] | 720 | case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break; |
| 721 | case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break; |
| 722 | case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break; |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 723 | // emulate fans via rewriting index buffer |
| 724 | case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break; |
daniel@transgaming.com | 0b03b06 | 2012-11-28 21:03:49 +0000 | [diff] [blame] | 725 | default: |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 726 | return gl::error(GL_INVALID_ENUM, false); |
daniel@transgaming.com | 0b03b06 | 2012-11-28 21:03:49 +0000 | [diff] [blame] | 727 | } |
| 728 | |
daniel@transgaming.com | c52be63 | 2012-11-28 21:04:28 +0000 | [diff] [blame] | 729 | mDeviceContext->IASetPrimitiveTopology(primitiveTopology); |
daniel@transgaming.com | 0b03b06 | 2012-11-28 21:03:49 +0000 | [diff] [blame] | 730 | |
| 731 | return count > 0; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 735 | { |
daniel@transgaming.com | 80fc332 | 2012-11-28 21:02:13 +0000 | [diff] [blame] | 736 | // Get the color render buffer and serial |
| 737 | gl::Renderbuffer *renderbufferObject = NULL; |
| 738 | unsigned int renderTargetSerial = 0; |
| 739 | if (framebuffer->getColorbufferType() != GL_NONE) |
| 740 | { |
| 741 | renderbufferObject = framebuffer->getColorbuffer(); |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 742 | |
daniel@transgaming.com | 80fc332 | 2012-11-28 21:02:13 +0000 | [diff] [blame] | 743 | if (!renderbufferObject) |
| 744 | { |
| 745 | ERR("render target pointer unexpectedly null."); |
daniel@transgaming.com | e9c71b4 | 2012-11-28 21:02:23 +0000 | [diff] [blame] | 746 | return false; |
daniel@transgaming.com | 80fc332 | 2012-11-28 21:02:13 +0000 | [diff] [blame] | 747 | } |
| 748 | |
shannon.woods@transgaming.com | 3e3da58 | 2013-02-28 23:09:03 +0000 | [diff] [blame] | 749 | // check for zero-sized default framebuffer, which is a special case. |
| 750 | // in this case we do not wish to modify any state and just silently return false. |
| 751 | // this will not report any gl error but will cause the calling method to return. |
| 752 | if (renderbufferObject->getWidth() == 0 || renderbufferObject->getHeight() == 0) |
| 753 | { |
| 754 | return false; |
| 755 | } |
| 756 | |
daniel@transgaming.com | 80fc332 | 2012-11-28 21:02:13 +0000 | [diff] [blame] | 757 | renderTargetSerial = renderbufferObject->getSerial(); |
| 758 | } |
| 759 | |
| 760 | // Get the depth stencil render buffer and serials |
| 761 | gl::Renderbuffer *depthStencil = NULL; |
| 762 | unsigned int depthbufferSerial = 0; |
| 763 | unsigned int stencilbufferSerial = 0; |
| 764 | if (framebuffer->getDepthbufferType() != GL_NONE) |
| 765 | { |
| 766 | depthStencil = framebuffer->getDepthbuffer(); |
| 767 | if (!depthStencil) |
| 768 | { |
| 769 | ERR("Depth stencil pointer unexpectedly null."); |
| 770 | return false; |
| 771 | } |
| 772 | |
| 773 | depthbufferSerial = depthStencil->getSerial(); |
| 774 | } |
| 775 | else if (framebuffer->getStencilbufferType() != GL_NONE) |
| 776 | { |
| 777 | depthStencil = framebuffer->getStencilbuffer(); |
| 778 | if (!depthStencil) |
| 779 | { |
| 780 | ERR("Depth stencil pointer unexpectedly null."); |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | stencilbufferSerial = depthStencil->getSerial(); |
| 785 | } |
| 786 | |
| 787 | // Extract the render target dimensions and view |
| 788 | unsigned int renderTargetWidth = 0; |
| 789 | unsigned int renderTargetHeight = 0; |
| 790 | GLenum renderTargetFormat = 0; |
| 791 | ID3D11RenderTargetView* framebufferRTV = NULL; |
| 792 | if (renderbufferObject) |
| 793 | { |
| 794 | RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget()); |
| 795 | if (!renderTarget) |
| 796 | { |
| 797 | ERR("render target pointer unexpectedly null."); |
| 798 | return false; |
| 799 | } |
| 800 | |
| 801 | framebufferRTV = renderTarget->getRenderTargetView(); |
| 802 | if (!framebufferRTV) |
| 803 | { |
| 804 | ERR("render target view pointer unexpectedly null."); |
| 805 | return false; |
| 806 | } |
| 807 | |
| 808 | renderTargetWidth = renderbufferObject->getWidth(); |
| 809 | renderTargetHeight = renderbufferObject->getHeight(); |
| 810 | renderTargetFormat = renderbufferObject->getActualFormat(); |
| 811 | } |
| 812 | |
| 813 | // Extract the depth stencil sizes and view |
| 814 | unsigned int depthSize = 0; |
| 815 | unsigned int stencilSize = 0; |
| 816 | ID3D11DepthStencilView* framebufferDSV = NULL; |
| 817 | if (depthStencil) |
| 818 | { |
| 819 | RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil()); |
| 820 | if (!depthStencilRenderTarget) |
| 821 | { |
| 822 | ERR("render target pointer unexpectedly null."); |
| 823 | if (framebufferRTV) |
| 824 | { |
| 825 | framebufferRTV->Release(); |
| 826 | } |
| 827 | return false; |
| 828 | } |
| 829 | |
| 830 | framebufferDSV = depthStencilRenderTarget->getDepthStencilView(); |
| 831 | if (!framebufferDSV) |
| 832 | { |
| 833 | ERR("depth stencil view pointer unexpectedly null."); |
| 834 | if (framebufferRTV) |
| 835 | { |
| 836 | framebufferRTV->Release(); |
| 837 | } |
| 838 | return false; |
| 839 | } |
| 840 | |
| 841 | // If there is no render buffer, the width, height and format values come from |
| 842 | // the depth stencil |
| 843 | if (!renderbufferObject) |
| 844 | { |
| 845 | renderTargetWidth = depthStencil->getWidth(); |
| 846 | renderTargetHeight = depthStencil->getHeight(); |
| 847 | renderTargetFormat = depthStencil->getActualFormat(); |
| 848 | } |
| 849 | |
| 850 | depthSize = depthStencil->getDepthSize(); |
| 851 | stencilSize = depthStencil->getStencilSize(); |
| 852 | } |
| 853 | |
| 854 | // Apply the render target and depth stencil |
| 855 | if (!mRenderTargetDescInitialized || !mDepthStencilInitialized || |
| 856 | renderTargetSerial != mAppliedRenderTargetSerial || |
| 857 | depthbufferSerial != mAppliedDepthbufferSerial || |
| 858 | stencilbufferSerial != mAppliedStencilbufferSerial) |
| 859 | { |
| 860 | mDeviceContext->OMSetRenderTargets(1, &framebufferRTV, framebufferDSV); |
| 861 | |
| 862 | mRenderTargetDesc.width = renderTargetWidth; |
| 863 | mRenderTargetDesc.height = renderTargetHeight; |
| 864 | mRenderTargetDesc.format = renderTargetFormat; |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 865 | mForceSetViewport = true; |
| 866 | mForceSetScissor = true; |
daniel@transgaming.com | 80fc332 | 2012-11-28 21:02:13 +0000 | [diff] [blame] | 867 | |
| 868 | if (!mDepthStencilInitialized || depthSize != mCurDepthSize) |
| 869 | { |
| 870 | mCurDepthSize = depthSize; |
| 871 | mForceSetRasterState = true; |
| 872 | } |
| 873 | |
| 874 | mCurStencilSize = stencilSize; |
| 875 | |
| 876 | mAppliedRenderTargetSerial = renderTargetSerial; |
| 877 | mAppliedDepthbufferSerial = depthbufferSerial; |
| 878 | mAppliedStencilbufferSerial = stencilbufferSerial; |
| 879 | mRenderTargetDescInitialized = true; |
| 880 | mDepthStencilInitialized = true; |
| 881 | } |
| 882 | |
| 883 | if (framebufferRTV) |
| 884 | { |
| 885 | framebufferRTV->Release(); |
| 886 | } |
| 887 | if (framebufferDSV) |
| 888 | { |
| 889 | framebufferDSV->Release(); |
| 890 | } |
daniel@transgaming.com | ae39ee2 | 2012-11-28 19:42:02 +0000 | [diff] [blame] | 891 | |
| 892 | return true; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 893 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 894 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 895 | GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances) |
daniel@transgaming.com | def9f0f | 2012-11-28 20:53:20 +0000 | [diff] [blame] | 896 | { |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 897 | TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS]; |
| 898 | GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances); |
| 899 | if (err != GL_NO_ERROR) |
daniel@transgaming.com | da495a1 | 2012-11-28 21:03:56 +0000 | [diff] [blame] | 900 | { |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 901 | return err; |
| 902 | } |
daniel@transgaming.com | da495a1 | 2012-11-28 21:03:56 +0000 | [diff] [blame] | 903 | |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 904 | return mInputLayoutCache.applyVertexBuffers(attributes, programBinary); |
daniel@transgaming.com | def9f0f | 2012-11-28 20:53:20 +0000 | [diff] [blame] | 905 | } |
| 906 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 907 | GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo) |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 908 | { |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 909 | GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 910 | |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 911 | if (err == GL_NO_ERROR) |
| 912 | { |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 913 | if (indexInfo->storage) |
| 914 | { |
| 915 | if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset) |
| 916 | { |
| 917 | BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage); |
| 918 | IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer); |
| 919 | |
| 920 | mDeviceContext->IASetIndexBuffer(storage->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset); |
| 921 | |
| 922 | mAppliedIBSerial = 0; |
| 923 | mAppliedStorageIBSerial = storage->getSerial(); |
| 924 | mAppliedIBOffset = indexInfo->startOffset; |
| 925 | } |
| 926 | } |
| 927 | else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset) |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 928 | { |
| 929 | IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer); |
| 930 | |
daniel@transgaming.com | 22ada2c | 2013-01-11 04:07:12 +0000 | [diff] [blame] | 931 | mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset); |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 932 | |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 933 | mAppliedIBSerial = indexInfo->serial; |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 934 | mAppliedStorageIBSerial = 0; |
daniel@transgaming.com | 7fbf486 | 2013-01-11 04:09:22 +0000 | [diff] [blame] | 935 | mAppliedIBOffset = indexInfo->startOffset; |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
| 939 | return err; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances) |
| 943 | { |
daniel@transgaming.com | 1ef0967 | 2013-01-11 04:10:37 +0000 | [diff] [blame] | 944 | if (mode == GL_LINE_LOOP) |
| 945 | { |
| 946 | drawLineLoop(count, GL_NONE, NULL, 0, NULL); |
| 947 | } |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 948 | else if (mode == GL_TRIANGLE_FAN) |
| 949 | { |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 950 | drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 951 | } |
daniel@transgaming.com | 1ef0967 | 2013-01-11 04:10:37 +0000 | [diff] [blame] | 952 | else if (instances > 0) |
| 953 | { |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 954 | mDeviceContext->DrawInstanced(count, instances, 0, 0); |
daniel@transgaming.com | 1ef0967 | 2013-01-11 04:10:37 +0000 | [diff] [blame] | 955 | } |
| 956 | else |
| 957 | { |
| 958 | mDeviceContext->Draw(count, 0); |
| 959 | } |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 960 | } |
| 961 | |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 962 | void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances) |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 963 | { |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 964 | if (mode == GL_LINE_LOOP) |
| 965 | { |
| 966 | drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer); |
| 967 | } |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 968 | else if (mode == GL_TRIANGLE_FAN) |
| 969 | { |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 970 | drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances); |
| 971 | } |
| 972 | else if (instances > 0) |
| 973 | { |
| 974 | mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 975 | } |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 976 | else |
| 977 | { |
daniel@transgaming.com | 9f7ede6 | 2013-01-11 04:07:06 +0000 | [diff] [blame] | 978 | mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex)); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
| 982 | void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer) |
| 983 | { |
| 984 | // Get the raw indices for an indexed draw |
| 985 | if (type != GL_NONE && elementArrayBuffer) |
| 986 | { |
| 987 | gl::Buffer *indexBuffer = elementArrayBuffer; |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 988 | BufferStorage *storage = indexBuffer->getStorage(); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 989 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 990 | indices = static_cast<const GLubyte*>(storage->getData()) + offset; |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | if (!mLineLoopIB) |
| 994 | { |
| 995 | mLineLoopIB = new StreamingIndexBufferInterface(this); |
| 996 | if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT)) |
| 997 | { |
| 998 | delete mLineLoopIB; |
| 999 | mLineLoopIB = NULL; |
| 1000 | |
| 1001 | ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1002 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | const int spaceNeeded = (count + 1) * sizeof(unsigned int); |
| 1007 | if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT)) |
| 1008 | { |
| 1009 | ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1010 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | void* mappedMemory = NULL; |
| 1014 | int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory); |
| 1015 | if (offset == -1 || mappedMemory == NULL) |
| 1016 | { |
| 1017 | ERR("Could not map index buffer for GL_LINE_LOOP."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1018 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1021 | unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory); |
daniel@transgaming.com | 7fbf486 | 2013-01-11 04:09:22 +0000 | [diff] [blame] | 1022 | unsigned int indexBufferOffset = static_cast<unsigned int>(offset); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1023 | |
| 1024 | switch (type) |
| 1025 | { |
| 1026 | case GL_NONE: // Non-indexed draw |
| 1027 | for (int i = 0; i < count; i++) |
| 1028 | { |
| 1029 | data[i] = i; |
| 1030 | } |
| 1031 | data[count] = 0; |
| 1032 | break; |
| 1033 | case GL_UNSIGNED_BYTE: |
| 1034 | for (int i = 0; i < count; i++) |
| 1035 | { |
| 1036 | data[i] = static_cast<const GLubyte*>(indices)[i]; |
| 1037 | } |
| 1038 | data[count] = static_cast<const GLubyte*>(indices)[0]; |
| 1039 | break; |
| 1040 | case GL_UNSIGNED_SHORT: |
| 1041 | for (int i = 0; i < count; i++) |
| 1042 | { |
| 1043 | data[i] = static_cast<const GLushort*>(indices)[i]; |
| 1044 | } |
| 1045 | data[count] = static_cast<const GLushort*>(indices)[0]; |
| 1046 | break; |
| 1047 | case GL_UNSIGNED_INT: |
| 1048 | for (int i = 0; i < count; i++) |
| 1049 | { |
| 1050 | data[i] = static_cast<const GLuint*>(indices)[i]; |
| 1051 | } |
| 1052 | data[count] = static_cast<const GLuint*>(indices)[0]; |
| 1053 | break; |
| 1054 | default: UNREACHABLE(); |
| 1055 | } |
| 1056 | |
| 1057 | if (!mLineLoopIB->unmapBuffer()) |
| 1058 | { |
| 1059 | ERR("Could not unmap index buffer for GL_LINE_LOOP."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1060 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
daniel@transgaming.com | 7fbf486 | 2013-01-11 04:09:22 +0000 | [diff] [blame] | 1063 | if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset) |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1064 | { |
| 1065 | IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer()); |
| 1066 | |
daniel@transgaming.com | 7fbf486 | 2013-01-11 04:09:22 +0000 | [diff] [blame] | 1067 | mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1068 | mAppliedIBSerial = mLineLoopIB->getSerial(); |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 1069 | mAppliedStorageIBSerial = 0; |
daniel@transgaming.com | 7fbf486 | 2013-01-11 04:09:22 +0000 | [diff] [blame] | 1070 | mAppliedIBOffset = indexBufferOffset; |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
daniel@transgaming.com | 1ef0967 | 2013-01-11 04:10:37 +0000 | [diff] [blame] | 1073 | mDeviceContext->DrawIndexed(count + 1, 0, -minIndex); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 1076 | void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances) |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1077 | { |
| 1078 | // Get the raw indices for an indexed draw |
| 1079 | if (type != GL_NONE && elementArrayBuffer) |
| 1080 | { |
| 1081 | gl::Buffer *indexBuffer = elementArrayBuffer; |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 1082 | BufferStorage *storage = indexBuffer->getStorage(); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1083 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 1084 | indices = static_cast<const GLubyte*>(storage->getData()) + offset; |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | if (!mTriangleFanIB) |
| 1088 | { |
| 1089 | mTriangleFanIB = new StreamingIndexBufferInterface(this); |
| 1090 | if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT)) |
| 1091 | { |
| 1092 | delete mTriangleFanIB; |
| 1093 | mTriangleFanIB = NULL; |
| 1094 | |
| 1095 | ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1096 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | const int numTris = count - 2; |
| 1101 | const int spaceNeeded = (numTris * 3) * sizeof(unsigned int); |
| 1102 | if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT)) |
| 1103 | { |
| 1104 | ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1105 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | void* mappedMemory = NULL; |
| 1109 | int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory); |
| 1110 | if (offset == -1 || mappedMemory == NULL) |
| 1111 | { |
| 1112 | ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1113 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory); |
| 1117 | unsigned int indexBufferOffset = static_cast<unsigned int>(offset); |
| 1118 | |
| 1119 | switch (type) |
| 1120 | { |
| 1121 | case GL_NONE: // Non-indexed draw |
| 1122 | for (int i = 0; i < numTris; i++) |
| 1123 | { |
| 1124 | data[i*3 + 0] = 0; |
| 1125 | data[i*3 + 1] = i + 1; |
| 1126 | data[i*3 + 2] = i + 2; |
| 1127 | } |
| 1128 | break; |
| 1129 | case GL_UNSIGNED_BYTE: |
| 1130 | for (int i = 0; i < numTris; i++) |
| 1131 | { |
| 1132 | data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0]; |
| 1133 | data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1]; |
| 1134 | data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2]; |
| 1135 | } |
| 1136 | break; |
| 1137 | case GL_UNSIGNED_SHORT: |
| 1138 | for (int i = 0; i < numTris; i++) |
| 1139 | { |
| 1140 | data[i*3 + 0] = static_cast<const GLushort*>(indices)[0]; |
| 1141 | data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1]; |
| 1142 | data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2]; |
| 1143 | } |
| 1144 | break; |
| 1145 | case GL_UNSIGNED_INT: |
| 1146 | for (int i = 0; i < numTris; i++) |
| 1147 | { |
| 1148 | data[i*3 + 0] = static_cast<const GLuint*>(indices)[0]; |
| 1149 | data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1]; |
| 1150 | data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2]; |
| 1151 | } |
| 1152 | break; |
| 1153 | default: UNREACHABLE(); |
| 1154 | } |
| 1155 | |
| 1156 | if (!mTriangleFanIB->unmapBuffer()) |
| 1157 | { |
| 1158 | ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1159 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset) |
| 1163 | { |
| 1164 | IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer()); |
| 1165 | |
| 1166 | mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset); |
| 1167 | mAppliedIBSerial = mTriangleFanIB->getSerial(); |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 1168 | mAppliedStorageIBSerial = 0; |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1169 | mAppliedIBOffset = indexBufferOffset; |
| 1170 | } |
| 1171 | |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 1172 | if (instances > 0) |
| 1173 | { |
| 1174 | mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0); |
| 1175 | } |
| 1176 | else |
| 1177 | { |
| 1178 | mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex); |
| 1179 | } |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
daniel@transgaming.com | 5fbf177 | 2012-11-28 20:54:43 +0000 | [diff] [blame] | 1182 | void Renderer11::applyShaders(gl::ProgramBinary *programBinary) |
| 1183 | { |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1184 | unsigned int programBinarySerial = programBinary->getSerial(); |
shannon.woods@transgaming.com | 43ccf3f | 2013-02-28 23:07:19 +0000 | [diff] [blame] | 1185 | const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial); |
| 1186 | |
| 1187 | if (updateProgramState) |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1188 | { |
| 1189 | ShaderExecutable *vertexExe = programBinary->getVertexExecutable(); |
| 1190 | ShaderExecutable *pixelExe = programBinary->getPixelExecutable(); |
daniel@transgaming.com | d4b2db2 | 2012-11-28 21:05:15 +0000 | [diff] [blame] | 1191 | |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1192 | ID3D11VertexShader *vertexShader = NULL; |
| 1193 | if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader(); |
daniel@transgaming.com | d4b2db2 | 2012-11-28 21:05:15 +0000 | [diff] [blame] | 1194 | |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1195 | ID3D11PixelShader *pixelShader = NULL; |
| 1196 | if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader(); |
daniel@transgaming.com | d4b2db2 | 2012-11-28 21:05:15 +0000 | [diff] [blame] | 1197 | |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1198 | mDeviceContext->PSSetShader(pixelShader, NULL, 0); |
| 1199 | mDeviceContext->VSSetShader(vertexShader, NULL, 0); |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 1200 | |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 1201 | programBinary->dirtyAllUniforms(); |
| 1202 | |
| 1203 | mAppliedProgramBinarySerial = programBinarySerial; |
| 1204 | } |
| 1205 | |
| 1206 | // Only use the geometry shader currently for point sprite drawing |
shannon.woods@transgaming.com | 43ccf3f | 2013-02-28 23:07:19 +0000 | [diff] [blame] | 1207 | const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode); |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 1208 | |
shannon.woods@transgaming.com | 43ccf3f | 2013-02-28 23:07:19 +0000 | [diff] [blame] | 1209 | if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive) |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 1210 | { |
| 1211 | if (usesGeometryShader) |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 1212 | { |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 1213 | ShaderExecutable *geometryExe = programBinary->getGeometryExecutable(); |
| 1214 | ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader(); |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 1215 | mDeviceContext->GSSetShader(geometryShader, NULL, 0); |
| 1216 | } |
| 1217 | else |
| 1218 | { |
| 1219 | mDeviceContext->GSSetShader(NULL, NULL, 0); |
| 1220 | } |
| 1221 | |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 1222 | mIsGeometryShaderActive = usesGeometryShader; |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1223 | } |
daniel@transgaming.com | 5fbf177 | 2012-11-28 20:54:43 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1226 | void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray) |
daniel@transgaming.com | ab1c146 | 2012-12-20 21:08:30 +0000 | [diff] [blame] | 1227 | { |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1228 | ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable()); |
| 1229 | ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable()); |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1230 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1231 | unsigned int totalRegisterCountVS = 0; |
| 1232 | unsigned int totalRegisterCountPS = 0; |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1233 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1234 | bool vertexUniformsDirty = false; |
| 1235 | bool pixelUniformsDirty = false; |
| 1236 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1237 | for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++) |
| 1238 | { |
| 1239 | const gl::Uniform *uniform = *uniform_iterator; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1240 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1241 | if (uniform->vsRegisterIndex >= 0) |
| 1242 | { |
| 1243 | totalRegisterCountVS += uniform->registerCount; |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1244 | vertexUniformsDirty = vertexUniformsDirty || uniform->dirty; |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1245 | } |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1246 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1247 | if (uniform->psRegisterIndex >= 0) |
| 1248 | { |
| 1249 | totalRegisterCountPS += uniform->registerCount; |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1250 | pixelUniformsDirty = pixelUniformsDirty || uniform->dirty; |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1251 | } |
| 1252 | } |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1253 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1254 | ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS); |
| 1255 | ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS); |
| 1256 | |
shannon.woods@transgaming.com | 13979a6 | 2013-02-28 23:10:18 +0000 | [diff] [blame^] | 1257 | float (*mapVS)[4] = (totalRegisterCountVS > 0 && vertexUniformsDirty) ? new float[totalRegisterCountVS][4] : NULL; |
| 1258 | float (*mapPS)[4] = (totalRegisterCountPS > 0 && pixelUniformsDirty) ? new float[totalRegisterCountPS][4] : NULL; |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1259 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1260 | for (gl::UniformArray::iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++) |
daniel@transgaming.com | ab1c146 | 2012-12-20 21:08:30 +0000 | [diff] [blame] | 1261 | { |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1262 | gl::Uniform *uniform = *uniform_iterator; |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1263 | |
shannon.woods@transgaming.com | 13979a6 | 2013-02-28 23:10:18 +0000 | [diff] [blame^] | 1264 | if (uniform->type != GL_SAMPLER_2D && uniform->type != GL_SAMPLER_CUBE) |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1265 | { |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1266 | if (uniform->vsRegisterIndex >= 0 && mapVS) |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1267 | { |
shannon.woods@transgaming.com | 13979a6 | 2013-02-28 23:10:18 +0000 | [diff] [blame^] | 1268 | memcpy(mapVS + uniform->vsRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4])); |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1269 | } |
shannon.woods@transgaming.com | 13979a6 | 2013-02-28 23:10:18 +0000 | [diff] [blame^] | 1270 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1271 | if (uniform->psRegisterIndex >= 0 && mapPS) |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1272 | { |
shannon.woods@transgaming.com | 13979a6 | 2013-02-28 23:10:18 +0000 | [diff] [blame^] | 1273 | memcpy(mapPS + uniform->psRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4])); |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1274 | } |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1275 | } |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1276 | |
| 1277 | uniform->dirty = false; |
daniel@transgaming.com | ab1c146 | 2012-12-20 21:08:30 +0000 | [diff] [blame] | 1278 | } |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1279 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1280 | if (vertexConstantBuffer && mapVS) |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1281 | { |
| 1282 | mDeviceContext->UpdateSubresource(vertexConstantBuffer, 0, NULL, mapVS, 0, 0); |
| 1283 | } |
| 1284 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1285 | if (pixelConstantBuffer && mapPS) |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1286 | { |
| 1287 | mDeviceContext->UpdateSubresource(pixelConstantBuffer, 0, NULL, mapPS, 0, 0); |
| 1288 | } |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1289 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1290 | mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer); |
| 1291 | mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer); |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 1292 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1293 | delete[] mapVS; |
| 1294 | delete[] mapPS; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1295 | |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 1296 | // Driver uniforms |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1297 | if (!mDriverConstantBufferVS) |
| 1298 | { |
| 1299 | D3D11_BUFFER_DESC constantBufferDescription = {0}; |
| 1300 | constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants); |
| 1301 | constantBufferDescription.Usage = D3D11_USAGE_DEFAULT; |
| 1302 | constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER; |
| 1303 | constantBufferDescription.CPUAccessFlags = 0; |
| 1304 | constantBufferDescription.MiscFlags = 0; |
| 1305 | constantBufferDescription.StructureByteStride = 0; |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 1306 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1307 | HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS); |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1308 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1309 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1310 | mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS); |
| 1311 | } |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1312 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1313 | if (!mDriverConstantBufferPS) |
| 1314 | { |
| 1315 | D3D11_BUFFER_DESC constantBufferDescription = {0}; |
| 1316 | constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants); |
| 1317 | constantBufferDescription.Usage = D3D11_USAGE_DEFAULT; |
| 1318 | constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER; |
| 1319 | constantBufferDescription.CPUAccessFlags = 0; |
| 1320 | constantBufferDescription.MiscFlags = 0; |
| 1321 | constantBufferDescription.StructureByteStride = 0; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1322 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1323 | HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS); |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1324 | ASSERT(SUCCEEDED(result)); |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1325 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1326 | mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS); |
| 1327 | } |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1328 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1329 | if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0) |
| 1330 | { |
| 1331 | mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0); |
| 1332 | memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants)); |
| 1333 | } |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1334 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1335 | if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0) |
| 1336 | { |
| 1337 | mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0); |
| 1338 | memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants)); |
| 1339 | } |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 1340 | |
| 1341 | // needed for the point sprite geometry shader |
| 1342 | mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS); |
daniel@transgaming.com | ab1c146 | 2012-12-20 21:08:30 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
daniel@transgaming.com | 084a257 | 2012-11-28 20:55:17 +0000 | [diff] [blame] | 1345 | void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer) |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 1346 | { |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1347 | bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha; |
| 1348 | bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) && |
| 1349 | !(clearParams.colorMaskRed && clearParams.colorMaskGreen && |
| 1350 | clearParams.colorMaskBlue && alphaUnmasked); |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1351 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1352 | unsigned int stencilUnmasked = 0x0; |
| 1353 | if (frameBuffer->hasStencil()) |
| 1354 | { |
| 1355 | unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat()); |
| 1356 | stencilUnmasked = (0x1 << stencilSize) - 1; |
| 1357 | } |
| 1358 | bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) && |
| 1359 | (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked; |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1360 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1361 | bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 || |
| 1362 | mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width || |
| 1363 | mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height); |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1364 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1365 | if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear) |
| 1366 | { |
shannon.woods@transgaming.com | 34f507c | 2013-01-25 21:52:15 +0000 | [diff] [blame] | 1367 | maskedClear(clearParams); |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1368 | } |
| 1369 | else |
| 1370 | { |
| 1371 | if (clearParams.mask & GL_COLOR_BUFFER_BIT) |
| 1372 | { |
| 1373 | gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer(); |
| 1374 | if (renderbufferObject) |
| 1375 | { |
| 1376 | RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget()); |
| 1377 | if (!renderTarget) |
| 1378 | { |
| 1379 | ERR("render target pointer unexpectedly null."); |
| 1380 | return; |
| 1381 | } |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1382 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1383 | ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView(); |
| 1384 | if (!framebufferRTV) |
| 1385 | { |
| 1386 | ERR("render target view pointer unexpectedly null."); |
| 1387 | return; |
| 1388 | } |
| 1389 | |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1390 | const float clearValues[4] = { clearParams.colorClearValue.red, |
| 1391 | clearParams.colorClearValue.green, |
| 1392 | clearParams.colorClearValue.blue, |
| 1393 | clearParams.colorClearValue.alpha }; |
| 1394 | mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues); |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1395 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1396 | framebufferRTV->Release(); |
| 1397 | } |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1398 | } |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1399 | if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT) |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1400 | { |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1401 | gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer(); |
| 1402 | if (renderbufferObject) |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1403 | { |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1404 | RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil()); |
| 1405 | if (!renderTarget) |
| 1406 | { |
| 1407 | ERR("render target pointer unexpectedly null."); |
| 1408 | return; |
| 1409 | } |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1410 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1411 | ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView(); |
| 1412 | if (!framebufferDSV) |
| 1413 | { |
| 1414 | ERR("depth stencil view pointer unexpectedly null."); |
| 1415 | return; |
| 1416 | } |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1417 | |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1418 | UINT clearFlags = 0; |
| 1419 | if (clearParams.mask & GL_DEPTH_BUFFER_BIT) |
| 1420 | { |
| 1421 | clearFlags |= D3D11_CLEAR_DEPTH; |
| 1422 | } |
| 1423 | if (clearParams.mask & GL_STENCIL_BUFFER_BIT) |
| 1424 | { |
| 1425 | clearFlags |= D3D11_CLEAR_STENCIL; |
| 1426 | } |
| 1427 | |
| 1428 | float depthClear = clearParams.depthClearValue; |
| 1429 | UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF; |
| 1430 | |
| 1431 | mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear); |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1432 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1433 | framebufferDSV->Release(); |
| 1434 | } |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1435 | } |
| 1436 | } |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
shannon.woods@transgaming.com | 34f507c | 2013-01-25 21:52:15 +0000 | [diff] [blame] | 1439 | void Renderer11::maskedClear(const gl::ClearParameters &clearParams) |
| 1440 | { |
| 1441 | HRESULT result; |
| 1442 | |
| 1443 | if (!mClearResourcesInitialized) |
| 1444 | { |
| 1445 | ASSERT(!mClearVB && !mClearVS && !mClearPS && !mClearScissorRS && !mClearNoScissorRS); |
| 1446 | |
| 1447 | D3D11_BUFFER_DESC vbDesc; |
| 1448 | vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4; |
| 1449 | vbDesc.Usage = D3D11_USAGE_DYNAMIC; |
| 1450 | vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; |
| 1451 | vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| 1452 | vbDesc.MiscFlags = 0; |
| 1453 | vbDesc.StructureByteStride = 0; |
| 1454 | |
| 1455 | result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB); |
| 1456 | ASSERT(SUCCEEDED(result)); |
| 1457 | d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer"); |
| 1458 | |
| 1459 | D3D11_INPUT_ELEMENT_DESC quadLayout[] = |
| 1460 | { |
| 1461 | { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 1462 | { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 1463 | }; |
| 1464 | |
| 1465 | result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL); |
| 1466 | ASSERT(SUCCEEDED(result)); |
| 1467 | d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout"); |
| 1468 | |
| 1469 | result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS); |
| 1470 | ASSERT(SUCCEEDED(result)); |
| 1471 | d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader"); |
| 1472 | |
| 1473 | result = mDevice->CreatePixelShader(g_PS_Clear, sizeof(g_PS_Clear), NULL, &mClearPS); |
| 1474 | ASSERT(SUCCEEDED(result)); |
| 1475 | d3d11::SetDebugName(mClearPS, "Renderer11 masked clear pixel shader"); |
| 1476 | |
| 1477 | D3D11_RASTERIZER_DESC rsScissorDesc; |
| 1478 | rsScissorDesc.FillMode = D3D11_FILL_SOLID; |
| 1479 | rsScissorDesc.CullMode = D3D11_CULL_NONE; |
| 1480 | rsScissorDesc.FrontCounterClockwise = FALSE; |
| 1481 | rsScissorDesc.DepthBias = 0; |
| 1482 | rsScissorDesc.DepthBiasClamp = 0.0f; |
| 1483 | rsScissorDesc.SlopeScaledDepthBias = 0.0f; |
| 1484 | rsScissorDesc.DepthClipEnable = FALSE; |
| 1485 | rsScissorDesc.ScissorEnable = TRUE; |
| 1486 | rsScissorDesc.MultisampleEnable = FALSE; |
| 1487 | rsScissorDesc.AntialiasedLineEnable = FALSE; |
| 1488 | |
| 1489 | result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS); |
| 1490 | ASSERT(SUCCEEDED(result)); |
| 1491 | d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state"); |
| 1492 | |
| 1493 | D3D11_RASTERIZER_DESC rsNoScissorDesc; |
| 1494 | rsNoScissorDesc.FillMode = D3D11_FILL_SOLID; |
| 1495 | rsNoScissorDesc.CullMode = D3D11_CULL_NONE; |
| 1496 | rsNoScissorDesc.FrontCounterClockwise = FALSE; |
| 1497 | rsNoScissorDesc.DepthBias = 0; |
| 1498 | rsNoScissorDesc.DepthBiasClamp = 0.0f; |
| 1499 | rsNoScissorDesc.SlopeScaledDepthBias = 0.0f; |
| 1500 | rsNoScissorDesc.DepthClipEnable = FALSE; |
| 1501 | rsNoScissorDesc.ScissorEnable = FALSE; |
| 1502 | rsNoScissorDesc.MultisampleEnable = FALSE; |
| 1503 | rsNoScissorDesc.AntialiasedLineEnable = FALSE; |
| 1504 | |
| 1505 | result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS); |
| 1506 | ASSERT(SUCCEEDED(result)); |
| 1507 | d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state"); |
| 1508 | |
| 1509 | mClearResourcesInitialized = true; |
| 1510 | } |
| 1511 | |
| 1512 | // Prepare the depth stencil state to write depth values if the depth should be cleared |
| 1513 | // and stencil values if the stencil should be cleared |
| 1514 | gl::DepthStencilState glDSState; |
| 1515 | glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0; |
| 1516 | glDSState.depthFunc = GL_ALWAYS; |
| 1517 | glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0; |
| 1518 | glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0; |
| 1519 | glDSState.stencilFunc = GL_ALWAYS; |
| 1520 | glDSState.stencilMask = 0; |
| 1521 | glDSState.stencilFail = GL_REPLACE; |
| 1522 | glDSState.stencilPassDepthFail = GL_REPLACE; |
| 1523 | glDSState.stencilPassDepthPass = GL_REPLACE; |
| 1524 | glDSState.stencilWritemask = clearParams.stencilWriteMask; |
| 1525 | glDSState.stencilBackFunc = GL_ALWAYS; |
| 1526 | glDSState.stencilBackMask = 0; |
| 1527 | glDSState.stencilBackFail = GL_REPLACE; |
| 1528 | glDSState.stencilBackPassDepthFail = GL_REPLACE; |
| 1529 | glDSState.stencilBackPassDepthPass = GL_REPLACE; |
| 1530 | glDSState.stencilBackWritemask = clearParams.stencilWriteMask; |
| 1531 | |
| 1532 | int stencilClear = clearParams.stencilClearValue & 0x000000FF; |
| 1533 | |
| 1534 | ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState); |
| 1535 | |
| 1536 | // Prepare the blend state to use a write mask if the color buffer should be cleared |
| 1537 | gl::BlendState glBlendState; |
| 1538 | glBlendState.blend = false; |
| 1539 | glBlendState.sourceBlendRGB = GL_ONE; |
| 1540 | glBlendState.destBlendRGB = GL_ZERO; |
| 1541 | glBlendState.sourceBlendAlpha = GL_ONE; |
| 1542 | glBlendState.destBlendAlpha = GL_ZERO; |
| 1543 | glBlendState.blendEquationRGB = GL_FUNC_ADD; |
| 1544 | glBlendState.blendEquationAlpha = GL_FUNC_ADD; |
| 1545 | glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false; |
| 1546 | glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false; |
| 1547 | glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false; |
| 1548 | glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false; |
| 1549 | glBlendState.sampleAlphaToCoverage = false; |
| 1550 | glBlendState.dither = false; |
| 1551 | |
| 1552 | static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; |
| 1553 | static const UINT sampleMask = 0xFFFFFFFF; |
| 1554 | |
| 1555 | ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState); |
| 1556 | |
| 1557 | // Set the vertices |
| 1558 | D3D11_MAPPED_SUBRESOURCE mappedResource; |
| 1559 | result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); |
| 1560 | if (FAILED(result)) |
| 1561 | { |
| 1562 | ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result); |
| 1563 | return; |
| 1564 | } |
| 1565 | |
| 1566 | d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData); |
| 1567 | |
| 1568 | float depthClear = gl::clamp01(clearParams.depthClearValue); |
| 1569 | d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue); |
| 1570 | d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue); |
| 1571 | d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue); |
| 1572 | d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue); |
| 1573 | |
| 1574 | mDeviceContext->Unmap(mClearVB, 0); |
| 1575 | |
| 1576 | // Apply state |
| 1577 | mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask); |
| 1578 | mDeviceContext->OMSetDepthStencilState(dsState, stencilClear); |
| 1579 | mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS); |
| 1580 | |
| 1581 | // Apply shaders |
| 1582 | mDeviceContext->IASetInputLayout(mClearIL); |
| 1583 | mDeviceContext->VSSetShader(mClearVS, NULL, 0); |
| 1584 | mDeviceContext->PSSetShader(mClearPS, NULL, 0); |
| 1585 | |
| 1586 | // Apply vertex buffer |
| 1587 | static UINT stride = sizeof(d3d11::PositionDepthColorVertex); |
| 1588 | static UINT startIdx = 0; |
| 1589 | mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx); |
| 1590 | mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); |
| 1591 | |
| 1592 | // Draw the clear quad |
| 1593 | mDeviceContext->Draw(4, 0); |
| 1594 | |
| 1595 | // Clean up |
| 1596 | markAllStateDirty(); |
| 1597 | } |
| 1598 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 1599 | void Renderer11::markAllStateDirty() |
| 1600 | { |
daniel@transgaming.com | 9a06737 | 2012-12-20 20:55:24 +0000 | [diff] [blame] | 1601 | mAppliedRenderTargetSerial = 0; |
| 1602 | mAppliedDepthbufferSerial = 0; |
| 1603 | mAppliedStencilbufferSerial = 0; |
daniel@transgaming.com | 7b6b83e | 2012-11-28 21:00:30 +0000 | [diff] [blame] | 1604 | mDepthStencilInitialized = false; |
| 1605 | mRenderTargetDescInitialized = false; |
| 1606 | |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 1607 | for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++) |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 1608 | { |
| 1609 | mForceSetVertexSamplerStates[i] = true; |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 1610 | mCurVertexTextureSerials[i] = 0; |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 1611 | } |
| 1612 | for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++) |
| 1613 | { |
| 1614 | mForceSetPixelSamplerStates[i] = true; |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 1615 | mCurPixelTextureSerials[i] = 0; |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 1618 | mForceSetBlendState = true; |
| 1619 | mForceSetRasterState = true; |
| 1620 | mForceSetDepthStencilState = true; |
| 1621 | mForceSetScissor = true; |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 1622 | mForceSetViewport = true; |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1623 | |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 1624 | mAppliedIBSerial = 0; |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 1625 | mAppliedStorageIBSerial = 0; |
daniel@transgaming.com | 7fbf486 | 2013-01-11 04:09:22 +0000 | [diff] [blame] | 1626 | mAppliedIBOffset = 0; |
| 1627 | |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1628 | mAppliedProgramBinarySerial = 0; |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1629 | memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants)); |
| 1630 | memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants)); |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1633 | void Renderer11::releaseDeviceResources() |
| 1634 | { |
daniel@transgaming.com | f8ba109 | 2012-11-28 19:37:53 +0000 | [diff] [blame] | 1635 | mStateCache.clear(); |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 1636 | mInputLayoutCache.clear(); |
| 1637 | |
| 1638 | delete mVertexDataManager; |
| 1639 | mVertexDataManager = NULL; |
| 1640 | |
| 1641 | delete mIndexDataManager; |
| 1642 | mIndexDataManager = NULL; |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1643 | |
| 1644 | delete mLineLoopIB; |
| 1645 | mLineLoopIB = NULL; |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1646 | |
| 1647 | delete mTriangleFanIB; |
| 1648 | mTriangleFanIB = NULL; |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 1649 | |
| 1650 | if (mCopyVB) |
| 1651 | { |
| 1652 | mCopyVB->Release(); |
| 1653 | mCopyVB = NULL; |
| 1654 | } |
| 1655 | |
| 1656 | if (mCopySampler) |
| 1657 | { |
| 1658 | mCopySampler->Release(); |
| 1659 | mCopySampler = NULL; |
| 1660 | } |
| 1661 | |
| 1662 | if (mCopyIL) |
| 1663 | { |
| 1664 | mCopyIL->Release(); |
| 1665 | mCopyIL = NULL; |
| 1666 | } |
| 1667 | |
| 1668 | if (mCopyVS) |
| 1669 | { |
| 1670 | mCopyVS->Release(); |
| 1671 | mCopyVS = NULL; |
| 1672 | } |
| 1673 | |
| 1674 | if (mCopyRGBAPS) |
| 1675 | { |
| 1676 | mCopyRGBAPS->Release(); |
| 1677 | mCopyRGBAPS = NULL; |
| 1678 | } |
| 1679 | |
| 1680 | if (mCopyRGBPS) |
| 1681 | { |
| 1682 | mCopyRGBPS->Release(); |
| 1683 | mCopyRGBPS = NULL; |
| 1684 | } |
| 1685 | |
| 1686 | if (mCopyLumPS) |
| 1687 | { |
| 1688 | mCopyLumPS->Release(); |
| 1689 | mCopyLumPS = NULL; |
| 1690 | } |
| 1691 | |
| 1692 | if (mCopyLumAlphaPS) |
| 1693 | { |
| 1694 | mCopyLumAlphaPS->Release(); |
| 1695 | mCopyLumAlphaPS = NULL; |
| 1696 | } |
| 1697 | |
| 1698 | mCopyResourcesInitialized = false; |
shannon.woods@transgaming.com | 34f507c | 2013-01-25 21:52:15 +0000 | [diff] [blame] | 1699 | |
| 1700 | if (mClearVB) |
| 1701 | { |
| 1702 | mClearVB->Release(); |
| 1703 | mClearVB = NULL; |
| 1704 | } |
| 1705 | |
| 1706 | if (mClearIL) |
| 1707 | { |
| 1708 | mClearIL->Release(); |
| 1709 | mClearIL = NULL; |
| 1710 | } |
| 1711 | |
| 1712 | if (mClearVS) |
| 1713 | { |
| 1714 | mClearVS->Release(); |
| 1715 | mClearVS = NULL; |
| 1716 | } |
| 1717 | |
| 1718 | if (mClearPS) |
| 1719 | { |
| 1720 | mClearPS->Release(); |
| 1721 | mClearPS = NULL; |
| 1722 | } |
| 1723 | |
| 1724 | if (mClearScissorRS) |
| 1725 | { |
| 1726 | mClearScissorRS->Release(); |
| 1727 | mClearScissorRS = NULL; |
| 1728 | } |
| 1729 | |
| 1730 | if (mClearNoScissorRS) |
| 1731 | { |
| 1732 | mClearNoScissorRS->Release(); |
| 1733 | mClearNoScissorRS = NULL; |
| 1734 | } |
| 1735 | |
| 1736 | mClearResourcesInitialized = false; |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1737 | |
| 1738 | if (mDriverConstantBufferVS) |
| 1739 | { |
| 1740 | mDriverConstantBufferVS->Release(); |
| 1741 | mDriverConstantBufferVS = NULL; |
| 1742 | } |
| 1743 | |
| 1744 | if (mDriverConstantBufferPS) |
| 1745 | { |
| 1746 | mDriverConstantBufferPS->Release(); |
| 1747 | mDriverConstantBufferPS = NULL; |
| 1748 | } |
daniel@transgaming.com | bdf787f | 2013-02-01 03:20:36 +0000 | [diff] [blame] | 1749 | |
| 1750 | if (mSyncQuery) |
| 1751 | { |
| 1752 | mSyncQuery->Release(); |
| 1753 | mSyncQuery = NULL; |
| 1754 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 1757 | void Renderer11::notifyDeviceLost() |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1758 | { |
| 1759 | mDeviceLost = true; |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 1760 | mDisplay->notifyDeviceLost(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | bool Renderer11::isDeviceLost() |
| 1764 | { |
| 1765 | return mDeviceLost; |
| 1766 | } |
| 1767 | |
| 1768 | // set notify to true to broadcast a message to all contexts of the device loss |
| 1769 | bool Renderer11::testDeviceLost(bool notify) |
| 1770 | { |
| 1771 | bool isLost = false; |
| 1772 | |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1773 | // GetRemovedReason is used to test if the device is removed |
| 1774 | HRESULT result = mDevice->GetDeviceRemovedReason(); |
| 1775 | isLost = d3d11::isDeviceLostError(result); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1776 | |
| 1777 | if (isLost) |
| 1778 | { |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1779 | // Log error if this is a new device lost event |
| 1780 | if (mDeviceLost == false) |
| 1781 | { |
| 1782 | ERR("The D3D11 device was removed: 0x%08X", result); |
| 1783 | } |
| 1784 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1785 | // ensure we note the device loss -- |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1786 | // we'll probably get this done again by notifyDeviceLost |
| 1787 | // but best to remember it! |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1788 | // Note that we don't want to clear the device loss status here |
| 1789 | // -- this needs to be done by resetDevice |
| 1790 | mDeviceLost = true; |
| 1791 | if (notify) |
| 1792 | { |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 1793 | notifyDeviceLost(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | return isLost; |
| 1798 | } |
| 1799 | |
| 1800 | bool Renderer11::testDeviceResettable() |
| 1801 | { |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1802 | // determine if the device is resettable by creating a dummy device |
| 1803 | PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice"); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1804 | |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1805 | if (D3D11CreateDevice == NULL) |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1806 | { |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1807 | return false; |
| 1808 | } |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1809 | |
| 1810 | D3D_FEATURE_LEVEL featureLevel[] = |
| 1811 | { |
| 1812 | D3D_FEATURE_LEVEL_11_0, |
| 1813 | D3D_FEATURE_LEVEL_10_1, |
| 1814 | D3D_FEATURE_LEVEL_10_0, |
| 1815 | }; |
| 1816 | |
| 1817 | ID3D11Device* dummyDevice; |
| 1818 | D3D_FEATURE_LEVEL dummyFeatureLevel; |
| 1819 | ID3D11DeviceContext* dummyContext; |
| 1820 | |
| 1821 | HRESULT result = D3D11CreateDevice(NULL, |
| 1822 | D3D_DRIVER_TYPE_HARDWARE, |
| 1823 | NULL, |
| 1824 | #if defined(_DEBUG) |
| 1825 | D3D11_CREATE_DEVICE_DEBUG, |
| 1826 | #else |
| 1827 | 0, |
| 1828 | #endif |
| 1829 | featureLevel, |
| 1830 | sizeof(featureLevel)/sizeof(featureLevel[0]), |
| 1831 | D3D11_SDK_VERSION, |
| 1832 | &dummyDevice, |
| 1833 | &dummyFeatureLevel, |
| 1834 | &dummyContext); |
| 1835 | |
| 1836 | if (!mDevice || FAILED(result)) |
| 1837 | { |
| 1838 | return false; |
| 1839 | } |
| 1840 | |
| 1841 | dummyContext->Release(); |
| 1842 | dummyDevice->Release(); |
| 1843 | |
| 1844 | return true; |
| 1845 | } |
| 1846 | |
| 1847 | void Renderer11::release() |
| 1848 | { |
| 1849 | releaseDeviceResources(); |
| 1850 | |
| 1851 | if (mDxgiFactory) |
| 1852 | { |
| 1853 | mDxgiFactory->Release(); |
| 1854 | mDxgiFactory = NULL; |
| 1855 | } |
| 1856 | |
| 1857 | if (mDxgiAdapter) |
| 1858 | { |
| 1859 | mDxgiAdapter->Release(); |
| 1860 | mDxgiAdapter = NULL; |
| 1861 | } |
| 1862 | |
| 1863 | if (mDeviceContext) |
| 1864 | { |
| 1865 | mDeviceContext->ClearState(); |
| 1866 | mDeviceContext->Flush(); |
| 1867 | mDeviceContext->Release(); |
| 1868 | mDeviceContext = NULL; |
| 1869 | } |
| 1870 | |
| 1871 | if (mDevice) |
| 1872 | { |
| 1873 | mDevice->Release(); |
| 1874 | mDevice = NULL; |
| 1875 | } |
| 1876 | |
| 1877 | if (mD3d11Module) |
| 1878 | { |
| 1879 | FreeLibrary(mD3d11Module); |
| 1880 | mD3d11Module = NULL; |
| 1881 | } |
| 1882 | |
| 1883 | if (mDxgiModule) |
| 1884 | { |
| 1885 | FreeLibrary(mDxgiModule); |
| 1886 | mDxgiModule = NULL; |
| 1887 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | bool Renderer11::resetDevice() |
| 1891 | { |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1892 | // recreate everything |
| 1893 | release(); |
| 1894 | EGLint result = initialize(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1895 | |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1896 | if (result != EGL_SUCCESS) |
| 1897 | { |
| 1898 | ERR("Could not reinitialize D3D11 device: %08X", result); |
| 1899 | return false; |
| 1900 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1901 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1902 | mDeviceLost = false; |
| 1903 | |
| 1904 | return true; |
| 1905 | } |
| 1906 | |
| 1907 | DWORD Renderer11::getAdapterVendor() const |
| 1908 | { |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 1909 | return mAdapterDescription.VendorId; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
daniel@transgaming.com | ca1ac1f | 2013-01-11 04:13:05 +0000 | [diff] [blame] | 1912 | std::string Renderer11::getRendererDescription() const |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1913 | { |
daniel@transgaming.com | ca1ac1f | 2013-01-11 04:13:05 +0000 | [diff] [blame] | 1914 | std::ostringstream rendererString; |
| 1915 | |
| 1916 | rendererString << mDescription; |
| 1917 | rendererString << " Direct3D11"; |
| 1918 | |
| 1919 | rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel(); |
| 1920 | rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel(); |
| 1921 | |
| 1922 | return rendererString.str(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | GUID Renderer11::getAdapterIdentifier() const |
| 1926 | { |
shannon.woods@transgaming.com | 43db795 | 2013-02-28 23:04:28 +0000 | [diff] [blame] | 1927 | // Use the adapter LUID as our adapter ID |
| 1928 | // This number is local to a machine is only guaranteed to be unique between restarts |
| 1929 | META_ASSERT(sizeof(LUID) <= sizeof(GUID)); |
| 1930 | GUID adapterId = {0}; |
| 1931 | memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID)); |
| 1932 | return adapterId; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
shannon.woods@transgaming.com | bec04bf | 2013-01-25 21:53:52 +0000 | [diff] [blame] | 1935 | bool Renderer11::getBGRATextureSupport() const |
| 1936 | { |
| 1937 | return mBGRATextureSupport; |
| 1938 | } |
| 1939 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1940 | bool Renderer11::getDXT1TextureSupport() |
| 1941 | { |
| 1942 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 1943 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1944 | return false; |
| 1945 | } |
| 1946 | |
| 1947 | bool Renderer11::getDXT3TextureSupport() |
| 1948 | { |
| 1949 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 1950 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1951 | return false; |
| 1952 | } |
| 1953 | |
| 1954 | bool Renderer11::getDXT5TextureSupport() |
| 1955 | { |
| 1956 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 1957 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1958 | return false; |
| 1959 | } |
| 1960 | |
| 1961 | bool Renderer11::getDepthTextureSupport() const |
| 1962 | { |
| 1963 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 1964 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1965 | return false; |
| 1966 | } |
| 1967 | |
| 1968 | bool Renderer11::getFloat32TextureSupport(bool *filtering, bool *renderable) |
| 1969 | { |
shannon.woods@transgaming.com | 9cdced6 | 2013-02-28 23:07:31 +0000 | [diff] [blame] | 1970 | *renderable = mFloat32RenderSupport; |
| 1971 | *filtering = mFloat32FilterSupport; |
| 1972 | return mFloat32TextureSupport; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | bool Renderer11::getFloat16TextureSupport(bool *filtering, bool *renderable) |
| 1976 | { |
shannon.woods@transgaming.com | 9cdced6 | 2013-02-28 23:07:31 +0000 | [diff] [blame] | 1977 | *renderable = mFloat16RenderSupport; |
| 1978 | *filtering = mFloat16FilterSupport; |
| 1979 | return mFloat16TextureSupport; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | bool Renderer11::getLuminanceTextureSupport() |
| 1983 | { |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1984 | return false; |
| 1985 | } |
| 1986 | |
| 1987 | bool Renderer11::getLuminanceAlphaTextureSupport() |
| 1988 | { |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1989 | return false; |
| 1990 | } |
| 1991 | |
| 1992 | bool Renderer11::getTextureFilterAnisotropySupport() const |
| 1993 | { |
shannon.woods@transgaming.com | 1abd797 | 2013-02-28 23:06:58 +0000 | [diff] [blame] | 1994 | return true; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | float Renderer11::getTextureMaxAnisotropy() const |
| 1998 | { |
shannon.woods@transgaming.com | 1abd797 | 2013-02-28 23:06:58 +0000 | [diff] [blame] | 1999 | switch (mFeatureLevel) |
| 2000 | { |
| 2001 | case D3D_FEATURE_LEVEL_11_0: |
| 2002 | return D3D11_MAX_MAXANISOTROPY; |
| 2003 | case D3D_FEATURE_LEVEL_10_1: |
| 2004 | case D3D_FEATURE_LEVEL_10_0: |
| 2005 | return D3D10_MAX_MAXANISOTROPY; |
| 2006 | default: UNREACHABLE(); |
| 2007 | return 0; |
| 2008 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | bool Renderer11::getEventQuerySupport() |
| 2012 | { |
shannon.woods@transgaming.com | be58aa0 | 2013-02-28 23:03:55 +0000 | [diff] [blame] | 2013 | return true; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 2016 | Range Renderer11::getViewportBounds() const |
| 2017 | { |
| 2018 | switch (mFeatureLevel) |
| 2019 | { |
| 2020 | case D3D_FEATURE_LEVEL_11_0: |
| 2021 | return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX); |
| 2022 | case D3D_FEATURE_LEVEL_10_1: |
| 2023 | case D3D_FEATURE_LEVEL_10_0: |
| 2024 | return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX); |
| 2025 | default: UNREACHABLE(); |
| 2026 | return Range(0, 0); |
| 2027 | } |
| 2028 | } |
| 2029 | |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 2030 | unsigned int Renderer11::getMaxVertexTextureImageUnits() const |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2031 | { |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 2032 | META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS); |
| 2033 | switch (mFeatureLevel) |
| 2034 | { |
| 2035 | case D3D_FEATURE_LEVEL_11_0: |
| 2036 | case D3D_FEATURE_LEVEL_10_1: |
| 2037 | case D3D_FEATURE_LEVEL_10_0: |
| 2038 | return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4; |
| 2039 | default: UNREACHABLE(); |
| 2040 | return 0; |
| 2041 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
shannon.woods@transgaming.com | 76cd88c | 2013-01-25 21:54:36 +0000 | [diff] [blame] | 2044 | unsigned int Renderer11::getMaxCombinedTextureImageUnits() const |
| 2045 | { |
| 2046 | return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits(); |
| 2047 | } |
| 2048 | |
shannon.woods@transgaming.com | 254317d | 2013-01-25 21:54:09 +0000 | [diff] [blame] | 2049 | int Renderer11::getMaxVertexUniformVectors() const |
| 2050 | { |
shannon.woods@transgaming.com | 4e48204 | 2013-01-25 21:54:18 +0000 | [diff] [blame] | 2051 | META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT); |
| 2052 | ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0); |
| 2053 | return MAX_VERTEX_UNIFORM_VECTORS_D3D11; |
shannon.woods@transgaming.com | 254317d | 2013-01-25 21:54:09 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | int Renderer11::getMaxFragmentUniformVectors() const |
| 2057 | { |
shannon.woods@transgaming.com | 4e48204 | 2013-01-25 21:54:18 +0000 | [diff] [blame] | 2058 | META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT); |
| 2059 | ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0); |
| 2060 | return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11; |
shannon.woods@transgaming.com | 254317d | 2013-01-25 21:54:09 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
shannon.woods@transgaming.com | 28d268e | 2013-01-25 21:54:26 +0000 | [diff] [blame] | 2063 | int Renderer11::getMaxVaryingVectors() const |
| 2064 | { |
| 2065 | META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT); |
| 2066 | switch (mFeatureLevel) |
| 2067 | { |
| 2068 | case D3D_FEATURE_LEVEL_11_0: |
| 2069 | return D3D11_VS_OUTPUT_REGISTER_COUNT; |
| 2070 | case D3D_FEATURE_LEVEL_10_1: |
| 2071 | case D3D_FEATURE_LEVEL_10_0: |
| 2072 | return D3D10_VS_OUTPUT_REGISTER_COUNT; |
| 2073 | default: UNREACHABLE(); |
| 2074 | return 0; |
| 2075 | } |
| 2076 | } |
| 2077 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2078 | bool Renderer11::getNonPower2TextureSupport() const |
| 2079 | { |
shannon.woods@transgaming.com | 03951cf | 2013-01-25 21:57:01 +0000 | [diff] [blame] | 2080 | switch (mFeatureLevel) |
| 2081 | { |
| 2082 | case D3D_FEATURE_LEVEL_11_0: |
| 2083 | case D3D_FEATURE_LEVEL_10_1: |
| 2084 | case D3D_FEATURE_LEVEL_10_0: |
| 2085 | return true; |
| 2086 | default: UNREACHABLE(); |
| 2087 | return false; |
| 2088 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | bool Renderer11::getOcclusionQuerySupport() const |
| 2092 | { |
shannon.woods@transgaming.com | 8b7606a | 2013-02-28 23:03:47 +0000 | [diff] [blame] | 2093 | switch (mFeatureLevel) |
| 2094 | { |
| 2095 | case D3D_FEATURE_LEVEL_11_0: |
| 2096 | case D3D_FEATURE_LEVEL_10_1: |
| 2097 | case D3D_FEATURE_LEVEL_10_0: |
| 2098 | return true; |
| 2099 | default: UNREACHABLE(); |
| 2100 | return false; |
| 2101 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | bool Renderer11::getInstancingSupport() const |
| 2105 | { |
daniel@transgaming.com | fe32464 | 2013-02-01 03:20:11 +0000 | [diff] [blame] | 2106 | switch (mFeatureLevel) |
| 2107 | { |
| 2108 | case D3D_FEATURE_LEVEL_11_0: |
| 2109 | case D3D_FEATURE_LEVEL_10_1: |
| 2110 | case D3D_FEATURE_LEVEL_10_0: |
| 2111 | return true; |
| 2112 | default: UNREACHABLE(); |
| 2113 | return false; |
| 2114 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | bool Renderer11::getShareHandleSupport() const |
| 2118 | { |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame] | 2119 | // We only currently support share handles with BGRA surfaces, because |
| 2120 | // chrome needs BGRA. Once chrome fixes this, we should always support them. |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2121 | // PIX doesn't seem to support using share handles, so disable them. |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame] | 2122 | return getBGRATextureSupport() && !gl::perfActive(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
daniel@transgaming.com | 7629bb6 | 2013-01-11 04:12:28 +0000 | [diff] [blame] | 2125 | bool Renderer11::getDerivativeInstructionSupport() const |
| 2126 | { |
shannon.woods@transgaming.com | 09fd945 | 2013-02-28 23:02:28 +0000 | [diff] [blame] | 2127 | switch (mFeatureLevel) |
| 2128 | { |
| 2129 | case D3D_FEATURE_LEVEL_11_0: |
| 2130 | case D3D_FEATURE_LEVEL_10_1: |
| 2131 | case D3D_FEATURE_LEVEL_10_0: |
| 2132 | return true; |
| 2133 | default: UNREACHABLE(); |
| 2134 | return false; |
| 2135 | } |
daniel@transgaming.com | 7629bb6 | 2013-01-11 04:12:28 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
shannon.woods@transgaming.com | 8d2f086 | 2013-02-28 23:09:19 +0000 | [diff] [blame] | 2138 | bool Renderer11::getPostSubBufferSupport() const |
| 2139 | { |
| 2140 | // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2. |
| 2141 | return false; |
| 2142 | } |
| 2143 | |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 2144 | int Renderer11::getMajorShaderModel() const |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2145 | { |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 2146 | switch (mFeatureLevel) |
| 2147 | { |
| 2148 | case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5 |
daniel@transgaming.com | ca1ac1f | 2013-01-11 04:13:05 +0000 | [diff] [blame] | 2149 | case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4 |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 2150 | case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4 |
| 2151 | default: UNREACHABLE(); return 0; |
| 2152 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
daniel@transgaming.com | ca1ac1f | 2013-01-11 04:13:05 +0000 | [diff] [blame] | 2155 | int Renderer11::getMinorShaderModel() const |
| 2156 | { |
| 2157 | switch (mFeatureLevel) |
| 2158 | { |
| 2159 | case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0 |
| 2160 | case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1 |
| 2161 | case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0 |
| 2162 | default: UNREACHABLE(); return 0; |
| 2163 | } |
| 2164 | } |
| 2165 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2166 | float Renderer11::getMaxPointSize() const |
| 2167 | { |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 2168 | // choose a reasonable maximum. we enforce this in the shader. |
| 2169 | // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size) |
| 2170 | return 1024.0f; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 2173 | int Renderer11::getMaxViewportDimension() const |
| 2174 | { |
| 2175 | // Clamp viewport width/height to half of the maximum right/bottom edge |
| 2176 | switch (mFeatureLevel) |
| 2177 | { |
| 2178 | case D3D_FEATURE_LEVEL_11_0: |
| 2179 | return D3D11_VIEWPORT_BOUNDS_MAX - D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384 |
| 2180 | case D3D_FEATURE_LEVEL_10_1: |
| 2181 | case D3D_FEATURE_LEVEL_10_0: |
| 2182 | return D3D10_VIEWPORT_BOUNDS_MAX - D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192 |
| 2183 | default: UNREACHABLE(); |
| 2184 | return 0; |
| 2185 | } |
| 2186 | } |
| 2187 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2188 | int Renderer11::getMaxTextureWidth() const |
| 2189 | { |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 2190 | switch (mFeatureLevel) |
| 2191 | { |
| 2192 | case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384 |
| 2193 | case D3D_FEATURE_LEVEL_10_1: |
| 2194 | case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192 |
| 2195 | default: UNREACHABLE(); return 0; |
| 2196 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | int Renderer11::getMaxTextureHeight() const |
| 2200 | { |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 2201 | switch (mFeatureLevel) |
| 2202 | { |
| 2203 | case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384 |
| 2204 | case D3D_FEATURE_LEVEL_10_1: |
| 2205 | case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192 |
| 2206 | default: UNREACHABLE(); return 0; |
| 2207 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | bool Renderer11::get32BitIndexSupport() const |
| 2211 | { |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 2212 | switch (mFeatureLevel) |
| 2213 | { |
| 2214 | case D3D_FEATURE_LEVEL_11_0: |
| 2215 | case D3D_FEATURE_LEVEL_10_1: |
| 2216 | case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true |
| 2217 | default: UNREACHABLE(); return false; |
| 2218 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | int Renderer11::getMinSwapInterval() const |
| 2222 | { |
daniel@transgaming.com | 8c7b1a9 | 2012-11-28 19:34:06 +0000 | [diff] [blame] | 2223 | return 0; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | int Renderer11::getMaxSwapInterval() const |
| 2227 | { |
daniel@transgaming.com | 8c7b1a9 | 2012-11-28 19:34:06 +0000 | [diff] [blame] | 2228 | return 4; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | int Renderer11::getMaxSupportedSamples() const |
| 2232 | { |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 2233 | return mMaxSupportedSamples; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
shannon.woods@transgaming.com | 88fbd0f | 2013-02-28 23:05:46 +0000 | [diff] [blame] | 2236 | int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const |
| 2237 | { |
| 2238 | if (requested == 0) |
| 2239 | { |
| 2240 | return 0; |
| 2241 | } |
| 2242 | |
| 2243 | MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format); |
| 2244 | if (iter != mMultisampleSupportMap.end()) |
| 2245 | { |
| 2246 | const MultisampleSupportInfo& info = iter->second; |
| 2247 | for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++) |
| 2248 | { |
| 2249 | if (info.qualityLevels[i] > 0) |
| 2250 | { |
| 2251 | return i + 1; |
| 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | return -1; |
| 2257 | } |
| 2258 | |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 2259 | bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source) |
daniel@transgaming.com | ad6aee7 | 2012-11-28 19:33:42 +0000 | [diff] [blame] | 2260 | { |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2261 | if (source && dest) |
| 2262 | { |
| 2263 | TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance()); |
| 2264 | TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance()); |
| 2265 | |
daniel@transgaming.com | e9cf5e7 | 2013-01-11 04:06:55 +0000 | [diff] [blame] | 2266 | mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture()); |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2267 | return true; |
| 2268 | } |
| 2269 | |
daniel@transgaming.com | ad6aee7 | 2012-11-28 19:33:42 +0000 | [diff] [blame] | 2270 | return false; |
| 2271 | } |
| 2272 | |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 2273 | bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source) |
daniel@transgaming.com | ad6aee7 | 2012-11-28 19:33:42 +0000 | [diff] [blame] | 2274 | { |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2275 | if (source && dest) |
| 2276 | { |
daniel@transgaming.com | e9cf5e7 | 2013-01-11 04:06:55 +0000 | [diff] [blame] | 2277 | TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance()); |
| 2278 | TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance()); |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2279 | |
daniel@transgaming.com | e9cf5e7 | 2013-01-11 04:06:55 +0000 | [diff] [blame] | 2280 | mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture()); |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2281 | return true; |
| 2282 | } |
| 2283 | |
daniel@transgaming.com | ad6aee7 | 2012-11-28 19:33:42 +0000 | [diff] [blame] | 2284 | return false; |
| 2285 | } |
| 2286 | |
shannon.woods@transgaming.com | 664916b | 2013-01-25 21:50:32 +0000 | [diff] [blame] | 2287 | bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 2288 | GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level) |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2289 | { |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2290 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 2291 | if (!colorbuffer) |
| 2292 | { |
| 2293 | ERR("Failed to retrieve the color buffer from the frame buffer."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2294 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); |
| 2298 | if (!sourceRenderTarget) |
| 2299 | { |
| 2300 | ERR("Failed to retrieve the render target from the frame buffer."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2301 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView(); |
| 2305 | if (!source) |
| 2306 | { |
| 2307 | ERR("Failed to retrieve the render target view from the render target."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2308 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
| 2311 | TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance()); |
| 2312 | if (!storage11) |
| 2313 | { |
| 2314 | source->Release(); |
| 2315 | ERR("Failed to retrieve the texture storage from the destination."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2316 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level)); |
| 2320 | if (!destRenderTarget) |
| 2321 | { |
| 2322 | source->Release(); |
| 2323 | ERR("Failed to retrieve the render target from the destination storage."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2324 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView(); |
| 2328 | if (!dest) |
| 2329 | { |
| 2330 | source->Release(); |
| 2331 | ERR("Failed to retrieve the render target view from the destination render target."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2332 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | gl::Rectangle destRect; |
| 2336 | destRect.x = xoffset; |
| 2337 | destRect.y = yoffset; |
| 2338 | destRect.width = sourceRect.width; |
| 2339 | destRect.height = sourceRect.height; |
| 2340 | |
| 2341 | bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), |
| 2342 | dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat); |
| 2343 | |
| 2344 | source->Release(); |
| 2345 | dest->Release(); |
| 2346 | |
| 2347 | return ret; |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
shannon.woods@transgaming.com | 664916b | 2013-01-25 21:50:32 +0000 | [diff] [blame] | 2350 | bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 2351 | GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level) |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2352 | { |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2353 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 2354 | if (!colorbuffer) |
| 2355 | { |
| 2356 | ERR("Failed to retrieve the color buffer from the frame buffer."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2357 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); |
| 2361 | if (!sourceRenderTarget) |
| 2362 | { |
| 2363 | ERR("Failed to retrieve the render target from the frame buffer."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2364 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView(); |
| 2368 | if (!source) |
| 2369 | { |
| 2370 | ERR("Failed to retrieve the render target view from the render target."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2371 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance()); |
| 2375 | if (!storage11) |
| 2376 | { |
| 2377 | source->Release(); |
| 2378 | ERR("Failed to retrieve the texture storage from the destination."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2379 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(target, level)); |
| 2383 | if (!destRenderTarget) |
| 2384 | { |
| 2385 | source->Release(); |
| 2386 | ERR("Failed to retrieve the render target from the destination storage."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2387 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView(); |
| 2391 | if (!dest) |
| 2392 | { |
| 2393 | source->Release(); |
| 2394 | ERR("Failed to retrieve the render target view from the destination render target."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2395 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | gl::Rectangle destRect; |
| 2399 | destRect.x = xoffset; |
| 2400 | destRect.y = yoffset; |
| 2401 | destRect.width = sourceRect.width; |
| 2402 | destRect.height = sourceRect.height; |
| 2403 | |
| 2404 | bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), |
| 2405 | dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat); |
| 2406 | |
| 2407 | source->Release(); |
| 2408 | dest->Release(); |
| 2409 | |
| 2410 | return ret; |
| 2411 | } |
| 2412 | |
| 2413 | bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight, |
| 2414 | ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat) |
| 2415 | { |
| 2416 | HRESULT result; |
| 2417 | |
| 2418 | if (!mCopyResourcesInitialized) |
| 2419 | { |
| 2420 | ASSERT(!mCopyVB && !mCopySampler && !mCopyIL && !mCopyVS && !mCopyRGBAPS && !mCopyRGBPS && !mCopyLumPS && !mCopyLumAlphaPS); |
| 2421 | |
| 2422 | D3D11_BUFFER_DESC vbDesc; |
| 2423 | vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4; |
| 2424 | vbDesc.Usage = D3D11_USAGE_DYNAMIC; |
| 2425 | vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; |
| 2426 | vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| 2427 | vbDesc.MiscFlags = 0; |
| 2428 | vbDesc.StructureByteStride = 0; |
| 2429 | |
| 2430 | result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB); |
| 2431 | ASSERT(SUCCEEDED(result)); |
| 2432 | d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer"); |
| 2433 | |
| 2434 | D3D11_SAMPLER_DESC samplerDesc; |
| 2435 | samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; |
| 2436 | samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; |
| 2437 | samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; |
| 2438 | samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; |
| 2439 | samplerDesc.MipLODBias = 0.0f; |
| 2440 | samplerDesc.MaxAnisotropy = 0; |
| 2441 | samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; |
| 2442 | samplerDesc.BorderColor[0] = 0.0f; |
| 2443 | samplerDesc.BorderColor[1] = 0.0f; |
| 2444 | samplerDesc.BorderColor[2] = 0.0f; |
| 2445 | samplerDesc.BorderColor[3] = 0.0f; |
| 2446 | samplerDesc.MinLOD = 0.0f; |
| 2447 | samplerDesc.MaxLOD = 0.0f; |
| 2448 | |
| 2449 | result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler); |
| 2450 | ASSERT(SUCCEEDED(result)); |
| 2451 | d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler"); |
| 2452 | |
| 2453 | D3D11_INPUT_ELEMENT_DESC quadLayout[] = |
| 2454 | { |
| 2455 | { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 2456 | { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 2457 | }; |
| 2458 | |
| 2459 | result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mCopyIL); |
| 2460 | ASSERT(SUCCEEDED(result)); |
| 2461 | d3d11::SetDebugName(mCopyIL, "Renderer11 copy texture input layout"); |
| 2462 | |
| 2463 | result = mDevice->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mCopyVS); |
| 2464 | ASSERT(SUCCEEDED(result)); |
| 2465 | d3d11::SetDebugName(mCopyVS, "Renderer11 copy texture vertex shader"); |
| 2466 | |
| 2467 | result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mCopyRGBAPS); |
| 2468 | ASSERT(SUCCEEDED(result)); |
| 2469 | d3d11::SetDebugName(mCopyRGBAPS, "Renderer11 copy texture RGBA pixel shader"); |
| 2470 | |
| 2471 | result = mDevice->CreatePixelShader(g_PS_PassthroughRGB, sizeof(g_PS_PassthroughRGB), NULL, &mCopyRGBPS); |
| 2472 | ASSERT(SUCCEEDED(result)); |
| 2473 | d3d11::SetDebugName(mCopyRGBPS, "Renderer11 copy texture RGB pixel shader"); |
| 2474 | |
| 2475 | result = mDevice->CreatePixelShader(g_PS_PassthroughLum, sizeof(g_PS_PassthroughLum), NULL, &mCopyLumPS); |
| 2476 | ASSERT(SUCCEEDED(result)); |
| 2477 | d3d11::SetDebugName(mCopyLumPS, "Renderer11 copy texture luminance pixel shader"); |
| 2478 | |
| 2479 | result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha, sizeof(g_PS_PassthroughLumAlpha), NULL, &mCopyLumAlphaPS); |
| 2480 | ASSERT(SUCCEEDED(result)); |
| 2481 | d3d11::SetDebugName(mCopyLumAlphaPS, "Renderer11 copy texture luminance alpha pixel shader"); |
| 2482 | |
| 2483 | mCopyResourcesInitialized = true; |
| 2484 | } |
| 2485 | |
| 2486 | // Verify the source and destination area sizes |
| 2487 | if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) || |
| 2488 | sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) || |
| 2489 | destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) || |
| 2490 | destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight)) |
| 2491 | { |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2492 | return gl::error(GL_INVALID_VALUE, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | // Set vertices |
| 2496 | D3D11_MAPPED_SUBRESOURCE mappedResource; |
| 2497 | result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); |
| 2498 | if (FAILED(result)) |
| 2499 | { |
| 2500 | ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2501 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData); |
| 2505 | |
| 2506 | // Create a quad in homogeneous coordinates |
shannon.woods@transgaming.com | c25537e | 2013-02-28 23:06:03 +0000 | [diff] [blame] | 2507 | float x1 = (destArea.x / float(destWidth)) * 2.0f - 1.0f; |
| 2508 | float y1 = (destArea.y / float(destHeight)) * 2.0f - 1.0f; |
| 2509 | float x2 = ((destArea.x + destArea.width) / float(destWidth)) * 2.0f - 1.0f; |
| 2510 | float y2 = ((destArea.y + destArea.height) / float(destHeight)) * 2.0f - 1.0f; |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2511 | |
| 2512 | float u1 = sourceArea.x / float(sourceWidth); |
| 2513 | float v1 = sourceArea.y / float(sourceHeight); |
| 2514 | float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth); |
| 2515 | float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight); |
| 2516 | |
| 2517 | d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2); |
| 2518 | d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1); |
| 2519 | d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2); |
| 2520 | d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1); |
| 2521 | |
| 2522 | mDeviceContext->Unmap(mCopyVB, 0); |
| 2523 | |
| 2524 | static UINT stride = sizeof(d3d11::PositionTexCoordVertex); |
| 2525 | static UINT startIdx = 0; |
| 2526 | mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx); |
| 2527 | |
| 2528 | // Apply state |
shannon.woods@transgaming.com | c25537e | 2013-02-28 23:06:03 +0000 | [diff] [blame] | 2529 | mDeviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2530 | mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF); |
| 2531 | mDeviceContext->RSSetState(NULL); |
| 2532 | |
| 2533 | // Apply shaders |
| 2534 | mDeviceContext->IASetInputLayout(mCopyIL); |
| 2535 | mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); |
| 2536 | mDeviceContext->VSSetShader(mCopyVS, NULL, 0); |
| 2537 | |
| 2538 | ID3D11PixelShader *ps = NULL; |
| 2539 | switch(destFormat) |
| 2540 | { |
| 2541 | case GL_RGBA: ps = mCopyRGBAPS; break; |
| 2542 | case GL_RGB: ps = mCopyRGBPS; break; |
| 2543 | case GL_ALPHA: ps = mCopyRGBAPS; break; |
| 2544 | case GL_BGRA_EXT: ps = mCopyRGBAPS; break; |
| 2545 | case GL_LUMINANCE: ps = mCopyLumPS; break; |
| 2546 | case GL_LUMINANCE_ALPHA: ps = mCopyLumAlphaPS; break; |
| 2547 | default: UNREACHABLE(); ps = NULL; break; |
| 2548 | } |
| 2549 | |
| 2550 | mDeviceContext->PSSetShader(ps, NULL, 0); |
shannon.woods@transgaming.com | 28e7ba0 | 2013-02-28 23:09:28 +0000 | [diff] [blame] | 2551 | mDeviceContext->GSSetShader(NULL, NULL, 0); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2552 | |
| 2553 | // Unset the currently bound shader resource to avoid conflicts |
| 2554 | static ID3D11ShaderResourceView *const nullSRV = NULL; |
| 2555 | mDeviceContext->PSSetShaderResources(0, 1, &nullSRV); |
| 2556 | |
| 2557 | // Apply render targets |
| 2558 | mDeviceContext->OMSetRenderTargets(1, &dest, NULL); |
| 2559 | |
| 2560 | // Set the viewport |
| 2561 | D3D11_VIEWPORT viewport; |
| 2562 | viewport.TopLeftX = 0; |
| 2563 | viewport.TopLeftY = 0; |
| 2564 | viewport.Width = destWidth; |
| 2565 | viewport.Height = destHeight; |
| 2566 | viewport.MinDepth = 0.0f; |
| 2567 | viewport.MaxDepth = 1.0f; |
| 2568 | mDeviceContext->RSSetViewports(1, &viewport); |
| 2569 | |
| 2570 | // Apply textures |
| 2571 | mDeviceContext->PSSetShaderResources(0, 1, &source); |
| 2572 | mDeviceContext->PSSetSamplers(0, 1, &mCopySampler); |
| 2573 | |
| 2574 | // Draw the quad |
| 2575 | mDeviceContext->Draw(4, 0); |
| 2576 | |
| 2577 | // Unbind textures and render targets and vertex buffer |
| 2578 | mDeviceContext->PSSetShaderResources(0, 1, &nullSRV); |
| 2579 | |
| 2580 | static ID3D11RenderTargetView *const nullRTV = NULL; |
| 2581 | mDeviceContext->OMSetRenderTargets(1, &nullRTV, NULL); |
| 2582 | |
| 2583 | static UINT zero = 0; |
| 2584 | static ID3D11Buffer *const nullBuffer = NULL; |
| 2585 | mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero); |
| 2586 | |
| 2587 | markAllStateDirty(); |
| 2588 | |
| 2589 | return true; |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 2592 | RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth) |
| 2593 | { |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame] | 2594 | SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain); |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 2595 | RenderTarget11 *renderTarget = NULL; |
shannon.woods@transgaming.com | 8c6d9df | 2013-02-28 23:08:57 +0000 | [diff] [blame] | 2596 | |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 2597 | if (depth) |
| 2598 | { |
shannon.woods@transgaming.com | 8c6d9df | 2013-02-28 23:08:57 +0000 | [diff] [blame] | 2599 | // Note: depth stencil may be NULL for 0 sized surfaces |
shannon.woods@transgaming.com | 7e23285 | 2013-02-28 23:06:15 +0000 | [diff] [blame] | 2600 | renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(), |
| 2601 | swapChain11->getDepthStencilTexture(), NULL, |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame] | 2602 | swapChain11->getWidth(), swapChain11->getHeight()); |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 2603 | } |
| 2604 | else |
| 2605 | { |
shannon.woods@transgaming.com | 8c6d9df | 2013-02-28 23:08:57 +0000 | [diff] [blame] | 2606 | // Note: render target may be NULL for 0 sized surfaces |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame] | 2607 | renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(), |
shannon.woods@transgaming.com | 7e23285 | 2013-02-28 23:06:15 +0000 | [diff] [blame] | 2608 | swapChain11->getOffscreenTexture(), |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame] | 2609 | swapChain11->getRenderTargetShaderResource(), |
| 2610 | swapChain11->getWidth(), swapChain11->getHeight()); |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 2611 | } |
| 2612 | return renderTarget; |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth) |
| 2616 | { |
daniel@transgaming.com | c9a501d | 2013-01-11 04:08:46 +0000 | [diff] [blame] | 2617 | RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth); |
| 2618 | return renderTarget; |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 2621 | ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type) |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 2622 | { |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2623 | ShaderExecutable11 *executable = NULL; |
| 2624 | |
| 2625 | switch (type) |
| 2626 | { |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 2627 | case rx::SHADER_VERTEX: |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2628 | { |
| 2629 | ID3D11VertexShader *vshader = NULL; |
| 2630 | HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader); |
| 2631 | ASSERT(SUCCEEDED(result)); |
| 2632 | |
| 2633 | if (vshader) |
| 2634 | { |
daniel@transgaming.com | 7b18d0c | 2012-11-28 21:04:10 +0000 | [diff] [blame] | 2635 | executable = new ShaderExecutable11(function, length, vshader); |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2636 | } |
| 2637 | } |
| 2638 | break; |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 2639 | case rx::SHADER_PIXEL: |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2640 | { |
| 2641 | ID3D11PixelShader *pshader = NULL; |
| 2642 | HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader); |
| 2643 | ASSERT(SUCCEEDED(result)); |
| 2644 | |
| 2645 | if (pshader) |
| 2646 | { |
daniel@transgaming.com | 7b18d0c | 2012-11-28 21:04:10 +0000 | [diff] [blame] | 2647 | executable = new ShaderExecutable11(function, length, pshader); |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2648 | } |
| 2649 | } |
| 2650 | break; |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 2651 | case rx::SHADER_GEOMETRY: |
| 2652 | { |
| 2653 | ID3D11GeometryShader *gshader = NULL; |
| 2654 | HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader); |
| 2655 | ASSERT(SUCCEEDED(result)); |
| 2656 | |
| 2657 | if (gshader) |
| 2658 | { |
| 2659 | executable = new ShaderExecutable11(function, length, gshader); |
| 2660 | } |
| 2661 | } |
| 2662 | break; |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2663 | default: |
| 2664 | UNREACHABLE(); |
| 2665 | break; |
| 2666 | } |
| 2667 | |
| 2668 | return executable; |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 2671 | ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type) |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 2672 | { |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2673 | const char *profile = NULL; |
| 2674 | |
| 2675 | switch (type) |
| 2676 | { |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 2677 | case rx::SHADER_VERTEX: |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2678 | profile = "vs_4_0"; |
| 2679 | break; |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 2680 | case rx::SHADER_PIXEL: |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2681 | profile = "ps_4_0"; |
| 2682 | break; |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 2683 | case rx::SHADER_GEOMETRY: |
| 2684 | profile = "gs_4_0"; |
| 2685 | break; |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2686 | default: |
| 2687 | UNREACHABLE(); |
| 2688 | return NULL; |
| 2689 | } |
| 2690 | |
daniel@transgaming.com | 25e16af | 2012-11-28 21:05:57 +0000 | [diff] [blame] | 2691 | ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, false); |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2692 | if (!binary) |
| 2693 | return NULL; |
| 2694 | |
daniel@transgaming.com | 2275f91 | 2012-12-20 21:13:22 +0000 | [diff] [blame] | 2695 | ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type); |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2696 | binary->Release(); |
| 2697 | |
| 2698 | return executable; |
| 2699 | } |
| 2700 | |
daniel@transgaming.com | 3f255b4 | 2012-12-20 21:07:35 +0000 | [diff] [blame] | 2701 | VertexBuffer *Renderer11::createVertexBuffer() |
| 2702 | { |
daniel@transgaming.com | 2c4d070 | 2012-12-20 21:08:51 +0000 | [diff] [blame] | 2703 | return new VertexBuffer11(this); |
daniel@transgaming.com | 3f255b4 | 2012-12-20 21:07:35 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
daniel@transgaming.com | 0b6d774 | 2012-12-20 21:09:47 +0000 | [diff] [blame] | 2706 | IndexBuffer *Renderer11::createIndexBuffer() |
| 2707 | { |
daniel@transgaming.com | 11c2af5 | 2012-12-20 21:10:01 +0000 | [diff] [blame] | 2708 | return new IndexBuffer11(this); |
shannon.woods@transgaming.com | cfe787e | 2013-02-28 23:03:35 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
shannon.woods@transgaming.com | 4e52b63 | 2013-02-28 23:08:01 +0000 | [diff] [blame] | 2711 | BufferStorage *Renderer11::createBufferStorage() |
| 2712 | { |
| 2713 | return new BufferStorage11(this); |
| 2714 | } |
| 2715 | |
shannon.woods@transgaming.com | cfe787e | 2013-02-28 23:03:35 +0000 | [diff] [blame] | 2716 | QueryImpl *Renderer11::createQuery(GLenum type) |
| 2717 | { |
shannon.woods@transgaming.com | 8b7606a | 2013-02-28 23:03:47 +0000 | [diff] [blame] | 2718 | return new Query11(this, type); |
shannon.woods@transgaming.com | cfe787e | 2013-02-28 23:03:35 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | FenceImpl *Renderer11::createFence() |
| 2722 | { |
shannon.woods@transgaming.com | be58aa0 | 2013-02-28 23:03:55 +0000 | [diff] [blame] | 2723 | return new Fence11(this); |
daniel@transgaming.com | 0b6d774 | 2012-12-20 21:09:47 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
shannon.woods@transgaming.com | fdeacb8 | 2013-01-25 21:52:34 +0000 | [diff] [blame] | 2726 | bool Renderer11::getRenderTargetResource(gl::Framebuffer *framebuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource) |
| 2727 | { |
| 2728 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 2729 | if (colorbuffer) |
| 2730 | { |
| 2731 | RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); |
| 2732 | if (renderTarget) |
| 2733 | { |
| 2734 | *subresourceIndex = renderTarget->getSubresourceIndex(); |
| 2735 | |
| 2736 | ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView(); |
| 2737 | if (colorBufferRTV) |
| 2738 | { |
| 2739 | ID3D11Resource *textureResource = NULL; |
| 2740 | colorBufferRTV->GetResource(&textureResource); |
| 2741 | colorBufferRTV->Release(); |
| 2742 | |
| 2743 | if (textureResource) |
| 2744 | { |
| 2745 | HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource); |
| 2746 | textureResource->Release(); |
| 2747 | |
| 2748 | if (SUCCEEDED(result)) |
| 2749 | { |
| 2750 | return true; |
| 2751 | } |
| 2752 | else |
| 2753 | { |
| 2754 | ERR("Failed to extract the ID3D11Texture2D from the render target resource, " |
| 2755 | "HRESULT: 0x%X.", result); |
| 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | return false; |
| 2763 | } |
| 2764 | |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 2765 | bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect, |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2766 | bool blitRenderTarget, bool blitDepthStencil) |
| 2767 | { |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 2768 | if (blitRenderTarget && !blitRect(readTarget, readRect, drawTarget, drawRect, BLIT_RENDERTARGET)) |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 2769 | { |
| 2770 | return false; |
| 2771 | } |
| 2772 | |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 2773 | if (blitDepthStencil && !blitRect(readTarget, readRect, drawTarget, drawRect, BLIT_DEPTHSTENCIL)) |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 2774 | { |
| 2775 | return false; |
| 2776 | } |
| 2777 | |
| 2778 | return true; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, |
| 2782 | GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels) |
| 2783 | { |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2784 | ID3D11Texture2D *colorBufferTexture = NULL; |
daniel@transgaming.com | 2eb7ab7 | 2013-01-11 04:10:21 +0000 | [diff] [blame] | 2785 | unsigned int subresourceIndex = 0; |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2786 | |
shannon.woods@transgaming.com | fdeacb8 | 2013-01-25 21:52:34 +0000 | [diff] [blame] | 2787 | if (getRenderTargetResource(framebuffer, &subresourceIndex, &colorBufferTexture)) |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2788 | { |
| 2789 | gl::Rectangle area; |
| 2790 | area.x = x; |
| 2791 | area.y = y; |
| 2792 | area.width = width; |
| 2793 | area.height = height; |
| 2794 | |
daniel@transgaming.com | 2eb7ab7 | 2013-01-11 04:10:21 +0000 | [diff] [blame] | 2795 | readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, |
| 2796 | packReverseRowOrder, packAlignment, pixels); |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2797 | |
| 2798 | colorBufferTexture->Release(); |
| 2799 | colorBufferTexture = NULL; |
| 2800 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
daniel@transgaming.com | 244e183 | 2012-12-20 20:52:35 +0000 | [diff] [blame] | 2803 | Image *Renderer11::createImage() |
| 2804 | { |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 2805 | return new Image11(); |
daniel@transgaming.com | 244e183 | 2012-12-20 20:52:35 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
daniel@transgaming.com | f721fdb | 2012-12-20 20:53:11 +0000 | [diff] [blame] | 2808 | void Renderer11::generateMipmap(Image *dest, Image *src) |
| 2809 | { |
shannon.woods@transgaming.com | 2b132f4 | 2013-01-25 21:52:47 +0000 | [diff] [blame] | 2810 | Image11 *dest11 = Image11::makeImage11(dest); |
| 2811 | Image11 *src11 = Image11::makeImage11(src); |
| 2812 | Image11::generateMipmap(dest11, src11); |
daniel@transgaming.com | f721fdb | 2012-12-20 20:53:11 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 2815 | TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain) |
| 2816 | { |
daniel@transgaming.com | 36670db | 2013-01-11 04:08:22 +0000 | [diff] [blame] | 2817 | SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain); |
| 2818 | return new TextureStorage11_2D(this, swapChain11); |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
| 2821 | TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) |
| 2822 | { |
daniel@transgaming.com | 36670db | 2013-01-11 04:08:22 +0000 | [diff] [blame] | 2823 | return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height); |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) |
| 2827 | { |
daniel@transgaming.com | 36670db | 2013-01-11 04:08:22 +0000 | [diff] [blame] | 2828 | return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size); |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2831 | static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType) |
| 2832 | { |
| 2833 | if (sourceFormat == DXGI_FORMAT_A8_UNORM && |
| 2834 | destFormat == GL_ALPHA && |
| 2835 | destType == GL_UNSIGNED_BYTE) |
| 2836 | { |
| 2837 | return 1; |
| 2838 | } |
| 2839 | else if (sourceFormat == DXGI_FORMAT_R8G8B8A8_UNORM && |
| 2840 | destFormat == GL_RGBA && |
| 2841 | destType == GL_UNSIGNED_BYTE) |
| 2842 | { |
| 2843 | return 4; |
| 2844 | } |
| 2845 | else if (sourceFormat == DXGI_FORMAT_B8G8R8A8_UNORM && |
| 2846 | destFormat == GL_BGRA_EXT && |
| 2847 | destType == GL_UNSIGNED_BYTE) |
| 2848 | { |
| 2849 | return 4; |
| 2850 | } |
| 2851 | else if (sourceFormat == DXGI_FORMAT_R16G16B16A16_FLOAT && |
| 2852 | destFormat == GL_RGBA && |
| 2853 | destType == GL_HALF_FLOAT_OES) |
| 2854 | { |
| 2855 | return 8; |
| 2856 | } |
| 2857 | else if (sourceFormat == DXGI_FORMAT_R32G32B32_FLOAT && |
| 2858 | destFormat == GL_RGB && |
| 2859 | destType == GL_FLOAT) |
| 2860 | { |
| 2861 | return 12; |
| 2862 | } |
| 2863 | else if (sourceFormat == DXGI_FORMAT_R32G32B32A32_FLOAT && |
| 2864 | destFormat == GL_RGBA && |
| 2865 | destType == GL_FLOAT) |
| 2866 | { |
| 2867 | return 16; |
| 2868 | } |
| 2869 | else |
| 2870 | { |
| 2871 | return 0; |
| 2872 | } |
| 2873 | } |
| 2874 | |
| 2875 | static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x, |
| 2876 | unsigned int y, int inputPitch, gl::Color *outColor) |
| 2877 | { |
| 2878 | switch (format) |
| 2879 | { |
| 2880 | case DXGI_FORMAT_R8G8B8A8_UNORM: |
| 2881 | { |
| 2882 | unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch); |
shannon.woods@transgaming.com | 25950ea | 2013-02-28 23:04:15 +0000 | [diff] [blame] | 2883 | outColor->red = (rgba & 0x000000FF) * (1.0f / 0x000000FF); |
| 2884 | outColor->green = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 2885 | outColor->blue = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 2886 | outColor->alpha = (rgba & 0xFF000000) * (1.0f / 0xFF000000); |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2887 | } |
| 2888 | break; |
| 2889 | |
| 2890 | case DXGI_FORMAT_A8_UNORM: |
| 2891 | { |
| 2892 | outColor->red = 0.0f; |
| 2893 | outColor->green = 0.0f; |
| 2894 | outColor->blue = 0.0f; |
| 2895 | outColor->alpha = *(data + x + y * inputPitch) / 255.0f; |
| 2896 | } |
| 2897 | break; |
| 2898 | |
| 2899 | case DXGI_FORMAT_R32G32B32A32_FLOAT: |
| 2900 | { |
| 2901 | outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0); |
| 2902 | outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1); |
| 2903 | outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2); |
| 2904 | outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3); |
| 2905 | } |
| 2906 | break; |
| 2907 | |
| 2908 | case DXGI_FORMAT_R32G32B32_FLOAT: |
| 2909 | { |
| 2910 | outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0); |
| 2911 | outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1); |
| 2912 | outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2); |
| 2913 | outColor->alpha = 1.0f; |
| 2914 | } |
| 2915 | break; |
| 2916 | |
| 2917 | case DXGI_FORMAT_R16G16B16A16_FLOAT: |
| 2918 | { |
| 2919 | outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0)); |
| 2920 | outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1)); |
| 2921 | outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2)); |
| 2922 | outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3)); |
| 2923 | } |
| 2924 | break; |
| 2925 | |
| 2926 | case DXGI_FORMAT_B8G8R8A8_UNORM: |
| 2927 | { |
| 2928 | unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch); |
shannon.woods@transgaming.com | 25950ea | 2013-02-28 23:04:15 +0000 | [diff] [blame] | 2929 | outColor->red = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 2930 | outColor->blue = (bgra & 0x000000FF) * (1.0f / 0x000000FF); |
| 2931 | outColor->green = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 2932 | outColor->alpha = (bgra & 0xFF000000) * (1.0f / 0xFF000000); |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2933 | } |
| 2934 | break; |
| 2935 | |
| 2936 | case DXGI_FORMAT_R8_UNORM: |
| 2937 | { |
| 2938 | outColor->red = *(data + x + y * inputPitch) / 255.0f; |
| 2939 | outColor->green = 0.0f; |
| 2940 | outColor->blue = 0.0f; |
| 2941 | outColor->alpha = 1.0f; |
| 2942 | } |
| 2943 | break; |
| 2944 | |
| 2945 | case DXGI_FORMAT_R8G8_UNORM: |
| 2946 | { |
| 2947 | unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch); |
| 2948 | |
| 2949 | outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00); |
| 2950 | outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF); |
| 2951 | outColor->blue = 0.0f; |
| 2952 | outColor->alpha = 1.0f; |
| 2953 | } |
| 2954 | break; |
| 2955 | |
| 2956 | case DXGI_FORMAT_R16_FLOAT: |
| 2957 | { |
| 2958 | outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch)); |
| 2959 | outColor->green = 0.0f; |
| 2960 | outColor->blue = 0.0f; |
| 2961 | outColor->alpha = 1.0f; |
| 2962 | } |
| 2963 | break; |
| 2964 | |
| 2965 | case DXGI_FORMAT_R16G16_FLOAT: |
| 2966 | { |
| 2967 | outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0)); |
| 2968 | outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1)); |
| 2969 | outColor->blue = 0.0f; |
| 2970 | outColor->alpha = 1.0f; |
| 2971 | } |
| 2972 | break; |
| 2973 | |
| 2974 | default: |
| 2975 | ERR("ReadPixelColor not implemented for DXGI format %u.", format); |
| 2976 | UNIMPLEMENTED(); |
| 2977 | break; |
| 2978 | } |
| 2979 | } |
| 2980 | |
| 2981 | static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x, |
| 2982 | unsigned int y, int outputPitch, void *outData) |
| 2983 | { |
| 2984 | unsigned char* byteData = reinterpret_cast<unsigned char*>(outData); |
| 2985 | unsigned short* shortData = reinterpret_cast<unsigned short*>(outData); |
| 2986 | |
| 2987 | switch (format) |
| 2988 | { |
| 2989 | case GL_RGBA: |
| 2990 | switch (type) |
| 2991 | { |
| 2992 | case GL_UNSIGNED_BYTE: |
| 2993 | byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f); |
| 2994 | byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f); |
| 2995 | byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f); |
| 2996 | byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f); |
| 2997 | break; |
| 2998 | |
| 2999 | default: |
| 3000 | ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type); |
| 3001 | UNIMPLEMENTED(); |
| 3002 | break; |
| 3003 | } |
| 3004 | break; |
| 3005 | |
| 3006 | case GL_BGRA_EXT: |
| 3007 | switch (type) |
| 3008 | { |
| 3009 | case GL_UNSIGNED_BYTE: |
| 3010 | byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f); |
| 3011 | byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f); |
| 3012 | byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f); |
| 3013 | byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f); |
| 3014 | break; |
| 3015 | |
| 3016 | case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: |
| 3017 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 3018 | // this type is packed as follows: |
| 3019 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 3020 | // -------------------------------------------------------------------------------- |
| 3021 | // | 4th | 3rd | 2nd | 1st component | |
| 3022 | // -------------------------------------------------------------------------------- |
| 3023 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 3024 | shortData[x + y * outputPitch / sizeof(unsigned short)] = |
| 3025 | (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) | |
| 3026 | (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) | |
| 3027 | (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) | |
| 3028 | (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0); |
| 3029 | break; |
| 3030 | |
| 3031 | case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: |
| 3032 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 3033 | // this type is packed as follows: |
| 3034 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 3035 | // -------------------------------------------------------------------------------- |
| 3036 | // | 4th | 3rd | 2nd | 1st component | |
| 3037 | // -------------------------------------------------------------------------------- |
| 3038 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 3039 | shortData[x + y * outputPitch / sizeof(unsigned short)] = |
| 3040 | (static_cast<unsigned short>( color.alpha + 0.5f) << 15) | |
| 3041 | (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) | |
| 3042 | (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) | |
| 3043 | (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0); |
| 3044 | break; |
| 3045 | |
| 3046 | default: |
| 3047 | ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type); |
| 3048 | UNIMPLEMENTED(); |
| 3049 | break; |
| 3050 | } |
| 3051 | break; |
| 3052 | |
| 3053 | case GL_RGB: |
| 3054 | switch (type) |
| 3055 | { |
| 3056 | case GL_UNSIGNED_SHORT_5_6_5: |
| 3057 | shortData[x + y * outputPitch / sizeof(unsigned short)] = |
| 3058 | (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) | |
| 3059 | (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) | |
| 3060 | (static_cast<unsigned short>(31 * color.red + 0.5f) << 11); |
| 3061 | break; |
| 3062 | |
| 3063 | case GL_UNSIGNED_BYTE: |
| 3064 | byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f); |
| 3065 | byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f); |
| 3066 | byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f); |
| 3067 | break; |
| 3068 | |
| 3069 | default: |
| 3070 | ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type); |
| 3071 | UNIMPLEMENTED(); |
| 3072 | break; |
| 3073 | } |
| 3074 | break; |
| 3075 | |
| 3076 | default: |
| 3077 | ERR("WritePixelColor not implemented for format 0x%X.", format); |
| 3078 | UNIMPLEMENTED(); |
| 3079 | break; |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, |
| 3084 | GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder, |
| 3085 | GLint packAlignment, void *pixels) |
| 3086 | { |
| 3087 | D3D11_TEXTURE2D_DESC textureDesc; |
| 3088 | texture->GetDesc(&textureDesc); |
| 3089 | |
| 3090 | D3D11_TEXTURE2D_DESC stagingDesc; |
| 3091 | stagingDesc.Width = area.width; |
| 3092 | stagingDesc.Height = area.height; |
| 3093 | stagingDesc.MipLevels = 1; |
| 3094 | stagingDesc.ArraySize = 1; |
| 3095 | stagingDesc.Format = textureDesc.Format; |
| 3096 | stagingDesc.SampleDesc.Count = 1; |
| 3097 | stagingDesc.SampleDesc.Quality = 0; |
| 3098 | stagingDesc.Usage = D3D11_USAGE_STAGING; |
| 3099 | stagingDesc.BindFlags = 0; |
| 3100 | stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; |
| 3101 | stagingDesc.MiscFlags = 0; |
| 3102 | |
| 3103 | ID3D11Texture2D* stagingTex = NULL; |
| 3104 | HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex); |
| 3105 | if (FAILED(result)) |
| 3106 | { |
| 3107 | ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result); |
| 3108 | return; |
| 3109 | } |
| 3110 | |
| 3111 | ID3D11Texture2D* srcTex = NULL; |
| 3112 | if (textureDesc.SampleDesc.Count > 1) |
| 3113 | { |
| 3114 | D3D11_TEXTURE2D_DESC resolveDesc; |
| 3115 | resolveDesc.Width = textureDesc.Width; |
| 3116 | resolveDesc.Height = textureDesc.Height; |
| 3117 | resolveDesc.MipLevels = 1; |
| 3118 | resolveDesc.ArraySize = 1; |
| 3119 | resolveDesc.Format = textureDesc.Format; |
| 3120 | resolveDesc.SampleDesc.Count = 1; |
| 3121 | resolveDesc.SampleDesc.Quality = 0; |
| 3122 | resolveDesc.Usage = D3D11_USAGE_DEFAULT; |
| 3123 | resolveDesc.BindFlags = 0; |
| 3124 | resolveDesc.CPUAccessFlags = 0; |
| 3125 | resolveDesc.MiscFlags = 0; |
| 3126 | |
| 3127 | result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex); |
| 3128 | if (FAILED(result)) |
| 3129 | { |
| 3130 | ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result); |
| 3131 | stagingTex->Release(); |
| 3132 | return; |
| 3133 | } |
| 3134 | |
| 3135 | mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format); |
| 3136 | subResource = 0; |
| 3137 | } |
| 3138 | else |
| 3139 | { |
| 3140 | srcTex = texture; |
| 3141 | srcTex->AddRef(); |
| 3142 | } |
| 3143 | |
| 3144 | D3D11_BOX srcBox; |
| 3145 | srcBox.left = area.x; |
| 3146 | srcBox.right = area.x + area.width; |
| 3147 | srcBox.top = area.y; |
| 3148 | srcBox.bottom = area.y + area.height; |
| 3149 | srcBox.front = 0; |
| 3150 | srcBox.back = 1; |
| 3151 | |
| 3152 | mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox); |
| 3153 | |
| 3154 | srcTex->Release(); |
| 3155 | srcTex = NULL; |
| 3156 | |
| 3157 | D3D11_MAPPED_SUBRESOURCE mapping; |
| 3158 | mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping); |
| 3159 | |
| 3160 | unsigned char *source; |
| 3161 | int inputPitch; |
| 3162 | if (packReverseRowOrder) |
| 3163 | { |
| 3164 | source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1); |
| 3165 | inputPitch = -static_cast<int>(mapping.RowPitch); |
| 3166 | } |
| 3167 | else |
| 3168 | { |
| 3169 | source = static_cast<unsigned char*>(mapping.pData); |
| 3170 | inputPitch = static_cast<int>(mapping.RowPitch); |
| 3171 | } |
| 3172 | |
| 3173 | unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type); |
| 3174 | if (fastPixelSize != 0) |
| 3175 | { |
| 3176 | unsigned char *dest = static_cast<unsigned char*>(pixels); |
| 3177 | for (int j = 0; j < area.height; j++) |
| 3178 | { |
| 3179 | memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize); |
| 3180 | } |
| 3181 | } |
| 3182 | else |
| 3183 | { |
| 3184 | gl::Color pixelColor; |
| 3185 | for (int j = 0; j < area.height; j++) |
| 3186 | { |
| 3187 | for (int i = 0; i < area.width; i++) |
| 3188 | { |
| 3189 | readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor); |
| 3190 | writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels); |
| 3191 | } |
| 3192 | } |
| 3193 | } |
| 3194 | |
| 3195 | mDeviceContext->Unmap(stagingTex, 0); |
| 3196 | |
| 3197 | stagingTex->Release(); |
| 3198 | stagingTex = NULL; |
| 3199 | } |
| 3200 | |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 3201 | bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, |
| 3202 | const gl::Rectangle &drawRect, BlitTarget target) |
| 3203 | { |
| 3204 | ASSERT(readRect.width == drawRect.width && readRect.height == drawRect.height); |
| 3205 | |
| 3206 | gl::Renderbuffer *readBuffer = NULL; |
| 3207 | switch (target) |
| 3208 | { |
| 3209 | case BLIT_RENDERTARGET: |
| 3210 | readBuffer = readTarget->getColorbuffer(); |
| 3211 | break; |
| 3212 | case BLIT_DEPTHSTENCIL: |
| 3213 | readBuffer = readTarget->getDepthOrStencilbuffer(); |
| 3214 | break; |
| 3215 | default: UNREACHABLE(); |
| 3216 | } |
| 3217 | if (!readBuffer) |
| 3218 | { |
| 3219 | ERR("Failed to retrieve the color buffer from the read target."); |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 3220 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | RenderTarget11 *sourceRenderTarget = NULL; |
| 3224 | switch (target) |
| 3225 | { |
| 3226 | case BLIT_RENDERTARGET: |
| 3227 | sourceRenderTarget = RenderTarget11::makeRenderTarget11(readBuffer->getRenderTarget()); |
| 3228 | break; |
| 3229 | case BLIT_DEPTHSTENCIL: |
| 3230 | sourceRenderTarget = RenderTarget11::makeRenderTarget11(readBuffer->getDepthStencil()); |
| 3231 | break; |
| 3232 | default: UNREACHABLE(); |
| 3233 | } |
| 3234 | if (!sourceRenderTarget) |
| 3235 | { |
| 3236 | ERR("Failed to retrieve the render target from the frame buffer."); |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 3237 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | ID3D11Texture2D *source = NULL; |
| 3241 | unsigned int sourceSubresource = 0; |
| 3242 | if (sourceRenderTarget->getSamples() > 0) |
| 3243 | { |
| 3244 | ID3D11Texture2D *unresolvedTexture = sourceRenderTarget->getTexture(); |
| 3245 | |
| 3246 | source = resolveMultisampledTexture(unresolvedTexture, sourceRenderTarget->getSubresourceIndex()); |
| 3247 | sourceSubresource = 0; |
| 3248 | |
| 3249 | unresolvedTexture->Release(); |
| 3250 | } |
| 3251 | else |
| 3252 | { |
| 3253 | source = sourceRenderTarget->getTexture(); |
| 3254 | sourceSubresource = sourceRenderTarget->getSubresourceIndex(); |
| 3255 | } |
| 3256 | |
| 3257 | if (!source) |
| 3258 | { |
| 3259 | ERR("Failed to retrieve the render target view from the render target."); |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 3260 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | |
| 3264 | gl::Renderbuffer *drawBuffer = NULL; |
| 3265 | switch (target) |
| 3266 | { |
| 3267 | case BLIT_RENDERTARGET: |
| 3268 | drawBuffer = drawTarget->getColorbuffer(); |
| 3269 | break; |
| 3270 | case BLIT_DEPTHSTENCIL: |
| 3271 | drawBuffer = drawTarget->getDepthOrStencilbuffer(); |
| 3272 | break; |
| 3273 | default: UNREACHABLE(); |
| 3274 | } |
| 3275 | if (!drawBuffer) |
| 3276 | { |
| 3277 | source->Release(); |
| 3278 | ERR("Failed to retrieve the color buffer from the draw buffer."); |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 3279 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
| 3282 | RenderTarget11 *drawRenderTarget = NULL; |
| 3283 | switch (target) |
| 3284 | { |
| 3285 | case BLIT_RENDERTARGET: |
| 3286 | drawRenderTarget = RenderTarget11::makeRenderTarget11(drawBuffer->getRenderTarget()); |
| 3287 | break; |
| 3288 | case BLIT_DEPTHSTENCIL: |
| 3289 | drawRenderTarget = RenderTarget11::makeRenderTarget11(drawBuffer->getDepthStencil()); |
| 3290 | break; |
| 3291 | default: UNREACHABLE(); |
| 3292 | } |
| 3293 | if (!drawRenderTarget) |
| 3294 | { |
| 3295 | source->Release(); |
| 3296 | ERR("Failed to retrieve the render target from the render buffer."); |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 3297 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | ID3D11Texture2D *dest = drawRenderTarget->getTexture(); |
| 3301 | unsigned int destSubresource = drawRenderTarget->getSubresourceIndex(); |
| 3302 | |
| 3303 | D3D11_BOX srcBox; |
| 3304 | srcBox.left = readRect.x; |
| 3305 | srcBox.right = readRect.x + readRect.width; |
| 3306 | srcBox.top = readRect.y; |
| 3307 | srcBox.bottom = readRect.y + readRect.height; |
| 3308 | srcBox.front = 0; |
| 3309 | srcBox.back = 1; |
| 3310 | |
| 3311 | mDeviceContext->CopySubresourceRegion(dest, destSubresource, drawRect.x, drawRect.y, 0, |
| 3312 | source, sourceSubresource, &srcBox); |
| 3313 | |
| 3314 | source->Release(); |
| 3315 | dest->Release(); |
| 3316 | |
| 3317 | return true; |
| 3318 | } |
| 3319 | |
shannon.woods@transgaming.com | d67f9ce | 2013-02-28 23:06:09 +0000 | [diff] [blame] | 3320 | ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource) |
| 3321 | { |
| 3322 | D3D11_TEXTURE2D_DESC textureDesc; |
| 3323 | source->GetDesc(&textureDesc); |
| 3324 | |
| 3325 | if (textureDesc.SampleDesc.Count > 1) |
| 3326 | { |
| 3327 | D3D11_TEXTURE2D_DESC resolveDesc; |
| 3328 | resolveDesc.Width = textureDesc.Width; |
| 3329 | resolveDesc.Height = textureDesc.Height; |
| 3330 | resolveDesc.MipLevels = 1; |
| 3331 | resolveDesc.ArraySize = 1; |
| 3332 | resolveDesc.Format = textureDesc.Format; |
| 3333 | resolveDesc.SampleDesc.Count = 1; |
| 3334 | resolveDesc.SampleDesc.Quality = 0; |
| 3335 | resolveDesc.Usage = textureDesc.Usage; |
| 3336 | resolveDesc.BindFlags = textureDesc.BindFlags; |
| 3337 | resolveDesc.CPUAccessFlags = 0; |
| 3338 | resolveDesc.MiscFlags = 0; |
| 3339 | |
| 3340 | ID3D11Texture2D *resolveTexture = NULL; |
| 3341 | HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture); |
| 3342 | if (FAILED(result)) |
| 3343 | { |
| 3344 | ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result); |
| 3345 | return NULL; |
| 3346 | } |
| 3347 | |
| 3348 | mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format); |
| 3349 | return resolveTexture; |
| 3350 | } |
| 3351 | else |
| 3352 | { |
| 3353 | source->AddRef(); |
| 3354 | return source; |
| 3355 | } |
| 3356 | } |
| 3357 | |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 3358 | } |