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