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