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