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