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