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