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