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