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