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