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 | |
| 1873 | bool Renderer11::getNonPower2TextureSupport() const |
| 1874 | { |
| 1875 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 1876 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1877 | return false; |
| 1878 | } |
| 1879 | |
| 1880 | bool Renderer11::getOcclusionQuerySupport() const |
| 1881 | { |
| 1882 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 1883 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1884 | return false; |
| 1885 | } |
| 1886 | |
| 1887 | bool Renderer11::getInstancingSupport() const |
| 1888 | { |
| 1889 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 1890 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1891 | return false; |
| 1892 | } |
| 1893 | |
| 1894 | bool Renderer11::getShareHandleSupport() const |
| 1895 | { |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame^] | 1896 | // We only currently support share handles with BGRA surfaces, because |
| 1897 | // 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] | 1898 | // 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^] | 1899 | return getBGRATextureSupport() && !gl::perfActive(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
daniel@transgaming.com | 7629bb6 | 2013-01-11 04:12:28 +0000 | [diff] [blame] | 1902 | bool Renderer11::getDerivativeInstructionSupport() const |
| 1903 | { |
| 1904 | // TODO |
| 1905 | // UNIMPLEMENTED(); |
| 1906 | return false; |
| 1907 | } |
| 1908 | |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 1909 | int Renderer11::getMajorShaderModel() const |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1910 | { |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 1911 | switch (mFeatureLevel) |
| 1912 | { |
| 1913 | 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] | 1914 | 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] | 1915 | case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4 |
| 1916 | default: UNREACHABLE(); return 0; |
| 1917 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
daniel@transgaming.com | ca1ac1f | 2013-01-11 04:13:05 +0000 | [diff] [blame] | 1920 | int Renderer11::getMinorShaderModel() const |
| 1921 | { |
| 1922 | switch (mFeatureLevel) |
| 1923 | { |
| 1924 | case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0 |
| 1925 | case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1 |
| 1926 | case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0 |
| 1927 | default: UNREACHABLE(); return 0; |
| 1928 | } |
| 1929 | } |
| 1930 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1931 | float Renderer11::getMaxPointSize() const |
| 1932 | { |
| 1933 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 1934 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1935 | return 1.0f; |
| 1936 | } |
| 1937 | |
| 1938 | int Renderer11::getMaxTextureWidth() const |
| 1939 | { |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 1940 | switch (mFeatureLevel) |
| 1941 | { |
| 1942 | case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384 |
| 1943 | case D3D_FEATURE_LEVEL_10_1: |
| 1944 | case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192 |
| 1945 | default: UNREACHABLE(); return 0; |
| 1946 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | int Renderer11::getMaxTextureHeight() const |
| 1950 | { |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 1951 | switch (mFeatureLevel) |
| 1952 | { |
| 1953 | case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384 |
| 1954 | case D3D_FEATURE_LEVEL_10_1: |
| 1955 | case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192 |
| 1956 | default: UNREACHABLE(); return 0; |
| 1957 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | bool Renderer11::get32BitIndexSupport() const |
| 1961 | { |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 1962 | switch (mFeatureLevel) |
| 1963 | { |
| 1964 | case D3D_FEATURE_LEVEL_11_0: |
| 1965 | case D3D_FEATURE_LEVEL_10_1: |
| 1966 | case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true |
| 1967 | default: UNREACHABLE(); return false; |
| 1968 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | int Renderer11::getMinSwapInterval() const |
| 1972 | { |
daniel@transgaming.com | 8c7b1a9 | 2012-11-28 19:34:06 +0000 | [diff] [blame] | 1973 | return 0; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | int Renderer11::getMaxSwapInterval() const |
| 1977 | { |
daniel@transgaming.com | 8c7b1a9 | 2012-11-28 19:34:06 +0000 | [diff] [blame] | 1978 | return 4; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | int Renderer11::getMaxSupportedSamples() const |
| 1982 | { |
| 1983 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 1984 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1985 | return 1; |
| 1986 | } |
| 1987 | |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 1988 | bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source) |
daniel@transgaming.com | ad6aee7 | 2012-11-28 19:33:42 +0000 | [diff] [blame] | 1989 | { |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 1990 | if (source && dest) |
| 1991 | { |
| 1992 | TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance()); |
| 1993 | TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance()); |
| 1994 | |
daniel@transgaming.com | e9cf5e7 | 2013-01-11 04:06:55 +0000 | [diff] [blame] | 1995 | mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture()); |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 1996 | return true; |
| 1997 | } |
| 1998 | |
daniel@transgaming.com | ad6aee7 | 2012-11-28 19:33:42 +0000 | [diff] [blame] | 1999 | return false; |
| 2000 | } |
| 2001 | |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 2002 | bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source) |
daniel@transgaming.com | ad6aee7 | 2012-11-28 19:33:42 +0000 | [diff] [blame] | 2003 | { |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2004 | if (source && dest) |
| 2005 | { |
daniel@transgaming.com | e9cf5e7 | 2013-01-11 04:06:55 +0000 | [diff] [blame] | 2006 | TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance()); |
| 2007 | TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance()); |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2008 | |
daniel@transgaming.com | e9cf5e7 | 2013-01-11 04:06:55 +0000 | [diff] [blame] | 2009 | mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture()); |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2010 | return true; |
| 2011 | } |
| 2012 | |
daniel@transgaming.com | ad6aee7 | 2012-11-28 19:33:42 +0000 | [diff] [blame] | 2013 | return false; |
| 2014 | } |
| 2015 | |
shannon.woods@transgaming.com | 664916b | 2013-01-25 21:50:32 +0000 | [diff] [blame] | 2016 | 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] | 2017 | GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level) |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2018 | { |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2019 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 2020 | if (!colorbuffer) |
| 2021 | { |
| 2022 | ERR("Failed to retrieve the color buffer from the frame buffer."); |
| 2023 | return error(GL_OUT_OF_MEMORY, false); |
| 2024 | } |
| 2025 | |
| 2026 | RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); |
| 2027 | if (!sourceRenderTarget) |
| 2028 | { |
| 2029 | ERR("Failed to retrieve the render target from the frame buffer."); |
| 2030 | return error(GL_OUT_OF_MEMORY, false); |
| 2031 | } |
| 2032 | |
| 2033 | ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView(); |
| 2034 | if (!source) |
| 2035 | { |
| 2036 | ERR("Failed to retrieve the render target view from the render target."); |
| 2037 | return error(GL_OUT_OF_MEMORY, false); |
| 2038 | } |
| 2039 | |
| 2040 | TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance()); |
| 2041 | if (!storage11) |
| 2042 | { |
| 2043 | source->Release(); |
| 2044 | ERR("Failed to retrieve the texture storage from the destination."); |
| 2045 | return error(GL_OUT_OF_MEMORY, false); |
| 2046 | } |
| 2047 | |
| 2048 | RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level)); |
| 2049 | if (!destRenderTarget) |
| 2050 | { |
| 2051 | source->Release(); |
| 2052 | ERR("Failed to retrieve the render target from the destination storage."); |
| 2053 | return error(GL_OUT_OF_MEMORY, false); |
| 2054 | } |
| 2055 | |
| 2056 | ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView(); |
| 2057 | if (!dest) |
| 2058 | { |
| 2059 | source->Release(); |
| 2060 | ERR("Failed to retrieve the render target view from the destination render target."); |
| 2061 | return error(GL_OUT_OF_MEMORY, false); |
| 2062 | } |
| 2063 | |
| 2064 | gl::Rectangle destRect; |
| 2065 | destRect.x = xoffset; |
| 2066 | destRect.y = yoffset; |
| 2067 | destRect.width = sourceRect.width; |
| 2068 | destRect.height = sourceRect.height; |
| 2069 | |
| 2070 | bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), |
| 2071 | dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat); |
| 2072 | |
| 2073 | source->Release(); |
| 2074 | dest->Release(); |
| 2075 | |
| 2076 | return ret; |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
shannon.woods@transgaming.com | 664916b | 2013-01-25 21:50:32 +0000 | [diff] [blame] | 2079 | 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] | 2080 | GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level) |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2081 | { |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2082 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 2083 | if (!colorbuffer) |
| 2084 | { |
| 2085 | ERR("Failed to retrieve the color buffer from the frame buffer."); |
| 2086 | return error(GL_OUT_OF_MEMORY, false); |
| 2087 | } |
| 2088 | |
| 2089 | RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); |
| 2090 | if (!sourceRenderTarget) |
| 2091 | { |
| 2092 | ERR("Failed to retrieve the render target from the frame buffer."); |
| 2093 | return error(GL_OUT_OF_MEMORY, false); |
| 2094 | } |
| 2095 | |
| 2096 | ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView(); |
| 2097 | if (!source) |
| 2098 | { |
| 2099 | ERR("Failed to retrieve the render target view from the render target."); |
| 2100 | return error(GL_OUT_OF_MEMORY, false); |
| 2101 | } |
| 2102 | |
| 2103 | TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance()); |
| 2104 | if (!storage11) |
| 2105 | { |
| 2106 | source->Release(); |
| 2107 | ERR("Failed to retrieve the texture storage from the destination."); |
| 2108 | return error(GL_OUT_OF_MEMORY, false); |
| 2109 | } |
| 2110 | |
| 2111 | RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(target, level)); |
| 2112 | if (!destRenderTarget) |
| 2113 | { |
| 2114 | source->Release(); |
| 2115 | ERR("Failed to retrieve the render target from the destination storage."); |
| 2116 | return error(GL_OUT_OF_MEMORY, false); |
| 2117 | } |
| 2118 | |
| 2119 | ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView(); |
| 2120 | if (!dest) |
| 2121 | { |
| 2122 | source->Release(); |
| 2123 | ERR("Failed to retrieve the render target view from the destination render target."); |
| 2124 | return error(GL_OUT_OF_MEMORY, false); |
| 2125 | } |
| 2126 | |
| 2127 | gl::Rectangle destRect; |
| 2128 | destRect.x = xoffset; |
| 2129 | destRect.y = yoffset; |
| 2130 | destRect.width = sourceRect.width; |
| 2131 | destRect.height = sourceRect.height; |
| 2132 | |
| 2133 | bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), |
| 2134 | dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat); |
| 2135 | |
| 2136 | source->Release(); |
| 2137 | dest->Release(); |
| 2138 | |
| 2139 | return ret; |
| 2140 | } |
| 2141 | |
| 2142 | bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight, |
| 2143 | ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat) |
| 2144 | { |
| 2145 | HRESULT result; |
| 2146 | |
| 2147 | if (!mCopyResourcesInitialized) |
| 2148 | { |
| 2149 | ASSERT(!mCopyVB && !mCopySampler && !mCopyIL && !mCopyVS && !mCopyRGBAPS && !mCopyRGBPS && !mCopyLumPS && !mCopyLumAlphaPS); |
| 2150 | |
| 2151 | D3D11_BUFFER_DESC vbDesc; |
| 2152 | vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4; |
| 2153 | vbDesc.Usage = D3D11_USAGE_DYNAMIC; |
| 2154 | vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; |
| 2155 | vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| 2156 | vbDesc.MiscFlags = 0; |
| 2157 | vbDesc.StructureByteStride = 0; |
| 2158 | |
| 2159 | result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB); |
| 2160 | ASSERT(SUCCEEDED(result)); |
| 2161 | d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer"); |
| 2162 | |
| 2163 | D3D11_SAMPLER_DESC samplerDesc; |
| 2164 | samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; |
| 2165 | samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; |
| 2166 | samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; |
| 2167 | samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; |
| 2168 | samplerDesc.MipLODBias = 0.0f; |
| 2169 | samplerDesc.MaxAnisotropy = 0; |
| 2170 | samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; |
| 2171 | samplerDesc.BorderColor[0] = 0.0f; |
| 2172 | samplerDesc.BorderColor[1] = 0.0f; |
| 2173 | samplerDesc.BorderColor[2] = 0.0f; |
| 2174 | samplerDesc.BorderColor[3] = 0.0f; |
| 2175 | samplerDesc.MinLOD = 0.0f; |
| 2176 | samplerDesc.MaxLOD = 0.0f; |
| 2177 | |
| 2178 | result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler); |
| 2179 | ASSERT(SUCCEEDED(result)); |
| 2180 | d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler"); |
| 2181 | |
| 2182 | D3D11_INPUT_ELEMENT_DESC quadLayout[] = |
| 2183 | { |
| 2184 | { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 2185 | { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 2186 | }; |
| 2187 | |
| 2188 | result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mCopyIL); |
| 2189 | ASSERT(SUCCEEDED(result)); |
| 2190 | d3d11::SetDebugName(mCopyIL, "Renderer11 copy texture input layout"); |
| 2191 | |
| 2192 | result = mDevice->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mCopyVS); |
| 2193 | ASSERT(SUCCEEDED(result)); |
| 2194 | d3d11::SetDebugName(mCopyVS, "Renderer11 copy texture vertex shader"); |
| 2195 | |
| 2196 | result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mCopyRGBAPS); |
| 2197 | ASSERT(SUCCEEDED(result)); |
| 2198 | d3d11::SetDebugName(mCopyRGBAPS, "Renderer11 copy texture RGBA pixel shader"); |
| 2199 | |
| 2200 | result = mDevice->CreatePixelShader(g_PS_PassthroughRGB, sizeof(g_PS_PassthroughRGB), NULL, &mCopyRGBPS); |
| 2201 | ASSERT(SUCCEEDED(result)); |
| 2202 | d3d11::SetDebugName(mCopyRGBPS, "Renderer11 copy texture RGB pixel shader"); |
| 2203 | |
| 2204 | result = mDevice->CreatePixelShader(g_PS_PassthroughLum, sizeof(g_PS_PassthroughLum), NULL, &mCopyLumPS); |
| 2205 | ASSERT(SUCCEEDED(result)); |
| 2206 | d3d11::SetDebugName(mCopyLumPS, "Renderer11 copy texture luminance pixel shader"); |
| 2207 | |
| 2208 | result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha, sizeof(g_PS_PassthroughLumAlpha), NULL, &mCopyLumAlphaPS); |
| 2209 | ASSERT(SUCCEEDED(result)); |
| 2210 | d3d11::SetDebugName(mCopyLumAlphaPS, "Renderer11 copy texture luminance alpha pixel shader"); |
| 2211 | |
| 2212 | mCopyResourcesInitialized = true; |
| 2213 | } |
| 2214 | |
| 2215 | // Verify the source and destination area sizes |
| 2216 | if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) || |
| 2217 | sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) || |
| 2218 | destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) || |
| 2219 | destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight)) |
| 2220 | { |
| 2221 | return error(GL_INVALID_VALUE, false); |
| 2222 | } |
| 2223 | |
| 2224 | // Set vertices |
| 2225 | D3D11_MAPPED_SUBRESOURCE mappedResource; |
| 2226 | result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); |
| 2227 | if (FAILED(result)) |
| 2228 | { |
| 2229 | ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result); |
| 2230 | return error(GL_OUT_OF_MEMORY, false); |
| 2231 | } |
| 2232 | |
| 2233 | d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData); |
| 2234 | |
| 2235 | // Create a quad in homogeneous coordinates |
| 2236 | float x1 = (destArea.x / destWidth) * 2.0f - 1.0f; |
| 2237 | float y1 = (destArea.y / destHeight) * 2.0f - 1.0f; |
| 2238 | float x2 = ((destArea.x + destArea.width) / destWidth) * 2.0f - 1.0f; |
| 2239 | float y2 = ((destArea.y + destArea.height) / destHeight) * 2.0f - 1.0f; |
| 2240 | |
| 2241 | float u1 = sourceArea.x / float(sourceWidth); |
| 2242 | float v1 = sourceArea.y / float(sourceHeight); |
| 2243 | float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth); |
| 2244 | float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight); |
| 2245 | |
| 2246 | d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2); |
| 2247 | d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1); |
| 2248 | d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2); |
| 2249 | d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1); |
| 2250 | |
| 2251 | mDeviceContext->Unmap(mCopyVB, 0); |
| 2252 | |
| 2253 | static UINT stride = sizeof(d3d11::PositionTexCoordVertex); |
| 2254 | static UINT startIdx = 0; |
| 2255 | mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx); |
| 2256 | |
| 2257 | // Apply state |
| 2258 | static const float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; |
| 2259 | mDeviceContext->OMSetBlendState(NULL, blendFactor, 0xFFFFFFF); |
| 2260 | mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF); |
| 2261 | mDeviceContext->RSSetState(NULL); |
| 2262 | |
| 2263 | // Apply shaders |
| 2264 | mDeviceContext->IASetInputLayout(mCopyIL); |
| 2265 | mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); |
| 2266 | mDeviceContext->VSSetShader(mCopyVS, NULL, 0); |
| 2267 | |
| 2268 | ID3D11PixelShader *ps = NULL; |
| 2269 | switch(destFormat) |
| 2270 | { |
| 2271 | case GL_RGBA: ps = mCopyRGBAPS; break; |
| 2272 | case GL_RGB: ps = mCopyRGBPS; break; |
| 2273 | case GL_ALPHA: ps = mCopyRGBAPS; break; |
| 2274 | case GL_BGRA_EXT: ps = mCopyRGBAPS; break; |
| 2275 | case GL_LUMINANCE: ps = mCopyLumPS; break; |
| 2276 | case GL_LUMINANCE_ALPHA: ps = mCopyLumAlphaPS; break; |
| 2277 | default: UNREACHABLE(); ps = NULL; break; |
| 2278 | } |
| 2279 | |
| 2280 | mDeviceContext->PSSetShader(ps, NULL, 0); |
| 2281 | |
| 2282 | // Unset the currently bound shader resource to avoid conflicts |
| 2283 | static ID3D11ShaderResourceView *const nullSRV = NULL; |
| 2284 | mDeviceContext->PSSetShaderResources(0, 1, &nullSRV); |
| 2285 | |
| 2286 | // Apply render targets |
| 2287 | mDeviceContext->OMSetRenderTargets(1, &dest, NULL); |
| 2288 | |
| 2289 | // Set the viewport |
| 2290 | D3D11_VIEWPORT viewport; |
| 2291 | viewport.TopLeftX = 0; |
| 2292 | viewport.TopLeftY = 0; |
| 2293 | viewport.Width = destWidth; |
| 2294 | viewport.Height = destHeight; |
| 2295 | viewport.MinDepth = 0.0f; |
| 2296 | viewport.MaxDepth = 1.0f; |
| 2297 | mDeviceContext->RSSetViewports(1, &viewport); |
| 2298 | |
| 2299 | // Apply textures |
| 2300 | mDeviceContext->PSSetShaderResources(0, 1, &source); |
| 2301 | mDeviceContext->PSSetSamplers(0, 1, &mCopySampler); |
| 2302 | |
| 2303 | // Draw the quad |
| 2304 | mDeviceContext->Draw(4, 0); |
| 2305 | |
| 2306 | // Unbind textures and render targets and vertex buffer |
| 2307 | mDeviceContext->PSSetShaderResources(0, 1, &nullSRV); |
| 2308 | |
| 2309 | static ID3D11RenderTargetView *const nullRTV = NULL; |
| 2310 | mDeviceContext->OMSetRenderTargets(1, &nullRTV, NULL); |
| 2311 | |
| 2312 | static UINT zero = 0; |
| 2313 | static ID3D11Buffer *const nullBuffer = NULL; |
| 2314 | mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero); |
| 2315 | |
| 2316 | markAllStateDirty(); |
| 2317 | |
| 2318 | return true; |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 2321 | RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth) |
| 2322 | { |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame] | 2323 | SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain); |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 2324 | RenderTarget11 *renderTarget = NULL; |
| 2325 | if (depth) |
| 2326 | { |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame] | 2327 | renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(), NULL, |
| 2328 | swapChain11->getWidth(), swapChain11->getHeight()); |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 2329 | } |
| 2330 | else |
| 2331 | { |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame] | 2332 | renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(), |
| 2333 | swapChain11->getRenderTargetShaderResource(), |
| 2334 | swapChain11->getWidth(), swapChain11->getHeight()); |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 2335 | } |
| 2336 | return renderTarget; |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth) |
| 2340 | { |
daniel@transgaming.com | c9a501d | 2013-01-11 04:08:46 +0000 | [diff] [blame] | 2341 | RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth); |
| 2342 | return renderTarget; |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
daniel@transgaming.com | 2275f91 | 2012-12-20 21:13:22 +0000 | [diff] [blame] | 2345 | ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, GLenum type) |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 2346 | { |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2347 | ShaderExecutable11 *executable = NULL; |
| 2348 | |
| 2349 | switch (type) |
| 2350 | { |
| 2351 | case GL_VERTEX_SHADER: |
| 2352 | { |
| 2353 | ID3D11VertexShader *vshader = NULL; |
| 2354 | HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader); |
| 2355 | ASSERT(SUCCEEDED(result)); |
| 2356 | |
| 2357 | if (vshader) |
| 2358 | { |
daniel@transgaming.com | 7b18d0c | 2012-11-28 21:04:10 +0000 | [diff] [blame] | 2359 | executable = new ShaderExecutable11(function, length, vshader); |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2360 | } |
| 2361 | } |
| 2362 | break; |
| 2363 | case GL_FRAGMENT_SHADER: |
| 2364 | { |
| 2365 | ID3D11PixelShader *pshader = NULL; |
| 2366 | HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader); |
| 2367 | ASSERT(SUCCEEDED(result)); |
| 2368 | |
| 2369 | if (pshader) |
| 2370 | { |
daniel@transgaming.com | 7b18d0c | 2012-11-28 21:04:10 +0000 | [diff] [blame] | 2371 | executable = new ShaderExecutable11(function, length, pshader); |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2372 | } |
| 2373 | } |
| 2374 | break; |
| 2375 | default: |
| 2376 | UNREACHABLE(); |
| 2377 | break; |
| 2378 | } |
| 2379 | |
| 2380 | return executable; |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 2383 | ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type) |
| 2384 | { |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2385 | const char *profile = NULL; |
| 2386 | |
| 2387 | switch (type) |
| 2388 | { |
| 2389 | case GL_VERTEX_SHADER: |
| 2390 | profile = "vs_4_0"; |
| 2391 | break; |
| 2392 | case GL_FRAGMENT_SHADER: |
| 2393 | profile = "ps_4_0"; |
| 2394 | break; |
| 2395 | default: |
| 2396 | UNREACHABLE(); |
| 2397 | return NULL; |
| 2398 | } |
| 2399 | |
daniel@transgaming.com | 25e16af | 2012-11-28 21:05:57 +0000 | [diff] [blame] | 2400 | ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, false); |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2401 | if (!binary) |
| 2402 | return NULL; |
| 2403 | |
daniel@transgaming.com | 2275f91 | 2012-12-20 21:13:22 +0000 | [diff] [blame] | 2404 | ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type); |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2405 | binary->Release(); |
| 2406 | |
| 2407 | return executable; |
| 2408 | } |
| 2409 | |
daniel@transgaming.com | 3f255b4 | 2012-12-20 21:07:35 +0000 | [diff] [blame] | 2410 | VertexBuffer *Renderer11::createVertexBuffer() |
| 2411 | { |
daniel@transgaming.com | 2c4d070 | 2012-12-20 21:08:51 +0000 | [diff] [blame] | 2412 | return new VertexBuffer11(this); |
daniel@transgaming.com | 3f255b4 | 2012-12-20 21:07:35 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
daniel@transgaming.com | 0b6d774 | 2012-12-20 21:09:47 +0000 | [diff] [blame] | 2415 | IndexBuffer *Renderer11::createIndexBuffer() |
| 2416 | { |
daniel@transgaming.com | 11c2af5 | 2012-12-20 21:10:01 +0000 | [diff] [blame] | 2417 | return new IndexBuffer11(this); |
daniel@transgaming.com | 0b6d774 | 2012-12-20 21:09:47 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
shannon.woods@transgaming.com | fdeacb8 | 2013-01-25 21:52:34 +0000 | [diff] [blame] | 2420 | bool Renderer11::getRenderTargetResource(gl::Framebuffer *framebuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource) |
| 2421 | { |
| 2422 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 2423 | if (colorbuffer) |
| 2424 | { |
| 2425 | RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); |
| 2426 | if (renderTarget) |
| 2427 | { |
| 2428 | *subresourceIndex = renderTarget->getSubresourceIndex(); |
| 2429 | |
| 2430 | ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView(); |
| 2431 | if (colorBufferRTV) |
| 2432 | { |
| 2433 | ID3D11Resource *textureResource = NULL; |
| 2434 | colorBufferRTV->GetResource(&textureResource); |
| 2435 | colorBufferRTV->Release(); |
| 2436 | |
| 2437 | if (textureResource) |
| 2438 | { |
| 2439 | HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource); |
| 2440 | textureResource->Release(); |
| 2441 | |
| 2442 | if (SUCCEEDED(result)) |
| 2443 | { |
| 2444 | return true; |
| 2445 | } |
| 2446 | else |
| 2447 | { |
| 2448 | ERR("Failed to extract the ID3D11Texture2D from the render target resource, " |
| 2449 | "HRESULT: 0x%X.", result); |
| 2450 | } |
| 2451 | } |
| 2452 | } |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | return false; |
| 2457 | } |
| 2458 | |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2459 | bool Renderer11::blitRect(gl::Framebuffer *readTarget, gl::Rectangle *readRect, gl::Framebuffer *drawTarget, gl::Rectangle *drawRect, |
| 2460 | bool blitRenderTarget, bool blitDepthStencil) |
| 2461 | { |
| 2462 | // TODO |
| 2463 | UNIMPLEMENTED(); |
| 2464 | return false; |
| 2465 | } |
| 2466 | |
| 2467 | void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, |
| 2468 | GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels) |
| 2469 | { |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2470 | ID3D11Texture2D *colorBufferTexture = NULL; |
daniel@transgaming.com | 2eb7ab7 | 2013-01-11 04:10:21 +0000 | [diff] [blame] | 2471 | unsigned int subresourceIndex = 0; |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2472 | |
shannon.woods@transgaming.com | fdeacb8 | 2013-01-25 21:52:34 +0000 | [diff] [blame] | 2473 | if (getRenderTargetResource(framebuffer, &subresourceIndex, &colorBufferTexture)) |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2474 | { |
| 2475 | gl::Rectangle area; |
| 2476 | area.x = x; |
| 2477 | area.y = y; |
| 2478 | area.width = width; |
| 2479 | area.height = height; |
| 2480 | |
daniel@transgaming.com | 2eb7ab7 | 2013-01-11 04:10:21 +0000 | [diff] [blame] | 2481 | readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, |
| 2482 | packReverseRowOrder, packAlignment, pixels); |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2483 | |
| 2484 | colorBufferTexture->Release(); |
| 2485 | colorBufferTexture = NULL; |
| 2486 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
daniel@transgaming.com | 244e183 | 2012-12-20 20:52:35 +0000 | [diff] [blame] | 2489 | Image *Renderer11::createImage() |
| 2490 | { |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 2491 | return new Image11(); |
daniel@transgaming.com | 244e183 | 2012-12-20 20:52:35 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
daniel@transgaming.com | f721fdb | 2012-12-20 20:53:11 +0000 | [diff] [blame] | 2494 | void Renderer11::generateMipmap(Image *dest, Image *src) |
| 2495 | { |
shannon.woods@transgaming.com | 2b132f4 | 2013-01-25 21:52:47 +0000 | [diff] [blame] | 2496 | Image11 *dest11 = Image11::makeImage11(dest); |
| 2497 | Image11 *src11 = Image11::makeImage11(src); |
| 2498 | Image11::generateMipmap(dest11, src11); |
daniel@transgaming.com | f721fdb | 2012-12-20 20:53:11 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 2501 | TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain) |
| 2502 | { |
daniel@transgaming.com | 36670db | 2013-01-11 04:08:22 +0000 | [diff] [blame] | 2503 | SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain); |
| 2504 | return new TextureStorage11_2D(this, swapChain11); |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) |
| 2508 | { |
daniel@transgaming.com | 36670db | 2013-01-11 04:08:22 +0000 | [diff] [blame] | 2509 | return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height); |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) |
| 2513 | { |
daniel@transgaming.com | 36670db | 2013-01-11 04:08:22 +0000 | [diff] [blame] | 2514 | return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size); |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2517 | static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType) |
| 2518 | { |
| 2519 | if (sourceFormat == DXGI_FORMAT_A8_UNORM && |
| 2520 | destFormat == GL_ALPHA && |
| 2521 | destType == GL_UNSIGNED_BYTE) |
| 2522 | { |
| 2523 | return 1; |
| 2524 | } |
| 2525 | else if (sourceFormat == DXGI_FORMAT_R8G8B8A8_UNORM && |
| 2526 | destFormat == GL_RGBA && |
| 2527 | destType == GL_UNSIGNED_BYTE) |
| 2528 | { |
| 2529 | return 4; |
| 2530 | } |
| 2531 | else if (sourceFormat == DXGI_FORMAT_B8G8R8A8_UNORM && |
| 2532 | destFormat == GL_BGRA_EXT && |
| 2533 | destType == GL_UNSIGNED_BYTE) |
| 2534 | { |
| 2535 | return 4; |
| 2536 | } |
| 2537 | else if (sourceFormat == DXGI_FORMAT_R16G16B16A16_FLOAT && |
| 2538 | destFormat == GL_RGBA && |
| 2539 | destType == GL_HALF_FLOAT_OES) |
| 2540 | { |
| 2541 | return 8; |
| 2542 | } |
| 2543 | else if (sourceFormat == DXGI_FORMAT_R32G32B32_FLOAT && |
| 2544 | destFormat == GL_RGB && |
| 2545 | destType == GL_FLOAT) |
| 2546 | { |
| 2547 | return 12; |
| 2548 | } |
| 2549 | else if (sourceFormat == DXGI_FORMAT_R32G32B32A32_FLOAT && |
| 2550 | destFormat == GL_RGBA && |
| 2551 | destType == GL_FLOAT) |
| 2552 | { |
| 2553 | return 16; |
| 2554 | } |
| 2555 | else |
| 2556 | { |
| 2557 | return 0; |
| 2558 | } |
| 2559 | } |
| 2560 | |
| 2561 | static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x, |
| 2562 | unsigned int y, int inputPitch, gl::Color *outColor) |
| 2563 | { |
| 2564 | switch (format) |
| 2565 | { |
| 2566 | case DXGI_FORMAT_R8G8B8A8_UNORM: |
| 2567 | { |
| 2568 | unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch); |
| 2569 | outColor->red = (rgba & 0xFF000000) * (1.0f / 0xFF000000); |
| 2570 | outColor->green = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 2571 | outColor->blue = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 2572 | outColor->alpha = (rgba & 0x000000FF) * (1.0f / 0x000000FF); |
| 2573 | } |
| 2574 | break; |
| 2575 | |
| 2576 | case DXGI_FORMAT_A8_UNORM: |
| 2577 | { |
| 2578 | outColor->red = 0.0f; |
| 2579 | outColor->green = 0.0f; |
| 2580 | outColor->blue = 0.0f; |
| 2581 | outColor->alpha = *(data + x + y * inputPitch) / 255.0f; |
| 2582 | } |
| 2583 | break; |
| 2584 | |
| 2585 | case DXGI_FORMAT_R32G32B32A32_FLOAT: |
| 2586 | { |
| 2587 | outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0); |
| 2588 | outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1); |
| 2589 | outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2); |
| 2590 | outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3); |
| 2591 | } |
| 2592 | break; |
| 2593 | |
| 2594 | case DXGI_FORMAT_R32G32B32_FLOAT: |
| 2595 | { |
| 2596 | outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0); |
| 2597 | outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1); |
| 2598 | outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2); |
| 2599 | outColor->alpha = 1.0f; |
| 2600 | } |
| 2601 | break; |
| 2602 | |
| 2603 | case DXGI_FORMAT_R16G16B16A16_FLOAT: |
| 2604 | { |
| 2605 | outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0)); |
| 2606 | outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1)); |
| 2607 | outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2)); |
| 2608 | outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3)); |
| 2609 | } |
| 2610 | break; |
| 2611 | |
| 2612 | case DXGI_FORMAT_B8G8R8A8_UNORM: |
| 2613 | { |
| 2614 | unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch); |
| 2615 | outColor->red = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 2616 | outColor->blue = (bgra & 0xFF000000) * (1.0f / 0xFF000000); |
| 2617 | outColor->green = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 2618 | outColor->alpha = (bgra & 0x000000FF) * (1.0f / 0x000000FF); |
| 2619 | } |
| 2620 | break; |
| 2621 | |
| 2622 | case DXGI_FORMAT_R8_UNORM: |
| 2623 | { |
| 2624 | outColor->red = *(data + x + y * inputPitch) / 255.0f; |
| 2625 | outColor->green = 0.0f; |
| 2626 | outColor->blue = 0.0f; |
| 2627 | outColor->alpha = 1.0f; |
| 2628 | } |
| 2629 | break; |
| 2630 | |
| 2631 | case DXGI_FORMAT_R8G8_UNORM: |
| 2632 | { |
| 2633 | unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch); |
| 2634 | |
| 2635 | outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00); |
| 2636 | outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF); |
| 2637 | outColor->blue = 0.0f; |
| 2638 | outColor->alpha = 1.0f; |
| 2639 | } |
| 2640 | break; |
| 2641 | |
| 2642 | case DXGI_FORMAT_R16_FLOAT: |
| 2643 | { |
| 2644 | outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch)); |
| 2645 | outColor->green = 0.0f; |
| 2646 | outColor->blue = 0.0f; |
| 2647 | outColor->alpha = 1.0f; |
| 2648 | } |
| 2649 | break; |
| 2650 | |
| 2651 | case DXGI_FORMAT_R16G16_FLOAT: |
| 2652 | { |
| 2653 | outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0)); |
| 2654 | outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1)); |
| 2655 | outColor->blue = 0.0f; |
| 2656 | outColor->alpha = 1.0f; |
| 2657 | } |
| 2658 | break; |
| 2659 | |
| 2660 | default: |
| 2661 | ERR("ReadPixelColor not implemented for DXGI format %u.", format); |
| 2662 | UNIMPLEMENTED(); |
| 2663 | break; |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x, |
| 2668 | unsigned int y, int outputPitch, void *outData) |
| 2669 | { |
| 2670 | unsigned char* byteData = reinterpret_cast<unsigned char*>(outData); |
| 2671 | unsigned short* shortData = reinterpret_cast<unsigned short*>(outData); |
| 2672 | |
| 2673 | switch (format) |
| 2674 | { |
| 2675 | case GL_RGBA: |
| 2676 | switch (type) |
| 2677 | { |
| 2678 | case GL_UNSIGNED_BYTE: |
| 2679 | byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f); |
| 2680 | byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f); |
| 2681 | byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f); |
| 2682 | byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f); |
| 2683 | break; |
| 2684 | |
| 2685 | default: |
| 2686 | ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type); |
| 2687 | UNIMPLEMENTED(); |
| 2688 | break; |
| 2689 | } |
| 2690 | break; |
| 2691 | |
| 2692 | case GL_BGRA_EXT: |
| 2693 | switch (type) |
| 2694 | { |
| 2695 | case GL_UNSIGNED_BYTE: |
| 2696 | byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f); |
| 2697 | byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f); |
| 2698 | byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f); |
| 2699 | byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f); |
| 2700 | break; |
| 2701 | |
| 2702 | case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: |
| 2703 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 2704 | // this type is packed as follows: |
| 2705 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 2706 | // -------------------------------------------------------------------------------- |
| 2707 | // | 4th | 3rd | 2nd | 1st component | |
| 2708 | // -------------------------------------------------------------------------------- |
| 2709 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 2710 | shortData[x + y * outputPitch / sizeof(unsigned short)] = |
| 2711 | (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) | |
| 2712 | (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) | |
| 2713 | (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) | |
| 2714 | (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0); |
| 2715 | break; |
| 2716 | |
| 2717 | case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: |
| 2718 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 2719 | // this type is packed as follows: |
| 2720 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 2721 | // -------------------------------------------------------------------------------- |
| 2722 | // | 4th | 3rd | 2nd | 1st component | |
| 2723 | // -------------------------------------------------------------------------------- |
| 2724 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 2725 | shortData[x + y * outputPitch / sizeof(unsigned short)] = |
| 2726 | (static_cast<unsigned short>( color.alpha + 0.5f) << 15) | |
| 2727 | (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) | |
| 2728 | (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) | |
| 2729 | (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0); |
| 2730 | break; |
| 2731 | |
| 2732 | default: |
| 2733 | ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type); |
| 2734 | UNIMPLEMENTED(); |
| 2735 | break; |
| 2736 | } |
| 2737 | break; |
| 2738 | |
| 2739 | case GL_RGB: |
| 2740 | switch (type) |
| 2741 | { |
| 2742 | case GL_UNSIGNED_SHORT_5_6_5: |
| 2743 | shortData[x + y * outputPitch / sizeof(unsigned short)] = |
| 2744 | (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) | |
| 2745 | (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) | |
| 2746 | (static_cast<unsigned short>(31 * color.red + 0.5f) << 11); |
| 2747 | break; |
| 2748 | |
| 2749 | case GL_UNSIGNED_BYTE: |
| 2750 | byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f); |
| 2751 | byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f); |
| 2752 | byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f); |
| 2753 | break; |
| 2754 | |
| 2755 | default: |
| 2756 | ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type); |
| 2757 | UNIMPLEMENTED(); |
| 2758 | break; |
| 2759 | } |
| 2760 | break; |
| 2761 | |
| 2762 | default: |
| 2763 | ERR("WritePixelColor not implemented for format 0x%X.", format); |
| 2764 | UNIMPLEMENTED(); |
| 2765 | break; |
| 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, |
| 2770 | GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder, |
| 2771 | GLint packAlignment, void *pixels) |
| 2772 | { |
| 2773 | D3D11_TEXTURE2D_DESC textureDesc; |
| 2774 | texture->GetDesc(&textureDesc); |
| 2775 | |
| 2776 | D3D11_TEXTURE2D_DESC stagingDesc; |
| 2777 | stagingDesc.Width = area.width; |
| 2778 | stagingDesc.Height = area.height; |
| 2779 | stagingDesc.MipLevels = 1; |
| 2780 | stagingDesc.ArraySize = 1; |
| 2781 | stagingDesc.Format = textureDesc.Format; |
| 2782 | stagingDesc.SampleDesc.Count = 1; |
| 2783 | stagingDesc.SampleDesc.Quality = 0; |
| 2784 | stagingDesc.Usage = D3D11_USAGE_STAGING; |
| 2785 | stagingDesc.BindFlags = 0; |
| 2786 | stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; |
| 2787 | stagingDesc.MiscFlags = 0; |
| 2788 | |
| 2789 | ID3D11Texture2D* stagingTex = NULL; |
| 2790 | HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex); |
| 2791 | if (FAILED(result)) |
| 2792 | { |
| 2793 | ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result); |
| 2794 | return; |
| 2795 | } |
| 2796 | |
| 2797 | ID3D11Texture2D* srcTex = NULL; |
| 2798 | if (textureDesc.SampleDesc.Count > 1) |
| 2799 | { |
| 2800 | D3D11_TEXTURE2D_DESC resolveDesc; |
| 2801 | resolveDesc.Width = textureDesc.Width; |
| 2802 | resolveDesc.Height = textureDesc.Height; |
| 2803 | resolveDesc.MipLevels = 1; |
| 2804 | resolveDesc.ArraySize = 1; |
| 2805 | resolveDesc.Format = textureDesc.Format; |
| 2806 | resolveDesc.SampleDesc.Count = 1; |
| 2807 | resolveDesc.SampleDesc.Quality = 0; |
| 2808 | resolveDesc.Usage = D3D11_USAGE_DEFAULT; |
| 2809 | resolveDesc.BindFlags = 0; |
| 2810 | resolveDesc.CPUAccessFlags = 0; |
| 2811 | resolveDesc.MiscFlags = 0; |
| 2812 | |
| 2813 | result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex); |
| 2814 | if (FAILED(result)) |
| 2815 | { |
| 2816 | ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result); |
| 2817 | stagingTex->Release(); |
| 2818 | return; |
| 2819 | } |
| 2820 | |
| 2821 | mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format); |
| 2822 | subResource = 0; |
| 2823 | } |
| 2824 | else |
| 2825 | { |
| 2826 | srcTex = texture; |
| 2827 | srcTex->AddRef(); |
| 2828 | } |
| 2829 | |
| 2830 | D3D11_BOX srcBox; |
| 2831 | srcBox.left = area.x; |
| 2832 | srcBox.right = area.x + area.width; |
| 2833 | srcBox.top = area.y; |
| 2834 | srcBox.bottom = area.y + area.height; |
| 2835 | srcBox.front = 0; |
| 2836 | srcBox.back = 1; |
| 2837 | |
| 2838 | mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox); |
| 2839 | |
| 2840 | srcTex->Release(); |
| 2841 | srcTex = NULL; |
| 2842 | |
| 2843 | D3D11_MAPPED_SUBRESOURCE mapping; |
| 2844 | mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping); |
| 2845 | |
| 2846 | unsigned char *source; |
| 2847 | int inputPitch; |
| 2848 | if (packReverseRowOrder) |
| 2849 | { |
| 2850 | source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1); |
| 2851 | inputPitch = -static_cast<int>(mapping.RowPitch); |
| 2852 | } |
| 2853 | else |
| 2854 | { |
| 2855 | source = static_cast<unsigned char*>(mapping.pData); |
| 2856 | inputPitch = static_cast<int>(mapping.RowPitch); |
| 2857 | } |
| 2858 | |
| 2859 | unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type); |
| 2860 | if (fastPixelSize != 0) |
| 2861 | { |
| 2862 | unsigned char *dest = static_cast<unsigned char*>(pixels); |
| 2863 | for (int j = 0; j < area.height; j++) |
| 2864 | { |
| 2865 | memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize); |
| 2866 | } |
| 2867 | } |
| 2868 | else |
| 2869 | { |
| 2870 | gl::Color pixelColor; |
| 2871 | for (int j = 0; j < area.height; j++) |
| 2872 | { |
| 2873 | for (int i = 0; i < area.width; i++) |
| 2874 | { |
| 2875 | readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor); |
| 2876 | writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels); |
| 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | |
| 2881 | mDeviceContext->Unmap(stagingTex, 0); |
| 2882 | |
| 2883 | stagingTex->Release(); |
| 2884 | stagingTex = NULL; |
| 2885 | } |
| 2886 | |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2887 | } |