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