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