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