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