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