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