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