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