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" |
shannon.woods@transgaming.com | 4e52b63 | 2013-02-28 23:08:01 +0000 | [diff] [blame] | 25 | #include "libGLESv2/renderer/BufferStorage11.h" |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 26 | #include "libGLESv2/renderer/VertexDataManager.h" |
| 27 | #include "libGLESv2/renderer/IndexDataManager.h" |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 28 | #include "libGLESv2/renderer/TextureStorage11.h" |
shannon.woods@transgaming.com | 8b7606a | 2013-02-28 23:03:47 +0000 | [diff] [blame] | 29 | #include "libGLESv2/renderer/Query11.h" |
shannon.woods@transgaming.com | be58aa0 | 2013-02-28 23:03:55 +0000 | [diff] [blame] | 30 | #include "libGLESv2/renderer/Fence11.h" |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 31 | |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 32 | #include "libGLESv2/renderer/shaders/compiled/passthrough11vs.h" |
| 33 | #include "libGLESv2/renderer/shaders/compiled/passthroughrgba11ps.h" |
| 34 | #include "libGLESv2/renderer/shaders/compiled/passthroughrgb11ps.h" |
| 35 | #include "libGLESv2/renderer/shaders/compiled/passthroughlum11ps.h" |
| 36 | #include "libGLESv2/renderer/shaders/compiled/passthroughlumalpha11ps.h" |
| 37 | |
shannon.woods@transgaming.com | 34f507c | 2013-01-25 21:52:15 +0000 | [diff] [blame] | 38 | #include "libGLESv2/renderer/shaders/compiled/clear11vs.h" |
| 39 | #include "libGLESv2/renderer/shaders/compiled/clear11ps.h" |
| 40 | |
daniel@transgaming.com | ca1ac1f | 2013-01-11 04:13:05 +0000 | [diff] [blame] | 41 | #include <sstream> |
| 42 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 43 | namespace rx |
| 44 | { |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 45 | static const DXGI_FORMAT RenderTargetFormats[] = |
| 46 | { |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame] | 47 | DXGI_FORMAT_B8G8R8A8_UNORM, |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 48 | DXGI_FORMAT_R8G8B8A8_UNORM |
| 49 | }; |
| 50 | |
| 51 | static const DXGI_FORMAT DepthStencilFormats[] = |
| 52 | { |
shannon.woods@transgaming.com | eec5c63 | 2013-02-28 23:04:21 +0000 | [diff] [blame] | 53 | DXGI_FORMAT_UNKNOWN, |
| 54 | DXGI_FORMAT_D24_UNORM_S8_UINT, |
| 55 | DXGI_FORMAT_D16_UNORM |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 56 | }; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 57 | |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 58 | enum |
| 59 | { |
| 60 | MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16 |
| 61 | }; |
| 62 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 63 | Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc) |
| 64 | { |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 65 | mVertexDataManager = NULL; |
| 66 | mIndexDataManager = NULL; |
| 67 | |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 68 | mLineLoopIB = NULL; |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 69 | mTriangleFanIB = NULL; |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 70 | |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 71 | mCopyResourcesInitialized = false; |
| 72 | mCopyVB = NULL; |
| 73 | mCopySampler = NULL; |
| 74 | mCopyIL = NULL; |
| 75 | mCopyVS = NULL; |
| 76 | mCopyRGBAPS = NULL; |
| 77 | mCopyRGBPS = NULL; |
| 78 | mCopyLumPS = NULL; |
| 79 | mCopyLumAlphaPS = NULL; |
| 80 | |
shannon.woods@transgaming.com | 34f507c | 2013-01-25 21:52:15 +0000 | [diff] [blame] | 81 | mClearResourcesInitialized = false; |
| 82 | mClearVB = NULL; |
| 83 | mClearIL = NULL; |
| 84 | mClearVS = NULL; |
| 85 | mClearPS = NULL; |
| 86 | mClearScissorRS = NULL; |
| 87 | mClearNoScissorRS = NULL; |
| 88 | |
daniel@transgaming.com | bdf787f | 2013-02-01 03:20:36 +0000 | [diff] [blame] | 89 | mSyncQuery = NULL; |
| 90 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 91 | mD3d11Module = NULL; |
| 92 | mDxgiModule = NULL; |
| 93 | |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 94 | mDeviceLost = false; |
| 95 | |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 96 | mMaxSupportedSamples = 0; |
| 97 | |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 98 | mDevice = NULL; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 99 | mDeviceContext = NULL; |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 100 | mDxgiAdapter = NULL; |
| 101 | mDxgiFactory = NULL; |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 102 | |
| 103 | mDriverConstantBufferVS = NULL; |
| 104 | mDriverConstantBufferPS = NULL; |
shannon.woods@transgaming.com | bec04bf | 2013-01-25 21:53:52 +0000 | [diff] [blame] | 105 | |
| 106 | mBGRATextureSupport = false; |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 107 | |
| 108 | mIsGeometryShaderActive = false; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | Renderer11::~Renderer11() |
| 112 | { |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 113 | release(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 114 | } |
| 115 | |
daniel@transgaming.com | b64ed28 | 2012-11-28 20:54:02 +0000 | [diff] [blame] | 116 | Renderer11 *Renderer11::makeRenderer11(Renderer *renderer) |
| 117 | { |
apatrick@chromium.org | 8b400b1 | 2013-01-30 21:53:40 +0000 | [diff] [blame] | 118 | ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer)); |
daniel@transgaming.com | b64ed28 | 2012-11-28 20:54:02 +0000 | [diff] [blame] | 119 | return static_cast<rx::Renderer11*>(renderer); |
| 120 | } |
| 121 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 122 | EGLint Renderer11::initialize() |
| 123 | { |
daniel@transgaming.com | 25e16af | 2012-11-28 21:05:57 +0000 | [diff] [blame] | 124 | if (!initializeCompiler()) |
| 125 | { |
| 126 | return EGL_NOT_INITIALIZED; |
| 127 | } |
| 128 | |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 129 | mDxgiModule = LoadLibrary(TEXT("dxgi.dll")); |
| 130 | mD3d11Module = LoadLibrary(TEXT("d3d11.dll")); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 131 | |
| 132 | if (mD3d11Module == NULL || mDxgiModule == NULL) |
| 133 | { |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 134 | ERR("Could not load D3D11 or DXGI library - aborting!\n"); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 135 | return EGL_NOT_INITIALIZED; |
| 136 | } |
| 137 | |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 138 | // create the D3D11 device |
| 139 | ASSERT(mDevice == NULL); |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 140 | 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] | 141 | |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 142 | if (D3D11CreateDevice == NULL) |
| 143 | { |
| 144 | ERR("Could not retrieve D3D11CreateDevice address - aborting!\n"); |
| 145 | return EGL_NOT_INITIALIZED; |
| 146 | } |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 147 | |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 148 | D3D_FEATURE_LEVEL featureLevel[] = |
| 149 | { |
| 150 | D3D_FEATURE_LEVEL_11_0, |
| 151 | D3D_FEATURE_LEVEL_10_1, |
| 152 | D3D_FEATURE_LEVEL_10_0, |
| 153 | }; |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 154 | |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 155 | HRESULT result = D3D11CreateDevice(NULL, |
| 156 | D3D_DRIVER_TYPE_HARDWARE, |
| 157 | NULL, |
daniel@transgaming.com | a60160b | 2012-11-28 19:41:15 +0000 | [diff] [blame] | 158 | #if defined(_DEBUG) |
| 159 | D3D11_CREATE_DEVICE_DEBUG, |
| 160 | #else |
| 161 | 0, |
| 162 | #endif |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 163 | featureLevel, |
| 164 | sizeof(featureLevel)/sizeof(featureLevel[0]), |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 165 | D3D11_SDK_VERSION, |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 166 | &mDevice, |
| 167 | &mFeatureLevel, |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 168 | &mDeviceContext); |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 169 | |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 170 | if (!mDevice || FAILED(result)) |
daniel@transgaming.com | c1e2634 | 2012-11-28 19:31:16 +0000 | [diff] [blame] | 171 | { |
| 172 | ERR("Could not create D3D11 device - aborting!\n"); |
| 173 | return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer |
| 174 | } |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 175 | |
| 176 | IDXGIDevice *dxgiDevice = NULL; |
| 177 | result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice); |
| 178 | |
| 179 | if (FAILED(result)) |
| 180 | { |
| 181 | ERR("Could not query DXGI device - aborting!\n"); |
| 182 | return EGL_NOT_INITIALIZED; |
| 183 | } |
| 184 | |
| 185 | result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter); |
| 186 | |
| 187 | if (FAILED(result)) |
| 188 | { |
| 189 | ERR("Could not retrieve DXGI adapter - aborting!\n"); |
| 190 | return EGL_NOT_INITIALIZED; |
| 191 | } |
| 192 | |
| 193 | dxgiDevice->Release(); |
| 194 | |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 195 | mDxgiAdapter->GetDesc(&mAdapterDescription); |
| 196 | memset(mDescription, 0, sizeof(mDescription)); |
| 197 | wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1); |
| 198 | |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 199 | result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory); |
| 200 | |
| 201 | if (!mDxgiFactory || FAILED(result)) |
| 202 | { |
| 203 | ERR("Could not create DXGI factory - aborting!\n"); |
| 204 | return EGL_NOT_INITIALIZED; |
| 205 | } |
| 206 | |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 207 | unsigned int maxSupportedSamples = 0; |
| 208 | unsigned int rtFormatCount = sizeof(RenderTargetFormats) / sizeof(DXGI_FORMAT); |
| 209 | unsigned int dsFormatCount = sizeof(DepthStencilFormats) / sizeof(DXGI_FORMAT); |
| 210 | for (unsigned int i = 0; i < rtFormatCount + dsFormatCount; ++i) |
| 211 | { |
| 212 | DXGI_FORMAT format = (i < rtFormatCount) ? RenderTargetFormats[i] : DepthStencilFormats[i - rtFormatCount]; |
| 213 | if (format != DXGI_FORMAT_UNKNOWN) |
| 214 | { |
| 215 | UINT formatSupport; |
| 216 | result = mDevice->CheckFormatSupport(format, &formatSupport); |
| 217 | if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET)) |
| 218 | { |
| 219 | MultisampleSupportInfo supportInfo; |
| 220 | |
| 221 | for (unsigned int j = 1; j <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; j++) |
| 222 | { |
| 223 | result = mDevice->CheckMultisampleQualityLevels(format, j, &supportInfo.qualityLevels[j - 1]); |
| 224 | if (SUCCEEDED(result) && supportInfo.qualityLevels[j - 1] > 0) |
| 225 | { |
| 226 | maxSupportedSamples = std::max(j, maxSupportedSamples); |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | supportInfo.qualityLevels[j - 1] = 0; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | mMultisampleSupportMap.insert(std::make_pair(format, supportInfo)); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | mMaxSupportedSamples = maxSupportedSamples; |
| 239 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 240 | initializeDevice(); |
| 241 | |
shannon.woods@transgaming.com | bec04bf | 2013-01-25 21:53:52 +0000 | [diff] [blame] | 242 | // BGRA texture support is optional in feature levels 10 and 10_1 |
| 243 | UINT formatSupport; |
| 244 | result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport); |
| 245 | if (FAILED(result)) |
| 246 | { |
| 247 | ERR("Error checking BGRA format support: 0x%08X", result); |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET); |
| 252 | mBGRATextureSupport = (formatSupport & flags) == flags; |
| 253 | } |
| 254 | |
shannon.woods@transgaming.com | 9cdced6 | 2013-02-28 23:07:31 +0000 | [diff] [blame] | 255 | // Check floating point texture support |
| 256 | static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE; |
| 257 | static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET; |
| 258 | static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE; |
| 259 | |
| 260 | DXGI_FORMAT float16Formats[] = |
| 261 | { |
| 262 | DXGI_FORMAT_R16_FLOAT, |
| 263 | DXGI_FORMAT_R16G16_FLOAT, |
| 264 | DXGI_FORMAT_R16G16B16A16_FLOAT, |
| 265 | }; |
| 266 | |
| 267 | DXGI_FORMAT float32Formats[] = |
| 268 | { |
| 269 | DXGI_FORMAT_R32_FLOAT, |
| 270 | DXGI_FORMAT_R32G32_FLOAT, |
| 271 | DXGI_FORMAT_R32G32B32_FLOAT, |
| 272 | DXGI_FORMAT_R32G32B32A32_FLOAT, |
| 273 | }; |
| 274 | |
| 275 | mFloat16TextureSupport = true; |
| 276 | mFloat16FilterSupport = true; |
| 277 | mFloat16RenderSupport = true; |
| 278 | for (unsigned int i = 0; i < ArraySize(float16Formats); i++) |
| 279 | { |
| 280 | if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport))) |
| 281 | { |
| 282 | mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags; |
| 283 | mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags; |
| 284 | mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags; |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | mFloat16TextureSupport = false; |
| 289 | mFloat16RenderSupport = false; |
| 290 | mFloat16FilterSupport = false; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | mFloat32TextureSupport = true; |
| 295 | mFloat32FilterSupport = true; |
| 296 | mFloat32RenderSupport = true; |
| 297 | for (unsigned int i = 0; i < ArraySize(float32Formats); i++) |
| 298 | { |
| 299 | if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport))) |
| 300 | { |
| 301 | mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags; |
| 302 | mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags; |
| 303 | mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | mFloat32TextureSupport = false; |
| 308 | mFloat32FilterSupport = false; |
| 309 | mFloat32RenderSupport = false; |
| 310 | } |
| 311 | } |
| 312 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 313 | return EGL_SUCCESS; |
| 314 | } |
| 315 | |
| 316 | // do any one-time device initialization |
| 317 | // NOTE: this is also needed after a device lost/reset |
| 318 | // to reset the scene status and ensure the default states are reset. |
| 319 | void Renderer11::initializeDevice() |
| 320 | { |
daniel@transgaming.com | f8ba109 | 2012-11-28 19:37:53 +0000 | [diff] [blame] | 321 | mStateCache.initialize(mDevice); |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 322 | mInputLayoutCache.initialize(mDevice, mDeviceContext); |
| 323 | |
| 324 | ASSERT(!mVertexDataManager && !mIndexDataManager); |
| 325 | mVertexDataManager = new VertexDataManager(this); |
| 326 | mIndexDataManager = new IndexDataManager(this); |
daniel@transgaming.com | f8ba109 | 2012-11-28 19:37:53 +0000 | [diff] [blame] | 327 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 328 | markAllStateDirty(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 329 | } |
| 330 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 331 | int Renderer11::generateConfigs(ConfigDesc **configDescList) |
| 332 | { |
daniel@transgaming.com | e3e826d | 2012-11-28 19:42:35 +0000 | [diff] [blame] | 333 | unsigned int numRenderFormats = sizeof(RenderTargetFormats) / sizeof(RenderTargetFormats[0]); |
| 334 | unsigned int numDepthFormats = sizeof(DepthStencilFormats) / sizeof(DepthStencilFormats[0]); |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 335 | (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats]; |
| 336 | int numConfigs = 0; |
| 337 | |
daniel@transgaming.com | e3e826d | 2012-11-28 19:42:35 +0000 | [diff] [blame] | 338 | for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++) |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 339 | { |
daniel@transgaming.com | e3e826d | 2012-11-28 19:42:35 +0000 | [diff] [blame] | 340 | for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++) |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 341 | { |
| 342 | DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex]; |
| 343 | |
| 344 | UINT formatSupport = 0; |
| 345 | HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport); |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 346 | |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 347 | if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET)) |
| 348 | { |
| 349 | DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex]; |
| 350 | |
shannon.woods@transgaming.com | eec5c63 | 2013-02-28 23:04:21 +0000 | [diff] [blame] | 351 | bool depthStencilFormatOK = true; |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 352 | |
shannon.woods@transgaming.com | eec5c63 | 2013-02-28 23:04:21 +0000 | [diff] [blame] | 353 | if (depthStencilFormat != DXGI_FORMAT_UNKNOWN) |
| 354 | { |
| 355 | UINT formatSupport = 0; |
| 356 | result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport); |
| 357 | depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL); |
| 358 | } |
| 359 | |
| 360 | if (depthStencilFormatOK) |
daniel@transgaming.com | 65e6537 | 2012-11-28 19:33:50 +0000 | [diff] [blame] | 361 | { |
| 362 | ConfigDesc newConfig; |
| 363 | newConfig.renderTargetFormat = d3d11_gl::ConvertBackBufferFormat(renderTargetFormat); |
| 364 | newConfig.depthStencilFormat = d3d11_gl::ConvertDepthStencilFormat(depthStencilFormat); |
| 365 | newConfig.multiSample = 0; // FIXME: enumerate multi-sampling |
| 366 | newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast |
| 367 | |
| 368 | (*configDescList)[numConfigs++] = newConfig; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | return numConfigs; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | void Renderer11::deleteConfigs(ConfigDesc *configDescList) |
| 378 | { |
| 379 | delete [] (configDescList); |
| 380 | } |
| 381 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 382 | void Renderer11::sync(bool block) |
| 383 | { |
daniel@transgaming.com | bdf787f | 2013-02-01 03:20:36 +0000 | [diff] [blame] | 384 | if (block) |
| 385 | { |
| 386 | HRESULT result; |
| 387 | |
| 388 | if (!mSyncQuery) |
| 389 | { |
| 390 | D3D11_QUERY_DESC queryDesc; |
| 391 | queryDesc.Query = D3D11_QUERY_EVENT; |
| 392 | queryDesc.MiscFlags = 0; |
| 393 | |
| 394 | result = mDevice->CreateQuery(&queryDesc, &mSyncQuery); |
| 395 | ASSERT(SUCCEEDED(result)); |
| 396 | } |
| 397 | |
| 398 | mDeviceContext->End(mSyncQuery); |
| 399 | mDeviceContext->Flush(); |
| 400 | |
| 401 | do |
| 402 | { |
| 403 | result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH); |
| 404 | |
| 405 | // Keep polling, but allow other threads to do something useful first |
| 406 | Sleep(0); |
| 407 | |
| 408 | if (testDeviceLost(true)) |
| 409 | { |
| 410 | return; |
| 411 | } |
| 412 | } |
| 413 | while (result == S_FALSE); |
| 414 | } |
| 415 | else |
| 416 | { |
| 417 | mDeviceContext->Flush(); |
| 418 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 419 | } |
| 420 | |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 421 | SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat) |
| 422 | { |
daniel@transgaming.com | a60160b | 2012-11-28 19:41:15 +0000 | [diff] [blame] | 423 | return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat); |
daniel@transgaming.com | b9bb279 | 2012-11-28 19:36:49 +0000 | [diff] [blame] | 424 | } |
| 425 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 426 | void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState) |
| 427 | { |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 428 | if (type == gl::SAMPLER_PIXEL) |
| 429 | { |
| 430 | if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS) |
| 431 | { |
| 432 | ERR("Pixel shader sampler index %i is not valid.", index); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0) |
| 437 | { |
| 438 | ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState); |
| 439 | |
| 440 | if (!dxSamplerState) |
| 441 | { |
| 442 | ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default" |
| 443 | "sampler state for pixel shaders at slot %i.", index); |
| 444 | } |
| 445 | |
| 446 | mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState); |
| 447 | |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 448 | mCurPixelSamplerStates[index] = samplerState; |
| 449 | } |
| 450 | |
| 451 | mForceSetPixelSamplerStates[index] = false; |
| 452 | } |
| 453 | else if (type == gl::SAMPLER_VERTEX) |
| 454 | { |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 455 | if (index < 0 || index >= (int)getMaxVertexTextureImageUnits()) |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 456 | { |
| 457 | ERR("Vertex shader sampler index %i is not valid.", index); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0) |
| 462 | { |
| 463 | ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState); |
| 464 | |
| 465 | if (!dxSamplerState) |
| 466 | { |
| 467 | ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default" |
| 468 | "sampler state for vertex shaders at slot %i.", index); |
| 469 | } |
| 470 | |
| 471 | mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState); |
| 472 | |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 473 | mCurVertexSamplerStates[index] = samplerState; |
| 474 | } |
| 475 | |
| 476 | mForceSetVertexSamplerStates[index] = false; |
| 477 | } |
| 478 | else UNREACHABLE(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture) |
| 482 | { |
daniel@transgaming.com | 0785fad | 2013-01-11 04:08:10 +0000 | [diff] [blame] | 483 | ID3D11ShaderResourceView *textureSRV = NULL; |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 484 | unsigned int serial = 0; |
| 485 | bool forceSetTexture = false; |
daniel@transgaming.com | 0785fad | 2013-01-11 04:08:10 +0000 | [diff] [blame] | 486 | |
| 487 | if (texture) |
| 488 | { |
| 489 | TextureStorageInterface *texStorage = texture->getNativeTexture(); |
| 490 | if (texStorage) |
| 491 | { |
| 492 | TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance()); |
| 493 | textureSRV = storage11->getSRV(); |
| 494 | } |
| 495 | |
| 496 | // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly |
| 497 | // missing the shader resource view |
| 498 | ASSERT(textureSRV != NULL); |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 499 | |
| 500 | serial = texture->getTextureSerial(); |
| 501 | forceSetTexture = texture->hasDirtyImages(); |
daniel@transgaming.com | 0785fad | 2013-01-11 04:08:10 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | if (type == gl::SAMPLER_PIXEL) |
| 505 | { |
| 506 | if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS) |
| 507 | { |
| 508 | ERR("Pixel shader sampler index %i is not valid.", index); |
| 509 | return; |
| 510 | } |
| 511 | |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 512 | if (forceSetTexture || mCurPixelTextureSerials[index] != serial) |
| 513 | { |
| 514 | mDeviceContext->PSSetShaderResources(index, 1, &textureSRV); |
| 515 | } |
| 516 | |
| 517 | mCurPixelTextureSerials[index] = serial; |
daniel@transgaming.com | 0785fad | 2013-01-11 04:08:10 +0000 | [diff] [blame] | 518 | } |
| 519 | else if (type == gl::SAMPLER_VERTEX) |
| 520 | { |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 521 | if (index < 0 || index >= (int)getMaxVertexTextureImageUnits()) |
daniel@transgaming.com | 0785fad | 2013-01-11 04:08:10 +0000 | [diff] [blame] | 522 | { |
| 523 | ERR("Vertex shader sampler index %i is not valid.", index); |
| 524 | return; |
| 525 | } |
| 526 | |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 527 | if (forceSetTexture || mCurVertexTextureSerials[index] != serial) |
| 528 | { |
| 529 | mDeviceContext->VSSetShaderResources(index, 1, &textureSRV); |
| 530 | } |
| 531 | |
| 532 | mCurVertexTextureSerials[index] = serial; |
daniel@transgaming.com | 0785fad | 2013-01-11 04:08:10 +0000 | [diff] [blame] | 533 | } |
| 534 | else UNREACHABLE(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 535 | } |
| 536 | |
daniel@transgaming.com | 237bc7e | 2012-11-28 21:01:06 +0000 | [diff] [blame] | 537 | void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 538 | { |
daniel@transgaming.com | 237bc7e | 2012-11-28 21:01:06 +0000 | [diff] [blame] | 539 | if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0) |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 540 | { |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 541 | ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled, |
| 542 | mCurDepthSize); |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 543 | if (!dxRasterState) |
| 544 | { |
daniel@transgaming.com | 0f9b320 | 2013-01-11 04:08:05 +0000 | [diff] [blame] | 545 | ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default" |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 546 | "rasterizer state."); |
| 547 | } |
| 548 | |
| 549 | mDeviceContext->RSSetState(dxRasterState); |
| 550 | |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 551 | mCurRasterState = rasterState; |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | mForceSetRasterState = false; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, |
| 558 | unsigned int sampleMask) |
| 559 | { |
daniel@transgaming.com | f8ba109 | 2012-11-28 19:37:53 +0000 | [diff] [blame] | 560 | if (mForceSetBlendState || |
| 561 | memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 || |
| 562 | memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0 || |
| 563 | sampleMask != mCurSampleMask) |
| 564 | { |
| 565 | ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState); |
| 566 | if (!dxBlendState) |
| 567 | { |
| 568 | ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default " |
| 569 | "blend state."); |
| 570 | } |
| 571 | |
| 572 | const float blendColors[] = { blendColor.red, blendColor.green, blendColor.blue, blendColor.alpha }; |
| 573 | mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask); |
| 574 | |
daniel@transgaming.com | f8ba109 | 2012-11-28 19:37:53 +0000 | [diff] [blame] | 575 | mCurBlendState = blendState; |
| 576 | mCurBlendColor = blendColor; |
| 577 | mCurSampleMask = sampleMask; |
| 578 | } |
| 579 | |
| 580 | mForceSetBlendState = false; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 581 | } |
| 582 | |
daniel@transgaming.com | 08c331d | 2012-11-28 19:38:39 +0000 | [diff] [blame] | 583 | void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef, |
daniel@transgaming.com | 3a0ef48 | 2012-11-28 21:01:20 +0000 | [diff] [blame] | 584 | int stencilBackRef, bool frontFaceCCW) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 585 | { |
daniel@transgaming.com | 5503fd0 | 2012-11-28 19:38:57 +0000 | [diff] [blame] | 586 | if (mForceSetDepthStencilState || |
| 587 | memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 || |
| 588 | stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef) |
| 589 | { |
| 590 | if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask || |
| 591 | stencilRef != stencilBackRef || |
| 592 | depthStencilState.stencilMask != depthStencilState.stencilBackMask) |
| 593 | { |
| 594 | ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are " |
| 595 | "invalid under WebGL."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 596 | return gl::error(GL_INVALID_OPERATION); |
daniel@transgaming.com | 5503fd0 | 2012-11-28 19:38:57 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState); |
| 600 | if (!dxDepthStencilState) |
| 601 | { |
| 602 | ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, " |
| 603 | "setting the default depth stencil state."); |
| 604 | } |
| 605 | |
| 606 | mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef)); |
| 607 | |
daniel@transgaming.com | 5503fd0 | 2012-11-28 19:38:57 +0000 | [diff] [blame] | 608 | mCurDepthStencilState = depthStencilState; |
| 609 | mCurStencilRef = stencilRef; |
| 610 | mCurStencilBackRef = stencilBackRef; |
| 611 | } |
| 612 | |
| 613 | mForceSetDepthStencilState = false; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 614 | } |
| 615 | |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 616 | void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 617 | { |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 618 | if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 || |
| 619 | enabled != mScissorEnabled) |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 620 | { |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 621 | if (enabled) |
| 622 | { |
| 623 | D3D11_RECT rect; |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 624 | rect.left = std::max(0, scissor.x); |
| 625 | rect.top = std::max(0, scissor.y); |
| 626 | rect.right = scissor.x + std::max(0, scissor.width); |
| 627 | rect.bottom = scissor.y + std::max(0, scissor.height); |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 628 | |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 629 | mDeviceContext->RSSetScissorRects(1, &rect); |
| 630 | } |
| 631 | |
| 632 | if (enabled != mScissorEnabled) |
| 633 | { |
| 634 | mForceSetRasterState = true; |
| 635 | } |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 636 | |
| 637 | mCurScissor = scissor; |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 638 | mScissorEnabled = enabled; |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | mForceSetScissor = false; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 642 | } |
| 643 | |
daniel@transgaming.com | 1298518 | 2012-12-20 20:56:31 +0000 | [diff] [blame] | 644 | bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace, |
shannon.woods@transgaming.com | 0b236e2 | 2013-01-25 21:57:07 +0000 | [diff] [blame] | 645 | bool ignoreViewport) |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 646 | { |
daniel@transgaming.com | 4c4ce23 | 2012-11-28 21:01:40 +0000 | [diff] [blame] | 647 | gl::Rectangle actualViewport = viewport; |
| 648 | float actualZNear = gl::clamp01(zNear); |
| 649 | float actualZFar = gl::clamp01(zFar); |
| 650 | if (ignoreViewport) |
| 651 | { |
| 652 | actualViewport.x = 0; |
| 653 | actualViewport.y = 0; |
| 654 | actualViewport.width = mRenderTargetDesc.width; |
| 655 | actualViewport.height = mRenderTargetDesc.height; |
| 656 | actualZNear = 0.0f; |
| 657 | actualZFar = 1.0f; |
| 658 | } |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 659 | |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 660 | // Get D3D viewport bounds, which depends on the feature level |
| 661 | const Range& viewportBounds = getViewportBounds(); |
| 662 | |
| 663 | // Clamp width and height first to the gl maximum, then clamp further if we extend past the D3D maximum bounds |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 664 | D3D11_VIEWPORT dxViewport; |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 665 | dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end); |
| 666 | dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end); |
| 667 | dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension()); |
| 668 | dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension()); |
| 669 | dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX)); |
| 670 | dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY)); |
daniel@transgaming.com | 4c4ce23 | 2012-11-28 21:01:40 +0000 | [diff] [blame] | 671 | dxViewport.MinDepth = actualZNear; |
| 672 | dxViewport.MaxDepth = actualZFar; |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 673 | |
| 674 | if (dxViewport.Width <= 0 || dxViewport.Height <= 0) |
| 675 | { |
| 676 | return false; // Nothing to render |
| 677 | } |
| 678 | |
daniel@transgaming.com | ed36abd | 2013-01-11 21:15:58 +0000 | [diff] [blame] | 679 | bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 || |
| 680 | actualZNear != mCurNear || actualZFar != mCurFar; |
daniel@transgaming.com | 4c4ce23 | 2012-11-28 21:01:40 +0000 | [diff] [blame] | 681 | |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 682 | if (viewportChanged) |
| 683 | { |
| 684 | mDeviceContext->RSSetViewports(1, &dxViewport); |
| 685 | |
daniel@transgaming.com | 4c4ce23 | 2012-11-28 21:01:40 +0000 | [diff] [blame] | 686 | mCurViewport = actualViewport; |
| 687 | mCurNear = actualZNear; |
| 688 | mCurFar = actualZFar; |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 689 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 690 | mVertexConstants.halfPixelSize[0] = 0.0f; |
| 691 | mVertexConstants.halfPixelSize[1] = 0.0f; |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 692 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 693 | mPixelConstants.coord[0] = actualViewport.width * 0.5f; |
| 694 | mPixelConstants.coord[1] = actualViewport.height * 0.5f; |
| 695 | mPixelConstants.coord[2] = actualViewport.x + (actualViewport.width * 0.5f); |
| 696 | mPixelConstants.coord[3] = actualViewport.y + (actualViewport.height * 0.5f); |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 697 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 698 | mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f; |
| 699 | mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f; |
| 700 | mPixelConstants.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);; |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 701 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 702 | mVertexConstants.depthRange[0] = actualZNear; |
| 703 | mVertexConstants.depthRange[1] = actualZFar; |
| 704 | mVertexConstants.depthRange[2] = actualZFar - actualZNear; |
daniel@transgaming.com | ed36abd | 2013-01-11 21:15:58 +0000 | [diff] [blame] | 705 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 706 | mPixelConstants.depthRange[0] = actualZNear; |
| 707 | mPixelConstants.depthRange[1] = actualZFar; |
| 708 | mPixelConstants.depthRange[2] = actualZFar - actualZNear; |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | mForceSetViewport = false; |
daniel@transgaming.com | 83e80ee | 2012-11-28 19:40:53 +0000 | [diff] [blame] | 712 | return true; |
| 713 | } |
| 714 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 715 | bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count) |
| 716 | { |
daniel@transgaming.com | c52be63 | 2012-11-28 21:04:28 +0000 | [diff] [blame] | 717 | D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 718 | |
daniel@transgaming.com | 0b03b06 | 2012-11-28 21:03:49 +0000 | [diff] [blame] | 719 | switch (mode) |
| 720 | { |
daniel@transgaming.com | c52be63 | 2012-11-28 21:04:28 +0000 | [diff] [blame] | 721 | case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break; |
| 722 | case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break; |
daniel@transgaming.com | 1ef0967 | 2013-01-11 04:10:37 +0000 | [diff] [blame] | 723 | case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break; |
daniel@transgaming.com | c52be63 | 2012-11-28 21:04:28 +0000 | [diff] [blame] | 724 | case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break; |
| 725 | case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break; |
| 726 | case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break; |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 727 | // emulate fans via rewriting index buffer |
| 728 | case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break; |
daniel@transgaming.com | 0b03b06 | 2012-11-28 21:03:49 +0000 | [diff] [blame] | 729 | default: |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 730 | return gl::error(GL_INVALID_ENUM, false); |
daniel@transgaming.com | 0b03b06 | 2012-11-28 21:03:49 +0000 | [diff] [blame] | 731 | } |
| 732 | |
daniel@transgaming.com | c52be63 | 2012-11-28 21:04:28 +0000 | [diff] [blame] | 733 | mDeviceContext->IASetPrimitiveTopology(primitiveTopology); |
daniel@transgaming.com | 0b03b06 | 2012-11-28 21:03:49 +0000 | [diff] [blame] | 734 | |
| 735 | return count > 0; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer) |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 739 | { |
daniel@transgaming.com | 80fc332 | 2012-11-28 21:02:13 +0000 | [diff] [blame] | 740 | // Get the color render buffer and serial |
| 741 | gl::Renderbuffer *renderbufferObject = NULL; |
| 742 | unsigned int renderTargetSerial = 0; |
| 743 | if (framebuffer->getColorbufferType() != GL_NONE) |
| 744 | { |
| 745 | renderbufferObject = framebuffer->getColorbuffer(); |
daniel@transgaming.com | dcf1e67 | 2012-11-28 19:38:19 +0000 | [diff] [blame] | 746 | |
daniel@transgaming.com | 80fc332 | 2012-11-28 21:02:13 +0000 | [diff] [blame] | 747 | if (!renderbufferObject) |
| 748 | { |
| 749 | ERR("render target pointer unexpectedly null."); |
daniel@transgaming.com | e9c71b4 | 2012-11-28 21:02:23 +0000 | [diff] [blame] | 750 | return false; |
daniel@transgaming.com | 80fc332 | 2012-11-28 21:02:13 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | renderTargetSerial = renderbufferObject->getSerial(); |
| 754 | } |
| 755 | |
| 756 | // Get the depth stencil render buffer and serials |
| 757 | gl::Renderbuffer *depthStencil = NULL; |
| 758 | unsigned int depthbufferSerial = 0; |
| 759 | unsigned int stencilbufferSerial = 0; |
| 760 | if (framebuffer->getDepthbufferType() != GL_NONE) |
| 761 | { |
| 762 | depthStencil = framebuffer->getDepthbuffer(); |
| 763 | if (!depthStencil) |
| 764 | { |
| 765 | ERR("Depth stencil pointer unexpectedly null."); |
| 766 | return false; |
| 767 | } |
| 768 | |
| 769 | depthbufferSerial = depthStencil->getSerial(); |
| 770 | } |
| 771 | else if (framebuffer->getStencilbufferType() != GL_NONE) |
| 772 | { |
| 773 | depthStencil = framebuffer->getStencilbuffer(); |
| 774 | if (!depthStencil) |
| 775 | { |
| 776 | ERR("Depth stencil pointer unexpectedly null."); |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | stencilbufferSerial = depthStencil->getSerial(); |
| 781 | } |
| 782 | |
| 783 | // Extract the render target dimensions and view |
| 784 | unsigned int renderTargetWidth = 0; |
| 785 | unsigned int renderTargetHeight = 0; |
| 786 | GLenum renderTargetFormat = 0; |
| 787 | ID3D11RenderTargetView* framebufferRTV = NULL; |
| 788 | if (renderbufferObject) |
| 789 | { |
| 790 | RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget()); |
| 791 | if (!renderTarget) |
| 792 | { |
| 793 | ERR("render target pointer unexpectedly null."); |
| 794 | return false; |
| 795 | } |
| 796 | |
| 797 | framebufferRTV = renderTarget->getRenderTargetView(); |
| 798 | if (!framebufferRTV) |
| 799 | { |
| 800 | ERR("render target view pointer unexpectedly null."); |
| 801 | return false; |
| 802 | } |
| 803 | |
| 804 | renderTargetWidth = renderbufferObject->getWidth(); |
| 805 | renderTargetHeight = renderbufferObject->getHeight(); |
| 806 | renderTargetFormat = renderbufferObject->getActualFormat(); |
| 807 | } |
| 808 | |
| 809 | // Extract the depth stencil sizes and view |
| 810 | unsigned int depthSize = 0; |
| 811 | unsigned int stencilSize = 0; |
| 812 | ID3D11DepthStencilView* framebufferDSV = NULL; |
| 813 | if (depthStencil) |
| 814 | { |
| 815 | RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil()); |
| 816 | if (!depthStencilRenderTarget) |
| 817 | { |
| 818 | ERR("render target pointer unexpectedly null."); |
| 819 | if (framebufferRTV) |
| 820 | { |
| 821 | framebufferRTV->Release(); |
| 822 | } |
| 823 | return false; |
| 824 | } |
| 825 | |
| 826 | framebufferDSV = depthStencilRenderTarget->getDepthStencilView(); |
| 827 | if (!framebufferDSV) |
| 828 | { |
| 829 | ERR("depth stencil view pointer unexpectedly null."); |
| 830 | if (framebufferRTV) |
| 831 | { |
| 832 | framebufferRTV->Release(); |
| 833 | } |
| 834 | return false; |
| 835 | } |
| 836 | |
| 837 | // If there is no render buffer, the width, height and format values come from |
| 838 | // the depth stencil |
| 839 | if (!renderbufferObject) |
| 840 | { |
| 841 | renderTargetWidth = depthStencil->getWidth(); |
| 842 | renderTargetHeight = depthStencil->getHeight(); |
| 843 | renderTargetFormat = depthStencil->getActualFormat(); |
| 844 | } |
| 845 | |
| 846 | depthSize = depthStencil->getDepthSize(); |
| 847 | stencilSize = depthStencil->getStencilSize(); |
| 848 | } |
| 849 | |
| 850 | // Apply the render target and depth stencil |
| 851 | if (!mRenderTargetDescInitialized || !mDepthStencilInitialized || |
| 852 | renderTargetSerial != mAppliedRenderTargetSerial || |
| 853 | depthbufferSerial != mAppliedDepthbufferSerial || |
| 854 | stencilbufferSerial != mAppliedStencilbufferSerial) |
| 855 | { |
| 856 | mDeviceContext->OMSetRenderTargets(1, &framebufferRTV, framebufferDSV); |
| 857 | |
| 858 | mRenderTargetDesc.width = renderTargetWidth; |
| 859 | mRenderTargetDesc.height = renderTargetHeight; |
| 860 | mRenderTargetDesc.format = renderTargetFormat; |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 861 | mForceSetViewport = true; |
| 862 | mForceSetScissor = true; |
daniel@transgaming.com | 80fc332 | 2012-11-28 21:02:13 +0000 | [diff] [blame] | 863 | |
| 864 | if (!mDepthStencilInitialized || depthSize != mCurDepthSize) |
| 865 | { |
| 866 | mCurDepthSize = depthSize; |
| 867 | mForceSetRasterState = true; |
| 868 | } |
| 869 | |
| 870 | mCurStencilSize = stencilSize; |
| 871 | |
| 872 | mAppliedRenderTargetSerial = renderTargetSerial; |
| 873 | mAppliedDepthbufferSerial = depthbufferSerial; |
| 874 | mAppliedStencilbufferSerial = stencilbufferSerial; |
| 875 | mRenderTargetDescInitialized = true; |
| 876 | mDepthStencilInitialized = true; |
| 877 | } |
| 878 | |
| 879 | if (framebufferRTV) |
| 880 | { |
| 881 | framebufferRTV->Release(); |
| 882 | } |
| 883 | if (framebufferDSV) |
| 884 | { |
| 885 | framebufferDSV->Release(); |
| 886 | } |
daniel@transgaming.com | ae39ee2 | 2012-11-28 19:42:02 +0000 | [diff] [blame] | 887 | |
| 888 | return true; |
daniel@transgaming.com | 493d4f8 | 2012-11-28 19:35:45 +0000 | [diff] [blame] | 889 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 890 | |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 891 | 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] | 892 | { |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 893 | TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS]; |
| 894 | GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances); |
| 895 | if (err != GL_NO_ERROR) |
daniel@transgaming.com | da495a1 | 2012-11-28 21:03:56 +0000 | [diff] [blame] | 896 | { |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 897 | return err; |
| 898 | } |
daniel@transgaming.com | da495a1 | 2012-11-28 21:03:56 +0000 | [diff] [blame] | 899 | |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 900 | return mInputLayoutCache.applyVertexBuffers(attributes, programBinary); |
daniel@transgaming.com | def9f0f | 2012-11-28 20:53:20 +0000 | [diff] [blame] | 901 | } |
| 902 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 903 | 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] | 904 | { |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 905 | GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 906 | |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 907 | if (err == GL_NO_ERROR) |
| 908 | { |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 909 | if (indexInfo->storage) |
| 910 | { |
| 911 | if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset) |
| 912 | { |
| 913 | BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage); |
| 914 | IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer); |
| 915 | |
| 916 | mDeviceContext->IASetIndexBuffer(storage->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset); |
| 917 | |
| 918 | mAppliedIBSerial = 0; |
| 919 | mAppliedStorageIBSerial = storage->getSerial(); |
| 920 | mAppliedIBOffset = indexInfo->startOffset; |
| 921 | } |
| 922 | } |
| 923 | else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset) |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 924 | { |
| 925 | IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer); |
| 926 | |
daniel@transgaming.com | 22ada2c | 2013-01-11 04:07:12 +0000 | [diff] [blame] | 927 | mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset); |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 928 | |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 929 | mAppliedIBSerial = indexInfo->serial; |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 930 | mAppliedStorageIBSerial = 0; |
daniel@transgaming.com | 7fbf486 | 2013-01-11 04:09:22 +0000 | [diff] [blame] | 931 | mAppliedIBOffset = indexInfo->startOffset; |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
| 935 | return err; |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances) |
| 939 | { |
daniel@transgaming.com | 1ef0967 | 2013-01-11 04:10:37 +0000 | [diff] [blame] | 940 | if (mode == GL_LINE_LOOP) |
| 941 | { |
| 942 | drawLineLoop(count, GL_NONE, NULL, 0, NULL); |
| 943 | } |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 944 | else if (mode == GL_TRIANGLE_FAN) |
| 945 | { |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 946 | drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 947 | } |
daniel@transgaming.com | 1ef0967 | 2013-01-11 04:10:37 +0000 | [diff] [blame] | 948 | else if (instances > 0) |
| 949 | { |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 950 | mDeviceContext->DrawInstanced(count, instances, 0, 0); |
daniel@transgaming.com | 1ef0967 | 2013-01-11 04:10:37 +0000 | [diff] [blame] | 951 | } |
| 952 | else |
| 953 | { |
| 954 | mDeviceContext->Draw(count, 0); |
| 955 | } |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 956 | } |
| 957 | |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 958 | void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances) |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 959 | { |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 960 | if (mode == GL_LINE_LOOP) |
| 961 | { |
| 962 | drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer); |
| 963 | } |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 964 | else if (mode == GL_TRIANGLE_FAN) |
| 965 | { |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 966 | drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances); |
| 967 | } |
| 968 | else if (instances > 0) |
| 969 | { |
| 970 | mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 971 | } |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 972 | else |
| 973 | { |
daniel@transgaming.com | 9f7ede6 | 2013-01-11 04:07:06 +0000 | [diff] [blame] | 974 | mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex)); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | |
| 978 | void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer) |
| 979 | { |
| 980 | // Get the raw indices for an indexed draw |
| 981 | if (type != GL_NONE && elementArrayBuffer) |
| 982 | { |
| 983 | gl::Buffer *indexBuffer = elementArrayBuffer; |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 984 | BufferStorage *storage = indexBuffer->getStorage(); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 985 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 986 | indices = static_cast<const GLubyte*>(storage->getData()) + offset; |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | if (!mLineLoopIB) |
| 990 | { |
| 991 | mLineLoopIB = new StreamingIndexBufferInterface(this); |
| 992 | if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT)) |
| 993 | { |
| 994 | delete mLineLoopIB; |
| 995 | mLineLoopIB = NULL; |
| 996 | |
| 997 | ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 998 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | const int spaceNeeded = (count + 1) * sizeof(unsigned int); |
| 1003 | if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT)) |
| 1004 | { |
| 1005 | ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1006 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | void* mappedMemory = NULL; |
| 1010 | int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory); |
| 1011 | if (offset == -1 || mappedMemory == NULL) |
| 1012 | { |
| 1013 | ERR("Could not map index buffer for GL_LINE_LOOP."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1014 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1017 | unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory); |
daniel@transgaming.com | 7fbf486 | 2013-01-11 04:09:22 +0000 | [diff] [blame] | 1018 | unsigned int indexBufferOffset = static_cast<unsigned int>(offset); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1019 | |
| 1020 | switch (type) |
| 1021 | { |
| 1022 | case GL_NONE: // Non-indexed draw |
| 1023 | for (int i = 0; i < count; i++) |
| 1024 | { |
| 1025 | data[i] = i; |
| 1026 | } |
| 1027 | data[count] = 0; |
| 1028 | break; |
| 1029 | case GL_UNSIGNED_BYTE: |
| 1030 | for (int i = 0; i < count; i++) |
| 1031 | { |
| 1032 | data[i] = static_cast<const GLubyte*>(indices)[i]; |
| 1033 | } |
| 1034 | data[count] = static_cast<const GLubyte*>(indices)[0]; |
| 1035 | break; |
| 1036 | case GL_UNSIGNED_SHORT: |
| 1037 | for (int i = 0; i < count; i++) |
| 1038 | { |
| 1039 | data[i] = static_cast<const GLushort*>(indices)[i]; |
| 1040 | } |
| 1041 | data[count] = static_cast<const GLushort*>(indices)[0]; |
| 1042 | break; |
| 1043 | case GL_UNSIGNED_INT: |
| 1044 | for (int i = 0; i < count; i++) |
| 1045 | { |
| 1046 | data[i] = static_cast<const GLuint*>(indices)[i]; |
| 1047 | } |
| 1048 | data[count] = static_cast<const GLuint*>(indices)[0]; |
| 1049 | break; |
| 1050 | default: UNREACHABLE(); |
| 1051 | } |
| 1052 | |
| 1053 | if (!mLineLoopIB->unmapBuffer()) |
| 1054 | { |
| 1055 | ERR("Could not unmap index buffer for GL_LINE_LOOP."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1056 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
daniel@transgaming.com | 7fbf486 | 2013-01-11 04:09:22 +0000 | [diff] [blame] | 1059 | if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset) |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1060 | { |
| 1061 | IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer()); |
| 1062 | |
daniel@transgaming.com | 7fbf486 | 2013-01-11 04:09:22 +0000 | [diff] [blame] | 1063 | mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset); |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1064 | mAppliedIBSerial = mLineLoopIB->getSerial(); |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 1065 | mAppliedStorageIBSerial = 0; |
daniel@transgaming.com | 7fbf486 | 2013-01-11 04:09:22 +0000 | [diff] [blame] | 1066 | mAppliedIBOffset = indexBufferOffset; |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
daniel@transgaming.com | 1ef0967 | 2013-01-11 04:10:37 +0000 | [diff] [blame] | 1069 | mDeviceContext->DrawIndexed(count + 1, 0, -minIndex); |
daniel@transgaming.com | 91207b7 | 2012-11-28 20:56:43 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 1072 | void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances) |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1073 | { |
| 1074 | // Get the raw indices for an indexed draw |
| 1075 | if (type != GL_NONE && elementArrayBuffer) |
| 1076 | { |
| 1077 | gl::Buffer *indexBuffer = elementArrayBuffer; |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 1078 | BufferStorage *storage = indexBuffer->getStorage(); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1079 | intptr_t offset = reinterpret_cast<intptr_t>(indices); |
shannon.woods@transgaming.com | 7665541 | 2013-02-28 23:08:09 +0000 | [diff] [blame] | 1080 | indices = static_cast<const GLubyte*>(storage->getData()) + offset; |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | if (!mTriangleFanIB) |
| 1084 | { |
| 1085 | mTriangleFanIB = new StreamingIndexBufferInterface(this); |
| 1086 | if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT)) |
| 1087 | { |
| 1088 | delete mTriangleFanIB; |
| 1089 | mTriangleFanIB = NULL; |
| 1090 | |
| 1091 | ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1092 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | const int numTris = count - 2; |
| 1097 | const int spaceNeeded = (numTris * 3) * sizeof(unsigned int); |
| 1098 | if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT)) |
| 1099 | { |
| 1100 | ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1101 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | void* mappedMemory = NULL; |
| 1105 | int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory); |
| 1106 | if (offset == -1 || mappedMemory == NULL) |
| 1107 | { |
| 1108 | ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1109 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory); |
| 1113 | unsigned int indexBufferOffset = static_cast<unsigned int>(offset); |
| 1114 | |
| 1115 | switch (type) |
| 1116 | { |
| 1117 | case GL_NONE: // Non-indexed draw |
| 1118 | for (int i = 0; i < numTris; i++) |
| 1119 | { |
| 1120 | data[i*3 + 0] = 0; |
| 1121 | data[i*3 + 1] = i + 1; |
| 1122 | data[i*3 + 2] = i + 2; |
| 1123 | } |
| 1124 | break; |
| 1125 | case GL_UNSIGNED_BYTE: |
| 1126 | for (int i = 0; i < numTris; i++) |
| 1127 | { |
| 1128 | data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0]; |
| 1129 | data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1]; |
| 1130 | data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2]; |
| 1131 | } |
| 1132 | break; |
| 1133 | case GL_UNSIGNED_SHORT: |
| 1134 | for (int i = 0; i < numTris; i++) |
| 1135 | { |
| 1136 | data[i*3 + 0] = static_cast<const GLushort*>(indices)[0]; |
| 1137 | data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1]; |
| 1138 | data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2]; |
| 1139 | } |
| 1140 | break; |
| 1141 | case GL_UNSIGNED_INT: |
| 1142 | for (int i = 0; i < numTris; i++) |
| 1143 | { |
| 1144 | data[i*3 + 0] = static_cast<const GLuint*>(indices)[0]; |
| 1145 | data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1]; |
| 1146 | data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2]; |
| 1147 | } |
| 1148 | break; |
| 1149 | default: UNREACHABLE(); |
| 1150 | } |
| 1151 | |
| 1152 | if (!mTriangleFanIB->unmapBuffer()) |
| 1153 | { |
| 1154 | ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1155 | return gl::error(GL_OUT_OF_MEMORY); |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset) |
| 1159 | { |
| 1160 | IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer()); |
| 1161 | |
| 1162 | mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset); |
| 1163 | mAppliedIBSerial = mTriangleFanIB->getSerial(); |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 1164 | mAppliedStorageIBSerial = 0; |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1165 | mAppliedIBOffset = indexBufferOffset; |
| 1166 | } |
| 1167 | |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 1168 | if (instances > 0) |
| 1169 | { |
| 1170 | mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0); |
| 1171 | } |
| 1172 | else |
| 1173 | { |
| 1174 | mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex); |
| 1175 | } |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
daniel@transgaming.com | 5fbf177 | 2012-11-28 20:54:43 +0000 | [diff] [blame] | 1178 | void Renderer11::applyShaders(gl::ProgramBinary *programBinary) |
| 1179 | { |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1180 | unsigned int programBinarySerial = programBinary->getSerial(); |
shannon.woods@transgaming.com | 43ccf3f | 2013-02-28 23:07:19 +0000 | [diff] [blame] | 1181 | const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial); |
| 1182 | |
| 1183 | if (updateProgramState) |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1184 | { |
| 1185 | ShaderExecutable *vertexExe = programBinary->getVertexExecutable(); |
| 1186 | ShaderExecutable *pixelExe = programBinary->getPixelExecutable(); |
daniel@transgaming.com | d4b2db2 | 2012-11-28 21:05:15 +0000 | [diff] [blame] | 1187 | |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1188 | ID3D11VertexShader *vertexShader = NULL; |
| 1189 | if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader(); |
daniel@transgaming.com | d4b2db2 | 2012-11-28 21:05:15 +0000 | [diff] [blame] | 1190 | |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1191 | ID3D11PixelShader *pixelShader = NULL; |
| 1192 | if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader(); |
daniel@transgaming.com | d4b2db2 | 2012-11-28 21:05:15 +0000 | [diff] [blame] | 1193 | |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1194 | mDeviceContext->PSSetShader(pixelShader, NULL, 0); |
| 1195 | mDeviceContext->VSSetShader(vertexShader, NULL, 0); |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 1196 | |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 1197 | programBinary->dirtyAllUniforms(); |
| 1198 | |
| 1199 | mAppliedProgramBinarySerial = programBinarySerial; |
| 1200 | } |
| 1201 | |
| 1202 | // Only use the geometry shader currently for point sprite drawing |
shannon.woods@transgaming.com | 43ccf3f | 2013-02-28 23:07:19 +0000 | [diff] [blame] | 1203 | const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode); |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 1204 | |
shannon.woods@transgaming.com | 43ccf3f | 2013-02-28 23:07:19 +0000 | [diff] [blame] | 1205 | if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive) |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 1206 | { |
| 1207 | if (usesGeometryShader) |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 1208 | { |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 1209 | ShaderExecutable *geometryExe = programBinary->getGeometryExecutable(); |
| 1210 | ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader(); |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 1211 | mDeviceContext->GSSetShader(geometryShader, NULL, 0); |
| 1212 | } |
| 1213 | else |
| 1214 | { |
| 1215 | mDeviceContext->GSSetShader(NULL, NULL, 0); |
| 1216 | } |
| 1217 | |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 1218 | mIsGeometryShaderActive = usesGeometryShader; |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1219 | } |
daniel@transgaming.com | 5fbf177 | 2012-11-28 20:54:43 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1222 | void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray) |
daniel@transgaming.com | ab1c146 | 2012-12-20 21:08:30 +0000 | [diff] [blame] | 1223 | { |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1224 | ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable()); |
| 1225 | ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable()); |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1226 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1227 | unsigned int totalRegisterCountVS = 0; |
| 1228 | unsigned int totalRegisterCountPS = 0; |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1229 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1230 | bool vertexUniformsDirty = false; |
| 1231 | bool pixelUniformsDirty = false; |
| 1232 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1233 | for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++) |
| 1234 | { |
| 1235 | const gl::Uniform *uniform = *uniform_iterator; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1236 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1237 | if (uniform->vsRegisterIndex >= 0) |
| 1238 | { |
| 1239 | totalRegisterCountVS += uniform->registerCount; |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1240 | vertexUniformsDirty = vertexUniformsDirty || uniform->dirty; |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1241 | } |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1242 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1243 | if (uniform->psRegisterIndex >= 0) |
| 1244 | { |
| 1245 | totalRegisterCountPS += uniform->registerCount; |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1246 | pixelUniformsDirty = pixelUniformsDirty || uniform->dirty; |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1247 | } |
| 1248 | } |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1249 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1250 | ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS); |
| 1251 | ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS); |
| 1252 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1253 | void *mapVS = (totalRegisterCountVS > 0 && vertexUniformsDirty) ? new float[4 * totalRegisterCountVS] : NULL; |
| 1254 | void *mapPS = (totalRegisterCountPS > 0 && pixelUniformsDirty) ? new float[4 * totalRegisterCountPS] : NULL; |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1255 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1256 | for (gl::UniformArray::iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++) |
daniel@transgaming.com | ab1c146 | 2012-12-20 21:08:30 +0000 | [diff] [blame] | 1257 | { |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1258 | gl::Uniform *uniform = *uniform_iterator; |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1259 | |
| 1260 | switch (uniform->type) |
| 1261 | { |
daniel@transgaming.com | da8d380 | 2012-12-20 21:12:55 +0000 | [diff] [blame] | 1262 | case GL_SAMPLER_2D: |
| 1263 | case GL_SAMPLER_CUBE: |
| 1264 | break; |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1265 | case GL_FLOAT: |
| 1266 | case GL_FLOAT_VEC2: |
| 1267 | case GL_FLOAT_VEC3: |
| 1268 | case GL_FLOAT_VEC4: |
| 1269 | case GL_FLOAT_MAT2: |
| 1270 | case GL_FLOAT_MAT3: |
| 1271 | case GL_FLOAT_MAT4: |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1272 | if (uniform->vsRegisterIndex >= 0 && mapVS) |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1273 | { |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1274 | GLfloat (*c)[4] = (GLfloat(*)[4])mapVS; |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1275 | float (*f)[4] = (float(*)[4])uniform->data; |
| 1276 | |
| 1277 | for (unsigned int i = 0; i < uniform->registerCount; i++) |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1278 | { |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1279 | c[uniform->vsRegisterIndex + i][0] = f[i][0]; |
| 1280 | c[uniform->vsRegisterIndex + i][1] = f[i][1]; |
| 1281 | c[uniform->vsRegisterIndex + i][2] = f[i][2]; |
| 1282 | c[uniform->vsRegisterIndex + i][3] = f[i][3]; |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1283 | } |
| 1284 | } |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1285 | if (uniform->psRegisterIndex >= 0 && mapPS) |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1286 | { |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1287 | GLfloat (*c)[4] = (GLfloat(*)[4])mapPS; |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1288 | float (*f)[4] = (float(*)[4])uniform->data; |
| 1289 | |
| 1290 | for (unsigned int i = 0; i < uniform->registerCount; i++) |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1291 | { |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1292 | c[uniform->psRegisterIndex + i][0] = f[i][0]; |
| 1293 | c[uniform->psRegisterIndex + i][1] = f[i][1]; |
| 1294 | c[uniform->psRegisterIndex + i][2] = f[i][2]; |
| 1295 | c[uniform->psRegisterIndex + i][3] = f[i][3]; |
| 1296 | } |
| 1297 | } |
| 1298 | break; |
| 1299 | case GL_INT: |
| 1300 | case GL_INT_VEC2: |
| 1301 | case GL_INT_VEC3: |
| 1302 | case GL_INT_VEC4: |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1303 | if (uniform->vsRegisterIndex >= 0 && mapVS) |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1304 | { |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1305 | int (*c)[4] = (int(*)[4])mapVS; |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1306 | GLint *x = (GLint*)uniform->data; |
| 1307 | int count = gl::VariableColumnCount(uniform->type); |
| 1308 | |
| 1309 | for (unsigned int i = 0; i < uniform->registerCount; i++) |
| 1310 | { |
| 1311 | if (count >= 1) c[uniform->vsRegisterIndex + i][0] = x[i * count + 0]; |
| 1312 | if (count >= 2) c[uniform->vsRegisterIndex + i][1] = x[i * count + 1]; |
| 1313 | if (count >= 3) c[uniform->vsRegisterIndex + i][2] = x[i * count + 2]; |
| 1314 | if (count >= 4) c[uniform->vsRegisterIndex + i][3] = x[i * count + 3]; |
| 1315 | } |
| 1316 | } |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1317 | if (uniform->psRegisterIndex >= 0 && mapPS) |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1318 | { |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1319 | int (*c)[4] = (int(*)[4])mapPS; |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1320 | GLint *x = (GLint*)uniform->data; |
| 1321 | int count = gl::VariableColumnCount(uniform->type); |
| 1322 | |
| 1323 | for (unsigned int i = 0; i < uniform->registerCount; i++) |
| 1324 | { |
| 1325 | if (count >= 1) c[uniform->psRegisterIndex + i][0] = x[i * count + 0]; |
| 1326 | if (count >= 2) c[uniform->psRegisterIndex + i][1] = x[i * count + 1]; |
| 1327 | if (count >= 3) c[uniform->psRegisterIndex + i][2] = x[i * count + 2]; |
| 1328 | if (count >= 4) c[uniform->psRegisterIndex + i][3] = x[i * count + 3]; |
| 1329 | } |
| 1330 | } |
| 1331 | break; |
| 1332 | case GL_BOOL: |
| 1333 | case GL_BOOL_VEC2: |
| 1334 | case GL_BOOL_VEC3: |
| 1335 | case GL_BOOL_VEC4: |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1336 | if (uniform->vsRegisterIndex >= 0 && mapVS) |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1337 | { |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1338 | int (*c)[4] = (int(*)[4])mapVS; |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1339 | GLboolean *b = (GLboolean*)uniform->data; |
| 1340 | int count = gl::VariableColumnCount(uniform->type); |
| 1341 | |
| 1342 | for (unsigned int i = 0; i < uniform->registerCount; i++) |
| 1343 | { |
| 1344 | if (count >= 1) c[uniform->vsRegisterIndex + i][0] = b[i * count + 0]; |
| 1345 | if (count >= 2) c[uniform->vsRegisterIndex + i][1] = b[i * count + 1]; |
| 1346 | if (count >= 3) c[uniform->vsRegisterIndex + i][2] = b[i * count + 2]; |
| 1347 | if (count >= 4) c[uniform->vsRegisterIndex + i][3] = b[i * count + 3]; |
| 1348 | } |
| 1349 | } |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1350 | if (uniform->psRegisterIndex >= 0 && mapPS) |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1351 | { |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1352 | int (*c)[4] = (int(*)[4])mapPS; |
daniel@transgaming.com | e76b64b | 2013-01-11 04:10:08 +0000 | [diff] [blame] | 1353 | GLboolean *b = (GLboolean*)uniform->data; |
| 1354 | int count = gl::VariableColumnCount(uniform->type); |
| 1355 | |
| 1356 | for (unsigned int i = 0; i < uniform->registerCount; i++) |
| 1357 | { |
| 1358 | if (count >= 1) c[uniform->psRegisterIndex + i][0] = b[i * count + 0]; |
| 1359 | if (count >= 2) c[uniform->psRegisterIndex + i][1] = b[i * count + 1]; |
| 1360 | if (count >= 3) c[uniform->psRegisterIndex + i][2] = b[i * count + 2]; |
| 1361 | 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] | 1362 | } |
| 1363 | } |
| 1364 | break; |
| 1365 | default: |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1366 | UNREACHABLE(); |
| 1367 | } |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1368 | |
| 1369 | uniform->dirty = false; |
daniel@transgaming.com | ab1c146 | 2012-12-20 21:08:30 +0000 | [diff] [blame] | 1370 | } |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1371 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1372 | if (vertexConstantBuffer && mapVS) |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1373 | { |
| 1374 | mDeviceContext->UpdateSubresource(vertexConstantBuffer, 0, NULL, mapVS, 0, 0); |
| 1375 | } |
| 1376 | |
shannon.woods@transgaming.com | 21ba647 | 2013-01-25 21:53:32 +0000 | [diff] [blame] | 1377 | if (pixelConstantBuffer && mapPS) |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1378 | { |
| 1379 | mDeviceContext->UpdateSubresource(pixelConstantBuffer, 0, NULL, mapPS, 0, 0); |
| 1380 | } |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1381 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1382 | mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer); |
| 1383 | mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer); |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 1384 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1385 | delete[] mapVS; |
| 1386 | delete[] mapPS; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1387 | |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 1388 | // Driver uniforms |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1389 | if (!mDriverConstantBufferVS) |
| 1390 | { |
| 1391 | D3D11_BUFFER_DESC constantBufferDescription = {0}; |
| 1392 | constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants); |
| 1393 | constantBufferDescription.Usage = D3D11_USAGE_DEFAULT; |
| 1394 | constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER; |
| 1395 | constantBufferDescription.CPUAccessFlags = 0; |
| 1396 | constantBufferDescription.MiscFlags = 0; |
| 1397 | constantBufferDescription.StructureByteStride = 0; |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 1398 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1399 | HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS); |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1400 | ASSERT(SUCCEEDED(result)); |
daniel@transgaming.com | 873f28a | 2012-12-20 21:12:42 +0000 | [diff] [blame] | 1401 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1402 | mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS); |
| 1403 | } |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1404 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1405 | if (!mDriverConstantBufferPS) |
| 1406 | { |
| 1407 | D3D11_BUFFER_DESC constantBufferDescription = {0}; |
| 1408 | constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants); |
| 1409 | constantBufferDescription.Usage = D3D11_USAGE_DEFAULT; |
| 1410 | constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER; |
| 1411 | constantBufferDescription.CPUAccessFlags = 0; |
| 1412 | constantBufferDescription.MiscFlags = 0; |
| 1413 | constantBufferDescription.StructureByteStride = 0; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1414 | |
shannon.woods@transgaming.com | 5929ef2 | 2013-01-25 21:53:24 +0000 | [diff] [blame] | 1415 | HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS); |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1416 | ASSERT(SUCCEEDED(result)); |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1417 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1418 | mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS); |
| 1419 | } |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1420 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1421 | if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0) |
| 1422 | { |
| 1423 | mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0); |
| 1424 | memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants)); |
| 1425 | } |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 1426 | |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1427 | if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0) |
| 1428 | { |
| 1429 | mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0); |
| 1430 | memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants)); |
| 1431 | } |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 1432 | |
| 1433 | // needed for the point sprite geometry shader |
| 1434 | mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS); |
daniel@transgaming.com | ab1c146 | 2012-12-20 21:08:30 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
daniel@transgaming.com | 084a257 | 2012-11-28 20:55:17 +0000 | [diff] [blame] | 1437 | void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer) |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 1438 | { |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1439 | bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha; |
| 1440 | bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) && |
| 1441 | !(clearParams.colorMaskRed && clearParams.colorMaskGreen && |
| 1442 | clearParams.colorMaskBlue && alphaUnmasked); |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1443 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1444 | unsigned int stencilUnmasked = 0x0; |
| 1445 | if (frameBuffer->hasStencil()) |
| 1446 | { |
| 1447 | unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat()); |
| 1448 | stencilUnmasked = (0x1 << stencilSize) - 1; |
| 1449 | } |
| 1450 | bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) && |
| 1451 | (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked; |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1452 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1453 | bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 || |
| 1454 | mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width || |
| 1455 | mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height); |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1456 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1457 | if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear) |
| 1458 | { |
shannon.woods@transgaming.com | 34f507c | 2013-01-25 21:52:15 +0000 | [diff] [blame] | 1459 | maskedClear(clearParams); |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1460 | } |
| 1461 | else |
| 1462 | { |
| 1463 | if (clearParams.mask & GL_COLOR_BUFFER_BIT) |
| 1464 | { |
| 1465 | gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer(); |
| 1466 | if (renderbufferObject) |
| 1467 | { |
| 1468 | RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget()); |
| 1469 | if (!renderTarget) |
| 1470 | { |
| 1471 | ERR("render target pointer unexpectedly null."); |
| 1472 | return; |
| 1473 | } |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1474 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1475 | ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView(); |
| 1476 | if (!framebufferRTV) |
| 1477 | { |
| 1478 | ERR("render target view pointer unexpectedly null."); |
| 1479 | return; |
| 1480 | } |
| 1481 | |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1482 | const float clearValues[4] = { clearParams.colorClearValue.red, |
| 1483 | clearParams.colorClearValue.green, |
| 1484 | clearParams.colorClearValue.blue, |
| 1485 | clearParams.colorClearValue.alpha }; |
| 1486 | mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues); |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1487 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1488 | framebufferRTV->Release(); |
| 1489 | } |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1490 | } |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1491 | if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT) |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1492 | { |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1493 | gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer(); |
| 1494 | if (renderbufferObject) |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1495 | { |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1496 | RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil()); |
| 1497 | if (!renderTarget) |
| 1498 | { |
| 1499 | ERR("render target pointer unexpectedly null."); |
| 1500 | return; |
| 1501 | } |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1502 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1503 | ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView(); |
| 1504 | if (!framebufferDSV) |
| 1505 | { |
| 1506 | ERR("depth stencil view pointer unexpectedly null."); |
| 1507 | return; |
| 1508 | } |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1509 | |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1510 | UINT clearFlags = 0; |
| 1511 | if (clearParams.mask & GL_DEPTH_BUFFER_BIT) |
| 1512 | { |
| 1513 | clearFlags |= D3D11_CLEAR_DEPTH; |
| 1514 | } |
| 1515 | if (clearParams.mask & GL_STENCIL_BUFFER_BIT) |
| 1516 | { |
| 1517 | clearFlags |= D3D11_CLEAR_STENCIL; |
| 1518 | } |
| 1519 | |
| 1520 | float depthClear = clearParams.depthClearValue; |
| 1521 | UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF; |
| 1522 | |
| 1523 | mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear); |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1524 | |
shannon.woods@transgaming.com | 81c5ef9 | 2013-01-25 21:52:08 +0000 | [diff] [blame] | 1525 | framebufferDSV->Release(); |
| 1526 | } |
daniel@transgaming.com | 54e6754 | 2012-11-28 21:02:31 +0000 | [diff] [blame] | 1527 | } |
| 1528 | } |
daniel@transgaming.com | d084c62 | 2012-11-28 19:36:05 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
shannon.woods@transgaming.com | 34f507c | 2013-01-25 21:52:15 +0000 | [diff] [blame] | 1531 | void Renderer11::maskedClear(const gl::ClearParameters &clearParams) |
| 1532 | { |
| 1533 | HRESULT result; |
| 1534 | |
| 1535 | if (!mClearResourcesInitialized) |
| 1536 | { |
| 1537 | ASSERT(!mClearVB && !mClearVS && !mClearPS && !mClearScissorRS && !mClearNoScissorRS); |
| 1538 | |
| 1539 | D3D11_BUFFER_DESC vbDesc; |
| 1540 | vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4; |
| 1541 | vbDesc.Usage = D3D11_USAGE_DYNAMIC; |
| 1542 | vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; |
| 1543 | vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| 1544 | vbDesc.MiscFlags = 0; |
| 1545 | vbDesc.StructureByteStride = 0; |
| 1546 | |
| 1547 | result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB); |
| 1548 | ASSERT(SUCCEEDED(result)); |
| 1549 | d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer"); |
| 1550 | |
| 1551 | D3D11_INPUT_ELEMENT_DESC quadLayout[] = |
| 1552 | { |
| 1553 | { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 1554 | { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 1555 | }; |
| 1556 | |
| 1557 | result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL); |
| 1558 | ASSERT(SUCCEEDED(result)); |
| 1559 | d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout"); |
| 1560 | |
| 1561 | result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS); |
| 1562 | ASSERT(SUCCEEDED(result)); |
| 1563 | d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader"); |
| 1564 | |
| 1565 | result = mDevice->CreatePixelShader(g_PS_Clear, sizeof(g_PS_Clear), NULL, &mClearPS); |
| 1566 | ASSERT(SUCCEEDED(result)); |
| 1567 | d3d11::SetDebugName(mClearPS, "Renderer11 masked clear pixel shader"); |
| 1568 | |
| 1569 | D3D11_RASTERIZER_DESC rsScissorDesc; |
| 1570 | rsScissorDesc.FillMode = D3D11_FILL_SOLID; |
| 1571 | rsScissorDesc.CullMode = D3D11_CULL_NONE; |
| 1572 | rsScissorDesc.FrontCounterClockwise = FALSE; |
| 1573 | rsScissorDesc.DepthBias = 0; |
| 1574 | rsScissorDesc.DepthBiasClamp = 0.0f; |
| 1575 | rsScissorDesc.SlopeScaledDepthBias = 0.0f; |
| 1576 | rsScissorDesc.DepthClipEnable = FALSE; |
| 1577 | rsScissorDesc.ScissorEnable = TRUE; |
| 1578 | rsScissorDesc.MultisampleEnable = FALSE; |
| 1579 | rsScissorDesc.AntialiasedLineEnable = FALSE; |
| 1580 | |
| 1581 | result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS); |
| 1582 | ASSERT(SUCCEEDED(result)); |
| 1583 | d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state"); |
| 1584 | |
| 1585 | D3D11_RASTERIZER_DESC rsNoScissorDesc; |
| 1586 | rsNoScissorDesc.FillMode = D3D11_FILL_SOLID; |
| 1587 | rsNoScissorDesc.CullMode = D3D11_CULL_NONE; |
| 1588 | rsNoScissorDesc.FrontCounterClockwise = FALSE; |
| 1589 | rsNoScissorDesc.DepthBias = 0; |
| 1590 | rsNoScissorDesc.DepthBiasClamp = 0.0f; |
| 1591 | rsNoScissorDesc.SlopeScaledDepthBias = 0.0f; |
| 1592 | rsNoScissorDesc.DepthClipEnable = FALSE; |
| 1593 | rsNoScissorDesc.ScissorEnable = FALSE; |
| 1594 | rsNoScissorDesc.MultisampleEnable = FALSE; |
| 1595 | rsNoScissorDesc.AntialiasedLineEnable = FALSE; |
| 1596 | |
| 1597 | result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS); |
| 1598 | ASSERT(SUCCEEDED(result)); |
| 1599 | d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state"); |
| 1600 | |
| 1601 | mClearResourcesInitialized = true; |
| 1602 | } |
| 1603 | |
| 1604 | // Prepare the depth stencil state to write depth values if the depth should be cleared |
| 1605 | // and stencil values if the stencil should be cleared |
| 1606 | gl::DepthStencilState glDSState; |
| 1607 | glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0; |
| 1608 | glDSState.depthFunc = GL_ALWAYS; |
| 1609 | glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0; |
| 1610 | glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0; |
| 1611 | glDSState.stencilFunc = GL_ALWAYS; |
| 1612 | glDSState.stencilMask = 0; |
| 1613 | glDSState.stencilFail = GL_REPLACE; |
| 1614 | glDSState.stencilPassDepthFail = GL_REPLACE; |
| 1615 | glDSState.stencilPassDepthPass = GL_REPLACE; |
| 1616 | glDSState.stencilWritemask = clearParams.stencilWriteMask; |
| 1617 | glDSState.stencilBackFunc = GL_ALWAYS; |
| 1618 | glDSState.stencilBackMask = 0; |
| 1619 | glDSState.stencilBackFail = GL_REPLACE; |
| 1620 | glDSState.stencilBackPassDepthFail = GL_REPLACE; |
| 1621 | glDSState.stencilBackPassDepthPass = GL_REPLACE; |
| 1622 | glDSState.stencilBackWritemask = clearParams.stencilWriteMask; |
| 1623 | |
| 1624 | int stencilClear = clearParams.stencilClearValue & 0x000000FF; |
| 1625 | |
| 1626 | ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState); |
| 1627 | |
| 1628 | // Prepare the blend state to use a write mask if the color buffer should be cleared |
| 1629 | gl::BlendState glBlendState; |
| 1630 | glBlendState.blend = false; |
| 1631 | glBlendState.sourceBlendRGB = GL_ONE; |
| 1632 | glBlendState.destBlendRGB = GL_ZERO; |
| 1633 | glBlendState.sourceBlendAlpha = GL_ONE; |
| 1634 | glBlendState.destBlendAlpha = GL_ZERO; |
| 1635 | glBlendState.blendEquationRGB = GL_FUNC_ADD; |
| 1636 | glBlendState.blendEquationAlpha = GL_FUNC_ADD; |
| 1637 | glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false; |
| 1638 | glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false; |
| 1639 | glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false; |
| 1640 | glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false; |
| 1641 | glBlendState.sampleAlphaToCoverage = false; |
| 1642 | glBlendState.dither = false; |
| 1643 | |
| 1644 | static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; |
| 1645 | static const UINT sampleMask = 0xFFFFFFFF; |
| 1646 | |
| 1647 | ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState); |
| 1648 | |
| 1649 | // Set the vertices |
| 1650 | D3D11_MAPPED_SUBRESOURCE mappedResource; |
| 1651 | result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); |
| 1652 | if (FAILED(result)) |
| 1653 | { |
| 1654 | ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result); |
| 1655 | return; |
| 1656 | } |
| 1657 | |
| 1658 | d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData); |
| 1659 | |
| 1660 | float depthClear = gl::clamp01(clearParams.depthClearValue); |
| 1661 | d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue); |
| 1662 | d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue); |
| 1663 | d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue); |
| 1664 | d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue); |
| 1665 | |
| 1666 | mDeviceContext->Unmap(mClearVB, 0); |
| 1667 | |
| 1668 | // Apply state |
| 1669 | mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask); |
| 1670 | mDeviceContext->OMSetDepthStencilState(dsState, stencilClear); |
| 1671 | mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS); |
| 1672 | |
| 1673 | // Apply shaders |
| 1674 | mDeviceContext->IASetInputLayout(mClearIL); |
| 1675 | mDeviceContext->VSSetShader(mClearVS, NULL, 0); |
| 1676 | mDeviceContext->PSSetShader(mClearPS, NULL, 0); |
| 1677 | |
| 1678 | // Apply vertex buffer |
| 1679 | static UINT stride = sizeof(d3d11::PositionDepthColorVertex); |
| 1680 | static UINT startIdx = 0; |
| 1681 | mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx); |
| 1682 | mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); |
| 1683 | |
| 1684 | // Draw the clear quad |
| 1685 | mDeviceContext->Draw(4, 0); |
| 1686 | |
| 1687 | // Clean up |
| 1688 | markAllStateDirty(); |
| 1689 | } |
| 1690 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 1691 | void Renderer11::markAllStateDirty() |
| 1692 | { |
daniel@transgaming.com | 9a06737 | 2012-12-20 20:55:24 +0000 | [diff] [blame] | 1693 | mAppliedRenderTargetSerial = 0; |
| 1694 | mAppliedDepthbufferSerial = 0; |
| 1695 | mAppliedStencilbufferSerial = 0; |
daniel@transgaming.com | 7b6b83e | 2012-11-28 21:00:30 +0000 | [diff] [blame] | 1696 | mDepthStencilInitialized = false; |
| 1697 | mRenderTargetDescInitialized = false; |
| 1698 | |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 1699 | for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++) |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 1700 | { |
| 1701 | mForceSetVertexSamplerStates[i] = true; |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 1702 | mCurVertexTextureSerials[i] = 0; |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 1703 | } |
| 1704 | for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++) |
| 1705 | { |
| 1706 | mForceSetPixelSamplerStates[i] = true; |
daniel@transgaming.com | e33c8bf | 2013-01-11 04:11:33 +0000 | [diff] [blame] | 1707 | mCurPixelTextureSerials[i] = 0; |
daniel@transgaming.com | 54de24f | 2013-01-11 04:07:59 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 1710 | mForceSetBlendState = true; |
| 1711 | mForceSetRasterState = true; |
| 1712 | mForceSetDepthStencilState = true; |
| 1713 | mForceSetScissor = true; |
daniel@transgaming.com | 5367004 | 2012-11-28 20:55:51 +0000 | [diff] [blame] | 1714 | mForceSetViewport = true; |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1715 | |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 1716 | mAppliedIBSerial = 0; |
shannon.woods@transgaming.com | a1229a3 | 2013-02-28 23:08:40 +0000 | [diff] [blame] | 1717 | mAppliedStorageIBSerial = 0; |
daniel@transgaming.com | 7fbf486 | 2013-01-11 04:09:22 +0000 | [diff] [blame] | 1718 | mAppliedIBOffset = 0; |
| 1719 | |
daniel@transgaming.com | e499141 | 2012-12-20 20:55:34 +0000 | [diff] [blame] | 1720 | mAppliedProgramBinarySerial = 0; |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1721 | memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants)); |
| 1722 | memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants)); |
daniel@transgaming.com | c43a605 | 2012-11-28 19:41:51 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1725 | void Renderer11::releaseDeviceResources() |
| 1726 | { |
daniel@transgaming.com | f8ba109 | 2012-11-28 19:37:53 +0000 | [diff] [blame] | 1727 | mStateCache.clear(); |
daniel@transgaming.com | c5431eb | 2012-12-20 21:10:15 +0000 | [diff] [blame] | 1728 | mInputLayoutCache.clear(); |
| 1729 | |
| 1730 | delete mVertexDataManager; |
| 1731 | mVertexDataManager = NULL; |
| 1732 | |
| 1733 | delete mIndexDataManager; |
| 1734 | mIndexDataManager = NULL; |
daniel@transgaming.com | c511430 | 2012-12-20 21:11:36 +0000 | [diff] [blame] | 1735 | |
| 1736 | delete mLineLoopIB; |
| 1737 | mLineLoopIB = NULL; |
daniel@transgaming.com | 4fd1f98 | 2013-01-11 04:12:58 +0000 | [diff] [blame] | 1738 | |
| 1739 | delete mTriangleFanIB; |
| 1740 | mTriangleFanIB = NULL; |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 1741 | |
| 1742 | if (mCopyVB) |
| 1743 | { |
| 1744 | mCopyVB->Release(); |
| 1745 | mCopyVB = NULL; |
| 1746 | } |
| 1747 | |
| 1748 | if (mCopySampler) |
| 1749 | { |
| 1750 | mCopySampler->Release(); |
| 1751 | mCopySampler = NULL; |
| 1752 | } |
| 1753 | |
| 1754 | if (mCopyIL) |
| 1755 | { |
| 1756 | mCopyIL->Release(); |
| 1757 | mCopyIL = NULL; |
| 1758 | } |
| 1759 | |
| 1760 | if (mCopyVS) |
| 1761 | { |
| 1762 | mCopyVS->Release(); |
| 1763 | mCopyVS = NULL; |
| 1764 | } |
| 1765 | |
| 1766 | if (mCopyRGBAPS) |
| 1767 | { |
| 1768 | mCopyRGBAPS->Release(); |
| 1769 | mCopyRGBAPS = NULL; |
| 1770 | } |
| 1771 | |
| 1772 | if (mCopyRGBPS) |
| 1773 | { |
| 1774 | mCopyRGBPS->Release(); |
| 1775 | mCopyRGBPS = NULL; |
| 1776 | } |
| 1777 | |
| 1778 | if (mCopyLumPS) |
| 1779 | { |
| 1780 | mCopyLumPS->Release(); |
| 1781 | mCopyLumPS = NULL; |
| 1782 | } |
| 1783 | |
| 1784 | if (mCopyLumAlphaPS) |
| 1785 | { |
| 1786 | mCopyLumAlphaPS->Release(); |
| 1787 | mCopyLumAlphaPS = NULL; |
| 1788 | } |
| 1789 | |
| 1790 | mCopyResourcesInitialized = false; |
shannon.woods@transgaming.com | 34f507c | 2013-01-25 21:52:15 +0000 | [diff] [blame] | 1791 | |
| 1792 | if (mClearVB) |
| 1793 | { |
| 1794 | mClearVB->Release(); |
| 1795 | mClearVB = NULL; |
| 1796 | } |
| 1797 | |
| 1798 | if (mClearIL) |
| 1799 | { |
| 1800 | mClearIL->Release(); |
| 1801 | mClearIL = NULL; |
| 1802 | } |
| 1803 | |
| 1804 | if (mClearVS) |
| 1805 | { |
| 1806 | mClearVS->Release(); |
| 1807 | mClearVS = NULL; |
| 1808 | } |
| 1809 | |
| 1810 | if (mClearPS) |
| 1811 | { |
| 1812 | mClearPS->Release(); |
| 1813 | mClearPS = NULL; |
| 1814 | } |
| 1815 | |
| 1816 | if (mClearScissorRS) |
| 1817 | { |
| 1818 | mClearScissorRS->Release(); |
| 1819 | mClearScissorRS = NULL; |
| 1820 | } |
| 1821 | |
| 1822 | if (mClearNoScissorRS) |
| 1823 | { |
| 1824 | mClearNoScissorRS->Release(); |
| 1825 | mClearNoScissorRS = NULL; |
| 1826 | } |
| 1827 | |
| 1828 | mClearResourcesInitialized = false; |
shannon.woods@transgaming.com | 5fb979d | 2013-01-25 21:53:04 +0000 | [diff] [blame] | 1829 | |
| 1830 | if (mDriverConstantBufferVS) |
| 1831 | { |
| 1832 | mDriverConstantBufferVS->Release(); |
| 1833 | mDriverConstantBufferVS = NULL; |
| 1834 | } |
| 1835 | |
| 1836 | if (mDriverConstantBufferPS) |
| 1837 | { |
| 1838 | mDriverConstantBufferPS->Release(); |
| 1839 | mDriverConstantBufferPS = NULL; |
| 1840 | } |
daniel@transgaming.com | bdf787f | 2013-02-01 03:20:36 +0000 | [diff] [blame] | 1841 | |
| 1842 | if (mSyncQuery) |
| 1843 | { |
| 1844 | mSyncQuery->Release(); |
| 1845 | mSyncQuery = NULL; |
| 1846 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 1849 | void Renderer11::notifyDeviceLost() |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1850 | { |
| 1851 | mDeviceLost = true; |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 1852 | mDisplay->notifyDeviceLost(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | bool Renderer11::isDeviceLost() |
| 1856 | { |
| 1857 | return mDeviceLost; |
| 1858 | } |
| 1859 | |
| 1860 | // set notify to true to broadcast a message to all contexts of the device loss |
| 1861 | bool Renderer11::testDeviceLost(bool notify) |
| 1862 | { |
| 1863 | bool isLost = false; |
| 1864 | |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1865 | // GetRemovedReason is used to test if the device is removed |
| 1866 | HRESULT result = mDevice->GetDeviceRemovedReason(); |
| 1867 | isLost = d3d11::isDeviceLostError(result); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1868 | |
| 1869 | if (isLost) |
| 1870 | { |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1871 | // Log error if this is a new device lost event |
| 1872 | if (mDeviceLost == false) |
| 1873 | { |
| 1874 | ERR("The D3D11 device was removed: 0x%08X", result); |
| 1875 | } |
| 1876 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1877 | // ensure we note the device loss -- |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 1878 | // we'll probably get this done again by notifyDeviceLost |
| 1879 | // but best to remember it! |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1880 | // Note that we don't want to clear the device loss status here |
| 1881 | // -- this needs to be done by resetDevice |
| 1882 | mDeviceLost = true; |
| 1883 | if (notify) |
| 1884 | { |
shannon.woods@transgaming.com | eb049e2 | 2013-02-28 23:04:49 +0000 | [diff] [blame] | 1885 | notifyDeviceLost(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | return isLost; |
| 1890 | } |
| 1891 | |
| 1892 | bool Renderer11::testDeviceResettable() |
| 1893 | { |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1894 | // determine if the device is resettable by creating a dummy device |
| 1895 | 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] | 1896 | |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1897 | if (D3D11CreateDevice == NULL) |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1898 | { |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1899 | return false; |
| 1900 | } |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1901 | |
| 1902 | D3D_FEATURE_LEVEL featureLevel[] = |
| 1903 | { |
| 1904 | D3D_FEATURE_LEVEL_11_0, |
| 1905 | D3D_FEATURE_LEVEL_10_1, |
| 1906 | D3D_FEATURE_LEVEL_10_0, |
| 1907 | }; |
| 1908 | |
| 1909 | ID3D11Device* dummyDevice; |
| 1910 | D3D_FEATURE_LEVEL dummyFeatureLevel; |
| 1911 | ID3D11DeviceContext* dummyContext; |
| 1912 | |
| 1913 | HRESULT result = D3D11CreateDevice(NULL, |
| 1914 | D3D_DRIVER_TYPE_HARDWARE, |
| 1915 | NULL, |
| 1916 | #if defined(_DEBUG) |
| 1917 | D3D11_CREATE_DEVICE_DEBUG, |
| 1918 | #else |
| 1919 | 0, |
| 1920 | #endif |
| 1921 | featureLevel, |
| 1922 | sizeof(featureLevel)/sizeof(featureLevel[0]), |
| 1923 | D3D11_SDK_VERSION, |
| 1924 | &dummyDevice, |
| 1925 | &dummyFeatureLevel, |
| 1926 | &dummyContext); |
| 1927 | |
| 1928 | if (!mDevice || FAILED(result)) |
| 1929 | { |
| 1930 | return false; |
| 1931 | } |
| 1932 | |
| 1933 | dummyContext->Release(); |
| 1934 | dummyDevice->Release(); |
| 1935 | |
| 1936 | return true; |
| 1937 | } |
| 1938 | |
| 1939 | void Renderer11::release() |
| 1940 | { |
| 1941 | releaseDeviceResources(); |
| 1942 | |
| 1943 | if (mDxgiFactory) |
| 1944 | { |
| 1945 | mDxgiFactory->Release(); |
| 1946 | mDxgiFactory = NULL; |
| 1947 | } |
| 1948 | |
| 1949 | if (mDxgiAdapter) |
| 1950 | { |
| 1951 | mDxgiAdapter->Release(); |
| 1952 | mDxgiAdapter = NULL; |
| 1953 | } |
| 1954 | |
| 1955 | if (mDeviceContext) |
| 1956 | { |
| 1957 | mDeviceContext->ClearState(); |
| 1958 | mDeviceContext->Flush(); |
| 1959 | mDeviceContext->Release(); |
| 1960 | mDeviceContext = NULL; |
| 1961 | } |
| 1962 | |
| 1963 | if (mDevice) |
| 1964 | { |
| 1965 | mDevice->Release(); |
| 1966 | mDevice = NULL; |
| 1967 | } |
| 1968 | |
| 1969 | if (mD3d11Module) |
| 1970 | { |
| 1971 | FreeLibrary(mD3d11Module); |
| 1972 | mD3d11Module = NULL; |
| 1973 | } |
| 1974 | |
| 1975 | if (mDxgiModule) |
| 1976 | { |
| 1977 | FreeLibrary(mDxgiModule); |
| 1978 | mDxgiModule = NULL; |
| 1979 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | bool Renderer11::resetDevice() |
| 1983 | { |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1984 | // recreate everything |
| 1985 | release(); |
| 1986 | EGLint result = initialize(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1987 | |
shannon.woods@transgaming.com | ddd6c80 | 2013-02-28 23:05:14 +0000 | [diff] [blame] | 1988 | if (result != EGL_SUCCESS) |
| 1989 | { |
| 1990 | ERR("Could not reinitialize D3D11 device: %08X", result); |
| 1991 | return false; |
| 1992 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1993 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 1994 | mDeviceLost = false; |
| 1995 | |
| 1996 | return true; |
| 1997 | } |
| 1998 | |
| 1999 | DWORD Renderer11::getAdapterVendor() const |
| 2000 | { |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 2001 | return mAdapterDescription.VendorId; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
daniel@transgaming.com | ca1ac1f | 2013-01-11 04:13:05 +0000 | [diff] [blame] | 2004 | std::string Renderer11::getRendererDescription() const |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2005 | { |
daniel@transgaming.com | ca1ac1f | 2013-01-11 04:13:05 +0000 | [diff] [blame] | 2006 | std::ostringstream rendererString; |
| 2007 | |
| 2008 | rendererString << mDescription; |
| 2009 | rendererString << " Direct3D11"; |
| 2010 | |
| 2011 | rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel(); |
| 2012 | rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel(); |
| 2013 | |
| 2014 | return rendererString.str(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | GUID Renderer11::getAdapterIdentifier() const |
| 2018 | { |
shannon.woods@transgaming.com | 43db795 | 2013-02-28 23:04:28 +0000 | [diff] [blame] | 2019 | // Use the adapter LUID as our adapter ID |
| 2020 | // This number is local to a machine is only guaranteed to be unique between restarts |
| 2021 | META_ASSERT(sizeof(LUID) <= sizeof(GUID)); |
| 2022 | GUID adapterId = {0}; |
| 2023 | memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID)); |
| 2024 | return adapterId; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
shannon.woods@transgaming.com | bec04bf | 2013-01-25 21:53:52 +0000 | [diff] [blame] | 2027 | bool Renderer11::getBGRATextureSupport() const |
| 2028 | { |
| 2029 | return mBGRATextureSupport; |
| 2030 | } |
| 2031 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2032 | bool Renderer11::getDXT1TextureSupport() |
| 2033 | { |
| 2034 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 2035 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2036 | return false; |
| 2037 | } |
| 2038 | |
| 2039 | bool Renderer11::getDXT3TextureSupport() |
| 2040 | { |
| 2041 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 2042 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2043 | return false; |
| 2044 | } |
| 2045 | |
| 2046 | bool Renderer11::getDXT5TextureSupport() |
| 2047 | { |
| 2048 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 2049 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2050 | return false; |
| 2051 | } |
| 2052 | |
| 2053 | bool Renderer11::getDepthTextureSupport() const |
| 2054 | { |
| 2055 | // TODO |
daniel@transgaming.com | 1f811f5 | 2012-11-28 20:57:39 +0000 | [diff] [blame] | 2056 | // UNIMPLEMENTED(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2057 | return false; |
| 2058 | } |
| 2059 | |
| 2060 | bool Renderer11::getFloat32TextureSupport(bool *filtering, bool *renderable) |
| 2061 | { |
shannon.woods@transgaming.com | 9cdced6 | 2013-02-28 23:07:31 +0000 | [diff] [blame] | 2062 | *renderable = mFloat32RenderSupport; |
| 2063 | *filtering = mFloat32FilterSupport; |
| 2064 | return mFloat32TextureSupport; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | bool Renderer11::getFloat16TextureSupport(bool *filtering, bool *renderable) |
| 2068 | { |
shannon.woods@transgaming.com | 9cdced6 | 2013-02-28 23:07:31 +0000 | [diff] [blame] | 2069 | *renderable = mFloat16RenderSupport; |
| 2070 | *filtering = mFloat16FilterSupport; |
| 2071 | return mFloat16TextureSupport; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | bool Renderer11::getLuminanceTextureSupport() |
| 2075 | { |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2076 | return false; |
| 2077 | } |
| 2078 | |
| 2079 | bool Renderer11::getLuminanceAlphaTextureSupport() |
| 2080 | { |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2081 | return false; |
| 2082 | } |
| 2083 | |
| 2084 | bool Renderer11::getTextureFilterAnisotropySupport() const |
| 2085 | { |
shannon.woods@transgaming.com | 1abd797 | 2013-02-28 23:06:58 +0000 | [diff] [blame] | 2086 | return true; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | float Renderer11::getTextureMaxAnisotropy() const |
| 2090 | { |
shannon.woods@transgaming.com | 1abd797 | 2013-02-28 23:06:58 +0000 | [diff] [blame] | 2091 | switch (mFeatureLevel) |
| 2092 | { |
| 2093 | case D3D_FEATURE_LEVEL_11_0: |
| 2094 | return D3D11_MAX_MAXANISOTROPY; |
| 2095 | case D3D_FEATURE_LEVEL_10_1: |
| 2096 | case D3D_FEATURE_LEVEL_10_0: |
| 2097 | return D3D10_MAX_MAXANISOTROPY; |
| 2098 | default: UNREACHABLE(); |
| 2099 | return 0; |
| 2100 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | bool Renderer11::getEventQuerySupport() |
| 2104 | { |
shannon.woods@transgaming.com | be58aa0 | 2013-02-28 23:03:55 +0000 | [diff] [blame] | 2105 | return true; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 2108 | Range Renderer11::getViewportBounds() const |
| 2109 | { |
| 2110 | switch (mFeatureLevel) |
| 2111 | { |
| 2112 | case D3D_FEATURE_LEVEL_11_0: |
| 2113 | return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX); |
| 2114 | case D3D_FEATURE_LEVEL_10_1: |
| 2115 | case D3D_FEATURE_LEVEL_10_0: |
| 2116 | return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX); |
| 2117 | default: UNREACHABLE(); |
| 2118 | return Range(0, 0); |
| 2119 | } |
| 2120 | } |
| 2121 | |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 2122 | unsigned int Renderer11::getMaxVertexTextureImageUnits() const |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2123 | { |
shannon.woods@transgaming.com | 233fe95 | 2013-01-25 21:51:57 +0000 | [diff] [blame] | 2124 | META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS); |
| 2125 | switch (mFeatureLevel) |
| 2126 | { |
| 2127 | case D3D_FEATURE_LEVEL_11_0: |
| 2128 | case D3D_FEATURE_LEVEL_10_1: |
| 2129 | case D3D_FEATURE_LEVEL_10_0: |
| 2130 | return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4; |
| 2131 | default: UNREACHABLE(); |
| 2132 | return 0; |
| 2133 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
shannon.woods@transgaming.com | 76cd88c | 2013-01-25 21:54:36 +0000 | [diff] [blame] | 2136 | unsigned int Renderer11::getMaxCombinedTextureImageUnits() const |
| 2137 | { |
| 2138 | return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits(); |
| 2139 | } |
| 2140 | |
shannon.woods@transgaming.com | 254317d | 2013-01-25 21:54:09 +0000 | [diff] [blame] | 2141 | int Renderer11::getMaxVertexUniformVectors() const |
| 2142 | { |
shannon.woods@transgaming.com | 4e48204 | 2013-01-25 21:54:18 +0000 | [diff] [blame] | 2143 | META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT); |
| 2144 | ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0); |
| 2145 | return MAX_VERTEX_UNIFORM_VECTORS_D3D11; |
shannon.woods@transgaming.com | 254317d | 2013-01-25 21:54:09 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | int Renderer11::getMaxFragmentUniformVectors() const |
| 2149 | { |
shannon.woods@transgaming.com | 4e48204 | 2013-01-25 21:54:18 +0000 | [diff] [blame] | 2150 | META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT); |
| 2151 | ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0); |
| 2152 | return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11; |
shannon.woods@transgaming.com | 254317d | 2013-01-25 21:54:09 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
shannon.woods@transgaming.com | 28d268e | 2013-01-25 21:54:26 +0000 | [diff] [blame] | 2155 | int Renderer11::getMaxVaryingVectors() const |
| 2156 | { |
| 2157 | META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT); |
| 2158 | switch (mFeatureLevel) |
| 2159 | { |
| 2160 | case D3D_FEATURE_LEVEL_11_0: |
| 2161 | return D3D11_VS_OUTPUT_REGISTER_COUNT; |
| 2162 | case D3D_FEATURE_LEVEL_10_1: |
| 2163 | case D3D_FEATURE_LEVEL_10_0: |
| 2164 | return D3D10_VS_OUTPUT_REGISTER_COUNT; |
| 2165 | default: UNREACHABLE(); |
| 2166 | return 0; |
| 2167 | } |
| 2168 | } |
| 2169 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2170 | bool Renderer11::getNonPower2TextureSupport() const |
| 2171 | { |
shannon.woods@transgaming.com | 03951cf | 2013-01-25 21:57:01 +0000 | [diff] [blame] | 2172 | switch (mFeatureLevel) |
| 2173 | { |
| 2174 | case D3D_FEATURE_LEVEL_11_0: |
| 2175 | case D3D_FEATURE_LEVEL_10_1: |
| 2176 | case D3D_FEATURE_LEVEL_10_0: |
| 2177 | return true; |
| 2178 | default: UNREACHABLE(); |
| 2179 | return false; |
| 2180 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | bool Renderer11::getOcclusionQuerySupport() const |
| 2184 | { |
shannon.woods@transgaming.com | 8b7606a | 2013-02-28 23:03:47 +0000 | [diff] [blame] | 2185 | switch (mFeatureLevel) |
| 2186 | { |
| 2187 | case D3D_FEATURE_LEVEL_11_0: |
| 2188 | case D3D_FEATURE_LEVEL_10_1: |
| 2189 | case D3D_FEATURE_LEVEL_10_0: |
| 2190 | return true; |
| 2191 | default: UNREACHABLE(); |
| 2192 | return false; |
| 2193 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | bool Renderer11::getInstancingSupport() const |
| 2197 | { |
daniel@transgaming.com | fe32464 | 2013-02-01 03:20:11 +0000 | [diff] [blame] | 2198 | switch (mFeatureLevel) |
| 2199 | { |
| 2200 | case D3D_FEATURE_LEVEL_11_0: |
| 2201 | case D3D_FEATURE_LEVEL_10_1: |
| 2202 | case D3D_FEATURE_LEVEL_10_0: |
| 2203 | return true; |
| 2204 | default: UNREACHABLE(); |
| 2205 | return false; |
| 2206 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | bool Renderer11::getShareHandleSupport() const |
| 2210 | { |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame] | 2211 | // We only currently support share handles with BGRA surfaces, because |
| 2212 | // chrome needs BGRA. Once chrome fixes this, we should always support them. |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2213 | // PIX doesn't seem to support using share handles, so disable them. |
shannon.woods@transgaming.com | c60c521 | 2013-01-25 21:54:01 +0000 | [diff] [blame] | 2214 | return getBGRATextureSupport() && !gl::perfActive(); |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
daniel@transgaming.com | 7629bb6 | 2013-01-11 04:12:28 +0000 | [diff] [blame] | 2217 | bool Renderer11::getDerivativeInstructionSupport() const |
| 2218 | { |
shannon.woods@transgaming.com | 09fd945 | 2013-02-28 23:02:28 +0000 | [diff] [blame] | 2219 | switch (mFeatureLevel) |
| 2220 | { |
| 2221 | case D3D_FEATURE_LEVEL_11_0: |
| 2222 | case D3D_FEATURE_LEVEL_10_1: |
| 2223 | case D3D_FEATURE_LEVEL_10_0: |
| 2224 | return true; |
| 2225 | default: UNREACHABLE(); |
| 2226 | return false; |
| 2227 | } |
daniel@transgaming.com | 7629bb6 | 2013-01-11 04:12:28 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 2230 | int Renderer11::getMajorShaderModel() const |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2231 | { |
daniel@transgaming.com | 9549bea | 2012-11-28 20:57:23 +0000 | [diff] [blame] | 2232 | switch (mFeatureLevel) |
| 2233 | { |
| 2234 | 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] | 2235 | 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] | 2236 | case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4 |
| 2237 | default: UNREACHABLE(); return 0; |
| 2238 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
daniel@transgaming.com | ca1ac1f | 2013-01-11 04:13:05 +0000 | [diff] [blame] | 2241 | int Renderer11::getMinorShaderModel() const |
| 2242 | { |
| 2243 | switch (mFeatureLevel) |
| 2244 | { |
| 2245 | case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0 |
| 2246 | case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1 |
| 2247 | case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0 |
| 2248 | default: UNREACHABLE(); return 0; |
| 2249 | } |
| 2250 | } |
| 2251 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2252 | float Renderer11::getMaxPointSize() const |
| 2253 | { |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 2254 | // choose a reasonable maximum. we enforce this in the shader. |
| 2255 | // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size) |
| 2256 | return 1024.0f; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
shannon.woods@transgaming.com | 8ce2f8f | 2013-02-28 23:07:10 +0000 | [diff] [blame] | 2259 | int Renderer11::getMaxViewportDimension() const |
| 2260 | { |
| 2261 | // Clamp viewport width/height to half of the maximum right/bottom edge |
| 2262 | switch (mFeatureLevel) |
| 2263 | { |
| 2264 | case D3D_FEATURE_LEVEL_11_0: |
| 2265 | return D3D11_VIEWPORT_BOUNDS_MAX - D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384 |
| 2266 | case D3D_FEATURE_LEVEL_10_1: |
| 2267 | case D3D_FEATURE_LEVEL_10_0: |
| 2268 | return D3D10_VIEWPORT_BOUNDS_MAX - D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192 |
| 2269 | default: UNREACHABLE(); |
| 2270 | return 0; |
| 2271 | } |
| 2272 | } |
| 2273 | |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2274 | int Renderer11::getMaxTextureWidth() const |
| 2275 | { |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 2276 | switch (mFeatureLevel) |
| 2277 | { |
| 2278 | case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384 |
| 2279 | case D3D_FEATURE_LEVEL_10_1: |
| 2280 | case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192 |
| 2281 | default: UNREACHABLE(); return 0; |
| 2282 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | int Renderer11::getMaxTextureHeight() const |
| 2286 | { |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 2287 | switch (mFeatureLevel) |
| 2288 | { |
| 2289 | case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384 |
| 2290 | case D3D_FEATURE_LEVEL_10_1: |
| 2291 | case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192 |
| 2292 | default: UNREACHABLE(); return 0; |
| 2293 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | bool Renderer11::get32BitIndexSupport() const |
| 2297 | { |
daniel@transgaming.com | 25072f6 | 2012-11-28 19:31:32 +0000 | [diff] [blame] | 2298 | switch (mFeatureLevel) |
| 2299 | { |
| 2300 | case D3D_FEATURE_LEVEL_11_0: |
| 2301 | case D3D_FEATURE_LEVEL_10_1: |
| 2302 | case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true |
| 2303 | default: UNREACHABLE(); return false; |
| 2304 | } |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | int Renderer11::getMinSwapInterval() const |
| 2308 | { |
daniel@transgaming.com | 8c7b1a9 | 2012-11-28 19:34:06 +0000 | [diff] [blame] | 2309 | return 0; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
| 2312 | int Renderer11::getMaxSwapInterval() const |
| 2313 | { |
daniel@transgaming.com | 8c7b1a9 | 2012-11-28 19:34:06 +0000 | [diff] [blame] | 2314 | return 4; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | int Renderer11::getMaxSupportedSamples() const |
| 2318 | { |
shannon.woods@transgaming.com | df2fd57 | 2013-02-28 23:05:40 +0000 | [diff] [blame] | 2319 | return mMaxSupportedSamples; |
daniel@transgaming.com | 1d6aff2 | 2012-11-28 19:30:42 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
shannon.woods@transgaming.com | 88fbd0f | 2013-02-28 23:05:46 +0000 | [diff] [blame] | 2322 | int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const |
| 2323 | { |
| 2324 | if (requested == 0) |
| 2325 | { |
| 2326 | return 0; |
| 2327 | } |
| 2328 | |
| 2329 | MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format); |
| 2330 | if (iter != mMultisampleSupportMap.end()) |
| 2331 | { |
| 2332 | const MultisampleSupportInfo& info = iter->second; |
| 2333 | for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++) |
| 2334 | { |
| 2335 | if (info.qualityLevels[i] > 0) |
| 2336 | { |
| 2337 | return i + 1; |
| 2338 | } |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | return -1; |
| 2343 | } |
| 2344 | |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 2345 | bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source) |
daniel@transgaming.com | ad6aee7 | 2012-11-28 19:33:42 +0000 | [diff] [blame] | 2346 | { |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2347 | if (source && dest) |
| 2348 | { |
| 2349 | TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance()); |
| 2350 | TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance()); |
| 2351 | |
daniel@transgaming.com | e9cf5e7 | 2013-01-11 04:06:55 +0000 | [diff] [blame] | 2352 | mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture()); |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2353 | return true; |
| 2354 | } |
| 2355 | |
daniel@transgaming.com | ad6aee7 | 2012-11-28 19:33:42 +0000 | [diff] [blame] | 2356 | return false; |
| 2357 | } |
| 2358 | |
daniel@transgaming.com | 87705f8 | 2012-12-20 21:10:45 +0000 | [diff] [blame] | 2359 | bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source) |
daniel@transgaming.com | ad6aee7 | 2012-11-28 19:33:42 +0000 | [diff] [blame] | 2360 | { |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2361 | if (source && dest) |
| 2362 | { |
daniel@transgaming.com | e9cf5e7 | 2013-01-11 04:06:55 +0000 | [diff] [blame] | 2363 | TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance()); |
| 2364 | TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance()); |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2365 | |
daniel@transgaming.com | e9cf5e7 | 2013-01-11 04:06:55 +0000 | [diff] [blame] | 2366 | mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture()); |
daniel@transgaming.com | b1c208f | 2013-01-11 04:06:49 +0000 | [diff] [blame] | 2367 | return true; |
| 2368 | } |
| 2369 | |
daniel@transgaming.com | ad6aee7 | 2012-11-28 19:33:42 +0000 | [diff] [blame] | 2370 | return false; |
| 2371 | } |
| 2372 | |
shannon.woods@transgaming.com | 664916b | 2013-01-25 21:50:32 +0000 | [diff] [blame] | 2373 | 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] | 2374 | GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level) |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2375 | { |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2376 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 2377 | if (!colorbuffer) |
| 2378 | { |
| 2379 | ERR("Failed to retrieve the color buffer from the frame buffer."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2380 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); |
| 2384 | if (!sourceRenderTarget) |
| 2385 | { |
| 2386 | ERR("Failed to retrieve the render target from the frame buffer."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2387 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView(); |
| 2391 | if (!source) |
| 2392 | { |
| 2393 | ERR("Failed to retrieve the render target view from the render target."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2394 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance()); |
| 2398 | if (!storage11) |
| 2399 | { |
| 2400 | source->Release(); |
| 2401 | ERR("Failed to retrieve the texture storage from the destination."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2402 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level)); |
| 2406 | if (!destRenderTarget) |
| 2407 | { |
| 2408 | source->Release(); |
| 2409 | ERR("Failed to retrieve the render target from the destination storage."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2410 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView(); |
| 2414 | if (!dest) |
| 2415 | { |
| 2416 | source->Release(); |
| 2417 | ERR("Failed to retrieve the render target view from the destination render target."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2418 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | gl::Rectangle destRect; |
| 2422 | destRect.x = xoffset; |
| 2423 | destRect.y = yoffset; |
| 2424 | destRect.width = sourceRect.width; |
| 2425 | destRect.height = sourceRect.height; |
| 2426 | |
| 2427 | bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), |
| 2428 | dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat); |
| 2429 | |
| 2430 | source->Release(); |
| 2431 | dest->Release(); |
| 2432 | |
| 2433 | return ret; |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
shannon.woods@transgaming.com | 664916b | 2013-01-25 21:50:32 +0000 | [diff] [blame] | 2436 | 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] | 2437 | GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level) |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2438 | { |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2439 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 2440 | if (!colorbuffer) |
| 2441 | { |
| 2442 | ERR("Failed to retrieve the color buffer from the frame buffer."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2443 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); |
| 2447 | if (!sourceRenderTarget) |
| 2448 | { |
| 2449 | ERR("Failed to retrieve the render target from the frame buffer."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2450 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView(); |
| 2454 | if (!source) |
| 2455 | { |
| 2456 | ERR("Failed to retrieve the render target view from the render target."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2457 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance()); |
| 2461 | if (!storage11) |
| 2462 | { |
| 2463 | source->Release(); |
| 2464 | ERR("Failed to retrieve the texture storage from the destination."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2465 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
| 2468 | RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(target, level)); |
| 2469 | if (!destRenderTarget) |
| 2470 | { |
| 2471 | source->Release(); |
| 2472 | ERR("Failed to retrieve the render target from the destination storage."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2473 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView(); |
| 2477 | if (!dest) |
| 2478 | { |
| 2479 | source->Release(); |
| 2480 | ERR("Failed to retrieve the render target view from the destination render target."); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2481 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | gl::Rectangle destRect; |
| 2485 | destRect.x = xoffset; |
| 2486 | destRect.y = yoffset; |
| 2487 | destRect.width = sourceRect.width; |
| 2488 | destRect.height = sourceRect.height; |
| 2489 | |
| 2490 | bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), |
| 2491 | dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat); |
| 2492 | |
| 2493 | source->Release(); |
| 2494 | dest->Release(); |
| 2495 | |
| 2496 | return ret; |
| 2497 | } |
| 2498 | |
| 2499 | bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight, |
| 2500 | ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat) |
| 2501 | { |
| 2502 | HRESULT result; |
| 2503 | |
| 2504 | if (!mCopyResourcesInitialized) |
| 2505 | { |
| 2506 | ASSERT(!mCopyVB && !mCopySampler && !mCopyIL && !mCopyVS && !mCopyRGBAPS && !mCopyRGBPS && !mCopyLumPS && !mCopyLumAlphaPS); |
| 2507 | |
| 2508 | D3D11_BUFFER_DESC vbDesc; |
| 2509 | vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4; |
| 2510 | vbDesc.Usage = D3D11_USAGE_DYNAMIC; |
| 2511 | vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; |
| 2512 | vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| 2513 | vbDesc.MiscFlags = 0; |
| 2514 | vbDesc.StructureByteStride = 0; |
| 2515 | |
| 2516 | result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB); |
| 2517 | ASSERT(SUCCEEDED(result)); |
| 2518 | d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer"); |
| 2519 | |
| 2520 | D3D11_SAMPLER_DESC samplerDesc; |
| 2521 | samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; |
| 2522 | samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; |
| 2523 | samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; |
| 2524 | samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; |
| 2525 | samplerDesc.MipLODBias = 0.0f; |
| 2526 | samplerDesc.MaxAnisotropy = 0; |
| 2527 | samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; |
| 2528 | samplerDesc.BorderColor[0] = 0.0f; |
| 2529 | samplerDesc.BorderColor[1] = 0.0f; |
| 2530 | samplerDesc.BorderColor[2] = 0.0f; |
| 2531 | samplerDesc.BorderColor[3] = 0.0f; |
| 2532 | samplerDesc.MinLOD = 0.0f; |
| 2533 | samplerDesc.MaxLOD = 0.0f; |
| 2534 | |
| 2535 | result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler); |
| 2536 | ASSERT(SUCCEEDED(result)); |
| 2537 | d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler"); |
| 2538 | |
| 2539 | D3D11_INPUT_ELEMENT_DESC quadLayout[] = |
| 2540 | { |
| 2541 | { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 2542 | { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 2543 | }; |
| 2544 | |
| 2545 | result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mCopyIL); |
| 2546 | ASSERT(SUCCEEDED(result)); |
| 2547 | d3d11::SetDebugName(mCopyIL, "Renderer11 copy texture input layout"); |
| 2548 | |
| 2549 | result = mDevice->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mCopyVS); |
| 2550 | ASSERT(SUCCEEDED(result)); |
| 2551 | d3d11::SetDebugName(mCopyVS, "Renderer11 copy texture vertex shader"); |
| 2552 | |
| 2553 | result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mCopyRGBAPS); |
| 2554 | ASSERT(SUCCEEDED(result)); |
| 2555 | d3d11::SetDebugName(mCopyRGBAPS, "Renderer11 copy texture RGBA pixel shader"); |
| 2556 | |
| 2557 | result = mDevice->CreatePixelShader(g_PS_PassthroughRGB, sizeof(g_PS_PassthroughRGB), NULL, &mCopyRGBPS); |
| 2558 | ASSERT(SUCCEEDED(result)); |
| 2559 | d3d11::SetDebugName(mCopyRGBPS, "Renderer11 copy texture RGB pixel shader"); |
| 2560 | |
| 2561 | result = mDevice->CreatePixelShader(g_PS_PassthroughLum, sizeof(g_PS_PassthroughLum), NULL, &mCopyLumPS); |
| 2562 | ASSERT(SUCCEEDED(result)); |
| 2563 | d3d11::SetDebugName(mCopyLumPS, "Renderer11 copy texture luminance pixel shader"); |
| 2564 | |
| 2565 | result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha, sizeof(g_PS_PassthroughLumAlpha), NULL, &mCopyLumAlphaPS); |
| 2566 | ASSERT(SUCCEEDED(result)); |
| 2567 | d3d11::SetDebugName(mCopyLumAlphaPS, "Renderer11 copy texture luminance alpha pixel shader"); |
| 2568 | |
| 2569 | mCopyResourcesInitialized = true; |
| 2570 | } |
| 2571 | |
| 2572 | // Verify the source and destination area sizes |
| 2573 | if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) || |
| 2574 | sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) || |
| 2575 | destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) || |
| 2576 | destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight)) |
| 2577 | { |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2578 | return gl::error(GL_INVALID_VALUE, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | // Set vertices |
| 2582 | D3D11_MAPPED_SUBRESOURCE mappedResource; |
| 2583 | result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); |
| 2584 | if (FAILED(result)) |
| 2585 | { |
| 2586 | ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result); |
shannon.woods@transgaming.com | 779aa26 | 2013-02-28 23:04:58 +0000 | [diff] [blame] | 2587 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData); |
| 2591 | |
| 2592 | // Create a quad in homogeneous coordinates |
shannon.woods@transgaming.com | c25537e | 2013-02-28 23:06:03 +0000 | [diff] [blame] | 2593 | float x1 = (destArea.x / float(destWidth)) * 2.0f - 1.0f; |
| 2594 | float y1 = (destArea.y / float(destHeight)) * 2.0f - 1.0f; |
| 2595 | float x2 = ((destArea.x + destArea.width) / float(destWidth)) * 2.0f - 1.0f; |
| 2596 | float y2 = ((destArea.y + destArea.height) / float(destHeight)) * 2.0f - 1.0f; |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2597 | |
| 2598 | float u1 = sourceArea.x / float(sourceWidth); |
| 2599 | float v1 = sourceArea.y / float(sourceHeight); |
| 2600 | float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth); |
| 2601 | float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight); |
| 2602 | |
| 2603 | d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2); |
| 2604 | d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1); |
| 2605 | d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2); |
| 2606 | d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1); |
| 2607 | |
| 2608 | mDeviceContext->Unmap(mCopyVB, 0); |
| 2609 | |
| 2610 | static UINT stride = sizeof(d3d11::PositionTexCoordVertex); |
| 2611 | static UINT startIdx = 0; |
| 2612 | mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx); |
| 2613 | |
| 2614 | // Apply state |
shannon.woods@transgaming.com | c25537e | 2013-02-28 23:06:03 +0000 | [diff] [blame] | 2615 | mDeviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF); |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 2616 | mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF); |
| 2617 | mDeviceContext->RSSetState(NULL); |
| 2618 | |
| 2619 | // Apply shaders |
| 2620 | mDeviceContext->IASetInputLayout(mCopyIL); |
| 2621 | mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); |
| 2622 | mDeviceContext->VSSetShader(mCopyVS, NULL, 0); |
| 2623 | |
| 2624 | ID3D11PixelShader *ps = NULL; |
| 2625 | switch(destFormat) |
| 2626 | { |
| 2627 | case GL_RGBA: ps = mCopyRGBAPS; break; |
| 2628 | case GL_RGB: ps = mCopyRGBPS; break; |
| 2629 | case GL_ALPHA: ps = mCopyRGBAPS; break; |
| 2630 | case GL_BGRA_EXT: ps = mCopyRGBAPS; break; |
| 2631 | case GL_LUMINANCE: ps = mCopyLumPS; break; |
| 2632 | case GL_LUMINANCE_ALPHA: ps = mCopyLumAlphaPS; break; |
| 2633 | default: UNREACHABLE(); ps = NULL; break; |
| 2634 | } |
| 2635 | |
| 2636 | mDeviceContext->PSSetShader(ps, NULL, 0); |
| 2637 | |
| 2638 | // Unset the currently bound shader resource to avoid conflicts |
| 2639 | static ID3D11ShaderResourceView *const nullSRV = NULL; |
| 2640 | mDeviceContext->PSSetShaderResources(0, 1, &nullSRV); |
| 2641 | |
| 2642 | // Apply render targets |
| 2643 | mDeviceContext->OMSetRenderTargets(1, &dest, NULL); |
| 2644 | |
| 2645 | // Set the viewport |
| 2646 | D3D11_VIEWPORT viewport; |
| 2647 | viewport.TopLeftX = 0; |
| 2648 | viewport.TopLeftY = 0; |
| 2649 | viewport.Width = destWidth; |
| 2650 | viewport.Height = destHeight; |
| 2651 | viewport.MinDepth = 0.0f; |
| 2652 | viewport.MaxDepth = 1.0f; |
| 2653 | mDeviceContext->RSSetViewports(1, &viewport); |
| 2654 | |
| 2655 | // Apply textures |
| 2656 | mDeviceContext->PSSetShaderResources(0, 1, &source); |
| 2657 | mDeviceContext->PSSetSamplers(0, 1, &mCopySampler); |
| 2658 | |
| 2659 | // Draw the quad |
| 2660 | mDeviceContext->Draw(4, 0); |
| 2661 | |
| 2662 | // Unbind textures and render targets and vertex buffer |
| 2663 | mDeviceContext->PSSetShaderResources(0, 1, &nullSRV); |
| 2664 | |
| 2665 | static ID3D11RenderTargetView *const nullRTV = NULL; |
| 2666 | mDeviceContext->OMSetRenderTargets(1, &nullRTV, NULL); |
| 2667 | |
| 2668 | static UINT zero = 0; |
| 2669 | static ID3D11Buffer *const nullBuffer = NULL; |
| 2670 | mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero); |
| 2671 | |
| 2672 | markAllStateDirty(); |
| 2673 | |
| 2674 | return true; |
daniel@transgaming.com | 3838088 | 2012-11-28 19:36:39 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 2677 | RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth) |
| 2678 | { |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame] | 2679 | SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain); |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 2680 | RenderTarget11 *renderTarget = NULL; |
| 2681 | if (depth) |
| 2682 | { |
shannon.woods@transgaming.com | 7e23285 | 2013-02-28 23:06:15 +0000 | [diff] [blame] | 2683 | renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(), |
| 2684 | swapChain11->getDepthStencilTexture(), NULL, |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame] | 2685 | swapChain11->getWidth(), swapChain11->getHeight()); |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 2686 | } |
| 2687 | else |
| 2688 | { |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame] | 2689 | renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(), |
shannon.woods@transgaming.com | 7e23285 | 2013-02-28 23:06:15 +0000 | [diff] [blame] | 2690 | swapChain11->getOffscreenTexture(), |
shannon.woods@transgaming.com | 183408d | 2013-01-25 21:50:07 +0000 | [diff] [blame] | 2691 | swapChain11->getRenderTargetShaderResource(), |
| 2692 | swapChain11->getWidth(), swapChain11->getHeight()); |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 2693 | } |
| 2694 | return renderTarget; |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth) |
| 2698 | { |
daniel@transgaming.com | c9a501d | 2013-01-11 04:08:46 +0000 | [diff] [blame] | 2699 | RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth); |
| 2700 | return renderTarget; |
daniel@transgaming.com | f242365 | 2012-11-28 20:53:50 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 2703 | ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type) |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 2704 | { |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2705 | ShaderExecutable11 *executable = NULL; |
| 2706 | |
| 2707 | switch (type) |
| 2708 | { |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 2709 | case rx::SHADER_VERTEX: |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2710 | { |
| 2711 | ID3D11VertexShader *vshader = NULL; |
| 2712 | HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader); |
| 2713 | ASSERT(SUCCEEDED(result)); |
| 2714 | |
| 2715 | if (vshader) |
| 2716 | { |
daniel@transgaming.com | 7b18d0c | 2012-11-28 21:04:10 +0000 | [diff] [blame] | 2717 | executable = new ShaderExecutable11(function, length, vshader); |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2718 | } |
| 2719 | } |
| 2720 | break; |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 2721 | case rx::SHADER_PIXEL: |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2722 | { |
| 2723 | ID3D11PixelShader *pshader = NULL; |
| 2724 | HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader); |
| 2725 | ASSERT(SUCCEEDED(result)); |
| 2726 | |
| 2727 | if (pshader) |
| 2728 | { |
daniel@transgaming.com | 7b18d0c | 2012-11-28 21:04:10 +0000 | [diff] [blame] | 2729 | executable = new ShaderExecutable11(function, length, pshader); |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2730 | } |
| 2731 | } |
| 2732 | break; |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 2733 | case rx::SHADER_GEOMETRY: |
| 2734 | { |
| 2735 | ID3D11GeometryShader *gshader = NULL; |
| 2736 | HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader); |
| 2737 | ASSERT(SUCCEEDED(result)); |
| 2738 | |
| 2739 | if (gshader) |
| 2740 | { |
| 2741 | executable = new ShaderExecutable11(function, length, gshader); |
| 2742 | } |
| 2743 | } |
| 2744 | break; |
daniel@transgaming.com | a2f9fbe | 2012-11-28 21:03:40 +0000 | [diff] [blame] | 2745 | default: |
| 2746 | UNREACHABLE(); |
| 2747 | break; |
| 2748 | } |
| 2749 | |
| 2750 | return executable; |
daniel@transgaming.com | 5531890 | 2012-11-28 20:58:58 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 2753 | ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type) |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 2754 | { |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2755 | const char *profile = NULL; |
| 2756 | |
| 2757 | switch (type) |
| 2758 | { |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 2759 | case rx::SHADER_VERTEX: |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2760 | profile = "vs_4_0"; |
| 2761 | break; |
shannon.woods@transgaming.com | 69ff776 | 2013-01-25 21:55:24 +0000 | [diff] [blame] | 2762 | case rx::SHADER_PIXEL: |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2763 | profile = "ps_4_0"; |
| 2764 | break; |
shannon.woods@transgaming.com | 3e773bb | 2013-01-25 21:55:47 +0000 | [diff] [blame] | 2765 | case rx::SHADER_GEOMETRY: |
| 2766 | profile = "gs_4_0"; |
| 2767 | break; |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2768 | default: |
| 2769 | UNREACHABLE(); |
| 2770 | return NULL; |
| 2771 | } |
| 2772 | |
daniel@transgaming.com | 25e16af | 2012-11-28 21:05:57 +0000 | [diff] [blame] | 2773 | ID3DBlob *binary = compileToBinary(infoLog, shaderHLSL, profile, false); |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2774 | if (!binary) |
| 2775 | return NULL; |
| 2776 | |
daniel@transgaming.com | 2275f91 | 2012-12-20 21:13:22 +0000 | [diff] [blame] | 2777 | ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type); |
daniel@transgaming.com | 071ee6a | 2012-11-28 21:03:21 +0000 | [diff] [blame] | 2778 | binary->Release(); |
| 2779 | |
| 2780 | return executable; |
| 2781 | } |
| 2782 | |
daniel@transgaming.com | 3f255b4 | 2012-12-20 21:07:35 +0000 | [diff] [blame] | 2783 | VertexBuffer *Renderer11::createVertexBuffer() |
| 2784 | { |
daniel@transgaming.com | 2c4d070 | 2012-12-20 21:08:51 +0000 | [diff] [blame] | 2785 | return new VertexBuffer11(this); |
daniel@transgaming.com | 3f255b4 | 2012-12-20 21:07:35 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
daniel@transgaming.com | 0b6d774 | 2012-12-20 21:09:47 +0000 | [diff] [blame] | 2788 | IndexBuffer *Renderer11::createIndexBuffer() |
| 2789 | { |
daniel@transgaming.com | 11c2af5 | 2012-12-20 21:10:01 +0000 | [diff] [blame] | 2790 | return new IndexBuffer11(this); |
shannon.woods@transgaming.com | cfe787e | 2013-02-28 23:03:35 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
shannon.woods@transgaming.com | 4e52b63 | 2013-02-28 23:08:01 +0000 | [diff] [blame] | 2793 | BufferStorage *Renderer11::createBufferStorage() |
| 2794 | { |
| 2795 | return new BufferStorage11(this); |
| 2796 | } |
| 2797 | |
shannon.woods@transgaming.com | cfe787e | 2013-02-28 23:03:35 +0000 | [diff] [blame] | 2798 | QueryImpl *Renderer11::createQuery(GLenum type) |
| 2799 | { |
shannon.woods@transgaming.com | 8b7606a | 2013-02-28 23:03:47 +0000 | [diff] [blame] | 2800 | return new Query11(this, type); |
shannon.woods@transgaming.com | cfe787e | 2013-02-28 23:03:35 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | FenceImpl *Renderer11::createFence() |
| 2804 | { |
shannon.woods@transgaming.com | be58aa0 | 2013-02-28 23:03:55 +0000 | [diff] [blame] | 2805 | return new Fence11(this); |
daniel@transgaming.com | 0b6d774 | 2012-12-20 21:09:47 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
shannon.woods@transgaming.com | fdeacb8 | 2013-01-25 21:52:34 +0000 | [diff] [blame] | 2808 | bool Renderer11::getRenderTargetResource(gl::Framebuffer *framebuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource) |
| 2809 | { |
| 2810 | gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(); |
| 2811 | if (colorbuffer) |
| 2812 | { |
| 2813 | RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); |
| 2814 | if (renderTarget) |
| 2815 | { |
| 2816 | *subresourceIndex = renderTarget->getSubresourceIndex(); |
| 2817 | |
| 2818 | ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView(); |
| 2819 | if (colorBufferRTV) |
| 2820 | { |
| 2821 | ID3D11Resource *textureResource = NULL; |
| 2822 | colorBufferRTV->GetResource(&textureResource); |
| 2823 | colorBufferRTV->Release(); |
| 2824 | |
| 2825 | if (textureResource) |
| 2826 | { |
| 2827 | HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource); |
| 2828 | textureResource->Release(); |
| 2829 | |
| 2830 | if (SUCCEEDED(result)) |
| 2831 | { |
| 2832 | return true; |
| 2833 | } |
| 2834 | else |
| 2835 | { |
| 2836 | ERR("Failed to extract the ID3D11Texture2D from the render target resource, " |
| 2837 | "HRESULT: 0x%X.", result); |
| 2838 | } |
| 2839 | } |
| 2840 | } |
| 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | return false; |
| 2845 | } |
| 2846 | |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 2847 | bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect, |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2848 | bool blitRenderTarget, bool blitDepthStencil) |
| 2849 | { |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 2850 | if (blitRenderTarget && !blitRect(readTarget, readRect, drawTarget, drawRect, BLIT_RENDERTARGET)) |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 2851 | { |
| 2852 | return false; |
| 2853 | } |
| 2854 | |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 2855 | if (blitDepthStencil && !blitRect(readTarget, readRect, drawTarget, drawRect, BLIT_DEPTHSTENCIL)) |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 2856 | { |
| 2857 | return false; |
| 2858 | } |
| 2859 | |
| 2860 | return true; |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, |
| 2864 | GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels) |
| 2865 | { |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2866 | ID3D11Texture2D *colorBufferTexture = NULL; |
daniel@transgaming.com | 2eb7ab7 | 2013-01-11 04:10:21 +0000 | [diff] [blame] | 2867 | unsigned int subresourceIndex = 0; |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2868 | |
shannon.woods@transgaming.com | fdeacb8 | 2013-01-25 21:52:34 +0000 | [diff] [blame] | 2869 | if (getRenderTargetResource(framebuffer, &subresourceIndex, &colorBufferTexture)) |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2870 | { |
| 2871 | gl::Rectangle area; |
| 2872 | area.x = x; |
| 2873 | area.y = y; |
| 2874 | area.width = width; |
| 2875 | area.height = height; |
| 2876 | |
daniel@transgaming.com | 2eb7ab7 | 2013-01-11 04:10:21 +0000 | [diff] [blame] | 2877 | readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, |
| 2878 | packReverseRowOrder, packAlignment, pixels); |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2879 | |
| 2880 | colorBufferTexture->Release(); |
| 2881 | colorBufferTexture = NULL; |
| 2882 | } |
daniel@transgaming.com | 6c87217 | 2012-11-28 19:39:33 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
daniel@transgaming.com | 244e183 | 2012-12-20 20:52:35 +0000 | [diff] [blame] | 2885 | Image *Renderer11::createImage() |
| 2886 | { |
daniel@transgaming.com | a8aac67 | 2012-12-20 21:08:00 +0000 | [diff] [blame] | 2887 | return new Image11(); |
daniel@transgaming.com | 244e183 | 2012-12-20 20:52:35 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
daniel@transgaming.com | f721fdb | 2012-12-20 20:53:11 +0000 | [diff] [blame] | 2890 | void Renderer11::generateMipmap(Image *dest, Image *src) |
| 2891 | { |
shannon.woods@transgaming.com | 2b132f4 | 2013-01-25 21:52:47 +0000 | [diff] [blame] | 2892 | Image11 *dest11 = Image11::makeImage11(dest); |
| 2893 | Image11 *src11 = Image11::makeImage11(src); |
| 2894 | Image11::generateMipmap(dest11, src11); |
daniel@transgaming.com | f721fdb | 2012-12-20 20:53:11 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 2897 | TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain) |
| 2898 | { |
daniel@transgaming.com | 36670db | 2013-01-11 04:08:22 +0000 | [diff] [blame] | 2899 | SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain); |
| 2900 | return new TextureStorage11_2D(this, swapChain11); |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) |
| 2904 | { |
daniel@transgaming.com | 36670db | 2013-01-11 04:08:22 +0000 | [diff] [blame] | 2905 | return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height); |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
| 2908 | TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) |
| 2909 | { |
daniel@transgaming.com | 36670db | 2013-01-11 04:08:22 +0000 | [diff] [blame] | 2910 | return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size); |
daniel@transgaming.com | 413d271 | 2012-12-20 21:11:04 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2913 | static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType) |
| 2914 | { |
| 2915 | if (sourceFormat == DXGI_FORMAT_A8_UNORM && |
| 2916 | destFormat == GL_ALPHA && |
| 2917 | destType == GL_UNSIGNED_BYTE) |
| 2918 | { |
| 2919 | return 1; |
| 2920 | } |
| 2921 | else if (sourceFormat == DXGI_FORMAT_R8G8B8A8_UNORM && |
| 2922 | destFormat == GL_RGBA && |
| 2923 | destType == GL_UNSIGNED_BYTE) |
| 2924 | { |
| 2925 | return 4; |
| 2926 | } |
| 2927 | else if (sourceFormat == DXGI_FORMAT_B8G8R8A8_UNORM && |
| 2928 | destFormat == GL_BGRA_EXT && |
| 2929 | destType == GL_UNSIGNED_BYTE) |
| 2930 | { |
| 2931 | return 4; |
| 2932 | } |
| 2933 | else if (sourceFormat == DXGI_FORMAT_R16G16B16A16_FLOAT && |
| 2934 | destFormat == GL_RGBA && |
| 2935 | destType == GL_HALF_FLOAT_OES) |
| 2936 | { |
| 2937 | return 8; |
| 2938 | } |
| 2939 | else if (sourceFormat == DXGI_FORMAT_R32G32B32_FLOAT && |
| 2940 | destFormat == GL_RGB && |
| 2941 | destType == GL_FLOAT) |
| 2942 | { |
| 2943 | return 12; |
| 2944 | } |
| 2945 | else if (sourceFormat == DXGI_FORMAT_R32G32B32A32_FLOAT && |
| 2946 | destFormat == GL_RGBA && |
| 2947 | destType == GL_FLOAT) |
| 2948 | { |
| 2949 | return 16; |
| 2950 | } |
| 2951 | else |
| 2952 | { |
| 2953 | return 0; |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x, |
| 2958 | unsigned int y, int inputPitch, gl::Color *outColor) |
| 2959 | { |
| 2960 | switch (format) |
| 2961 | { |
| 2962 | case DXGI_FORMAT_R8G8B8A8_UNORM: |
| 2963 | { |
| 2964 | unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch); |
shannon.woods@transgaming.com | 25950ea | 2013-02-28 23:04:15 +0000 | [diff] [blame] | 2965 | outColor->red = (rgba & 0x000000FF) * (1.0f / 0x000000FF); |
| 2966 | outColor->green = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 2967 | outColor->blue = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 2968 | outColor->alpha = (rgba & 0xFF000000) * (1.0f / 0xFF000000); |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 2969 | } |
| 2970 | break; |
| 2971 | |
| 2972 | case DXGI_FORMAT_A8_UNORM: |
| 2973 | { |
| 2974 | outColor->red = 0.0f; |
| 2975 | outColor->green = 0.0f; |
| 2976 | outColor->blue = 0.0f; |
| 2977 | outColor->alpha = *(data + x + y * inputPitch) / 255.0f; |
| 2978 | } |
| 2979 | break; |
| 2980 | |
| 2981 | case DXGI_FORMAT_R32G32B32A32_FLOAT: |
| 2982 | { |
| 2983 | outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0); |
| 2984 | outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1); |
| 2985 | outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2); |
| 2986 | outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3); |
| 2987 | } |
| 2988 | break; |
| 2989 | |
| 2990 | case DXGI_FORMAT_R32G32B32_FLOAT: |
| 2991 | { |
| 2992 | outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0); |
| 2993 | outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1); |
| 2994 | outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2); |
| 2995 | outColor->alpha = 1.0f; |
| 2996 | } |
| 2997 | break; |
| 2998 | |
| 2999 | case DXGI_FORMAT_R16G16B16A16_FLOAT: |
| 3000 | { |
| 3001 | outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0)); |
| 3002 | outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1)); |
| 3003 | outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2)); |
| 3004 | outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3)); |
| 3005 | } |
| 3006 | break; |
| 3007 | |
| 3008 | case DXGI_FORMAT_B8G8R8A8_UNORM: |
| 3009 | { |
| 3010 | unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch); |
shannon.woods@transgaming.com | 25950ea | 2013-02-28 23:04:15 +0000 | [diff] [blame] | 3011 | outColor->red = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000); |
| 3012 | outColor->blue = (bgra & 0x000000FF) * (1.0f / 0x000000FF); |
| 3013 | outColor->green = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00); |
| 3014 | outColor->alpha = (bgra & 0xFF000000) * (1.0f / 0xFF000000); |
daniel@transgaming.com | ee42a0a | 2013-01-11 04:09:15 +0000 | [diff] [blame] | 3015 | } |
| 3016 | break; |
| 3017 | |
| 3018 | case DXGI_FORMAT_R8_UNORM: |
| 3019 | { |
| 3020 | outColor->red = *(data + x + y * inputPitch) / 255.0f; |
| 3021 | outColor->green = 0.0f; |
| 3022 | outColor->blue = 0.0f; |
| 3023 | outColor->alpha = 1.0f; |
| 3024 | } |
| 3025 | break; |
| 3026 | |
| 3027 | case DXGI_FORMAT_R8G8_UNORM: |
| 3028 | { |
| 3029 | unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch); |
| 3030 | |
| 3031 | outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00); |
| 3032 | outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF); |
| 3033 | outColor->blue = 0.0f; |
| 3034 | outColor->alpha = 1.0f; |
| 3035 | } |
| 3036 | break; |
| 3037 | |
| 3038 | case DXGI_FORMAT_R16_FLOAT: |
| 3039 | { |
| 3040 | outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch)); |
| 3041 | outColor->green = 0.0f; |
| 3042 | outColor->blue = 0.0f; |
| 3043 | outColor->alpha = 1.0f; |
| 3044 | } |
| 3045 | break; |
| 3046 | |
| 3047 | case DXGI_FORMAT_R16G16_FLOAT: |
| 3048 | { |
| 3049 | outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0)); |
| 3050 | outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1)); |
| 3051 | outColor->blue = 0.0f; |
| 3052 | outColor->alpha = 1.0f; |
| 3053 | } |
| 3054 | break; |
| 3055 | |
| 3056 | default: |
| 3057 | ERR("ReadPixelColor not implemented for DXGI format %u.", format); |
| 3058 | UNIMPLEMENTED(); |
| 3059 | break; |
| 3060 | } |
| 3061 | } |
| 3062 | |
| 3063 | static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x, |
| 3064 | unsigned int y, int outputPitch, void *outData) |
| 3065 | { |
| 3066 | unsigned char* byteData = reinterpret_cast<unsigned char*>(outData); |
| 3067 | unsigned short* shortData = reinterpret_cast<unsigned short*>(outData); |
| 3068 | |
| 3069 | switch (format) |
| 3070 | { |
| 3071 | case GL_RGBA: |
| 3072 | switch (type) |
| 3073 | { |
| 3074 | case GL_UNSIGNED_BYTE: |
| 3075 | byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f); |
| 3076 | byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f); |
| 3077 | byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f); |
| 3078 | byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f); |
| 3079 | break; |
| 3080 | |
| 3081 | default: |
| 3082 | ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type); |
| 3083 | UNIMPLEMENTED(); |
| 3084 | break; |
| 3085 | } |
| 3086 | break; |
| 3087 | |
| 3088 | case GL_BGRA_EXT: |
| 3089 | switch (type) |
| 3090 | { |
| 3091 | case GL_UNSIGNED_BYTE: |
| 3092 | byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f); |
| 3093 | byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f); |
| 3094 | byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f); |
| 3095 | byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f); |
| 3096 | break; |
| 3097 | |
| 3098 | case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: |
| 3099 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 3100 | // this type is packed as follows: |
| 3101 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 3102 | // -------------------------------------------------------------------------------- |
| 3103 | // | 4th | 3rd | 2nd | 1st component | |
| 3104 | // -------------------------------------------------------------------------------- |
| 3105 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 3106 | shortData[x + y * outputPitch / sizeof(unsigned short)] = |
| 3107 | (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) | |
| 3108 | (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) | |
| 3109 | (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) | |
| 3110 | (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0); |
| 3111 | break; |
| 3112 | |
| 3113 | case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: |
| 3114 | // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section |
| 3115 | // this type is packed as follows: |
| 3116 | // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
| 3117 | // -------------------------------------------------------------------------------- |
| 3118 | // | 4th | 3rd | 2nd | 1st component | |
| 3119 | // -------------------------------------------------------------------------------- |
| 3120 | // in the case of BGRA_EXT, B is the first component, G the second, and so forth. |
| 3121 | shortData[x + y * outputPitch / sizeof(unsigned short)] = |
| 3122 | (static_cast<unsigned short>( color.alpha + 0.5f) << 15) | |
| 3123 | (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) | |
| 3124 | (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) | |
| 3125 | (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0); |
| 3126 | break; |
| 3127 | |
| 3128 | default: |
| 3129 | ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type); |
| 3130 | UNIMPLEMENTED(); |
| 3131 | break; |
| 3132 | } |
| 3133 | break; |
| 3134 | |
| 3135 | case GL_RGB: |
| 3136 | switch (type) |
| 3137 | { |
| 3138 | case GL_UNSIGNED_SHORT_5_6_5: |
| 3139 | shortData[x + y * outputPitch / sizeof(unsigned short)] = |
| 3140 | (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) | |
| 3141 | (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) | |
| 3142 | (static_cast<unsigned short>(31 * color.red + 0.5f) << 11); |
| 3143 | break; |
| 3144 | |
| 3145 | case GL_UNSIGNED_BYTE: |
| 3146 | byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f); |
| 3147 | byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f); |
| 3148 | byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f); |
| 3149 | break; |
| 3150 | |
| 3151 | default: |
| 3152 | ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type); |
| 3153 | UNIMPLEMENTED(); |
| 3154 | break; |
| 3155 | } |
| 3156 | break; |
| 3157 | |
| 3158 | default: |
| 3159 | ERR("WritePixelColor not implemented for format 0x%X.", format); |
| 3160 | UNIMPLEMENTED(); |
| 3161 | break; |
| 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, |
| 3166 | GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder, |
| 3167 | GLint packAlignment, void *pixels) |
| 3168 | { |
| 3169 | D3D11_TEXTURE2D_DESC textureDesc; |
| 3170 | texture->GetDesc(&textureDesc); |
| 3171 | |
| 3172 | D3D11_TEXTURE2D_DESC stagingDesc; |
| 3173 | stagingDesc.Width = area.width; |
| 3174 | stagingDesc.Height = area.height; |
| 3175 | stagingDesc.MipLevels = 1; |
| 3176 | stagingDesc.ArraySize = 1; |
| 3177 | stagingDesc.Format = textureDesc.Format; |
| 3178 | stagingDesc.SampleDesc.Count = 1; |
| 3179 | stagingDesc.SampleDesc.Quality = 0; |
| 3180 | stagingDesc.Usage = D3D11_USAGE_STAGING; |
| 3181 | stagingDesc.BindFlags = 0; |
| 3182 | stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; |
| 3183 | stagingDesc.MiscFlags = 0; |
| 3184 | |
| 3185 | ID3D11Texture2D* stagingTex = NULL; |
| 3186 | HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex); |
| 3187 | if (FAILED(result)) |
| 3188 | { |
| 3189 | ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result); |
| 3190 | return; |
| 3191 | } |
| 3192 | |
| 3193 | ID3D11Texture2D* srcTex = NULL; |
| 3194 | if (textureDesc.SampleDesc.Count > 1) |
| 3195 | { |
| 3196 | D3D11_TEXTURE2D_DESC resolveDesc; |
| 3197 | resolveDesc.Width = textureDesc.Width; |
| 3198 | resolveDesc.Height = textureDesc.Height; |
| 3199 | resolveDesc.MipLevels = 1; |
| 3200 | resolveDesc.ArraySize = 1; |
| 3201 | resolveDesc.Format = textureDesc.Format; |
| 3202 | resolveDesc.SampleDesc.Count = 1; |
| 3203 | resolveDesc.SampleDesc.Quality = 0; |
| 3204 | resolveDesc.Usage = D3D11_USAGE_DEFAULT; |
| 3205 | resolveDesc.BindFlags = 0; |
| 3206 | resolveDesc.CPUAccessFlags = 0; |
| 3207 | resolveDesc.MiscFlags = 0; |
| 3208 | |
| 3209 | result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex); |
| 3210 | if (FAILED(result)) |
| 3211 | { |
| 3212 | ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result); |
| 3213 | stagingTex->Release(); |
| 3214 | return; |
| 3215 | } |
| 3216 | |
| 3217 | mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format); |
| 3218 | subResource = 0; |
| 3219 | } |
| 3220 | else |
| 3221 | { |
| 3222 | srcTex = texture; |
| 3223 | srcTex->AddRef(); |
| 3224 | } |
| 3225 | |
| 3226 | D3D11_BOX srcBox; |
| 3227 | srcBox.left = area.x; |
| 3228 | srcBox.right = area.x + area.width; |
| 3229 | srcBox.top = area.y; |
| 3230 | srcBox.bottom = area.y + area.height; |
| 3231 | srcBox.front = 0; |
| 3232 | srcBox.back = 1; |
| 3233 | |
| 3234 | mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox); |
| 3235 | |
| 3236 | srcTex->Release(); |
| 3237 | srcTex = NULL; |
| 3238 | |
| 3239 | D3D11_MAPPED_SUBRESOURCE mapping; |
| 3240 | mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping); |
| 3241 | |
| 3242 | unsigned char *source; |
| 3243 | int inputPitch; |
| 3244 | if (packReverseRowOrder) |
| 3245 | { |
| 3246 | source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1); |
| 3247 | inputPitch = -static_cast<int>(mapping.RowPitch); |
| 3248 | } |
| 3249 | else |
| 3250 | { |
| 3251 | source = static_cast<unsigned char*>(mapping.pData); |
| 3252 | inputPitch = static_cast<int>(mapping.RowPitch); |
| 3253 | } |
| 3254 | |
| 3255 | unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type); |
| 3256 | if (fastPixelSize != 0) |
| 3257 | { |
| 3258 | unsigned char *dest = static_cast<unsigned char*>(pixels); |
| 3259 | for (int j = 0; j < area.height; j++) |
| 3260 | { |
| 3261 | memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize); |
| 3262 | } |
| 3263 | } |
| 3264 | else |
| 3265 | { |
| 3266 | gl::Color pixelColor; |
| 3267 | for (int j = 0; j < area.height; j++) |
| 3268 | { |
| 3269 | for (int i = 0; i < area.width; i++) |
| 3270 | { |
| 3271 | readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor); |
| 3272 | writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels); |
| 3273 | } |
| 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | mDeviceContext->Unmap(stagingTex, 0); |
| 3278 | |
| 3279 | stagingTex->Release(); |
| 3280 | stagingTex = NULL; |
| 3281 | } |
| 3282 | |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 3283 | bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, |
| 3284 | const gl::Rectangle &drawRect, BlitTarget target) |
| 3285 | { |
| 3286 | ASSERT(readRect.width == drawRect.width && readRect.height == drawRect.height); |
| 3287 | |
| 3288 | gl::Renderbuffer *readBuffer = NULL; |
| 3289 | switch (target) |
| 3290 | { |
| 3291 | case BLIT_RENDERTARGET: |
| 3292 | readBuffer = readTarget->getColorbuffer(); |
| 3293 | break; |
| 3294 | case BLIT_DEPTHSTENCIL: |
| 3295 | readBuffer = readTarget->getDepthOrStencilbuffer(); |
| 3296 | break; |
| 3297 | default: UNREACHABLE(); |
| 3298 | } |
| 3299 | if (!readBuffer) |
| 3300 | { |
| 3301 | ERR("Failed to retrieve the color buffer from the read target."); |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 3302 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | RenderTarget11 *sourceRenderTarget = NULL; |
| 3306 | switch (target) |
| 3307 | { |
| 3308 | case BLIT_RENDERTARGET: |
| 3309 | sourceRenderTarget = RenderTarget11::makeRenderTarget11(readBuffer->getRenderTarget()); |
| 3310 | break; |
| 3311 | case BLIT_DEPTHSTENCIL: |
| 3312 | sourceRenderTarget = RenderTarget11::makeRenderTarget11(readBuffer->getDepthStencil()); |
| 3313 | break; |
| 3314 | default: UNREACHABLE(); |
| 3315 | } |
| 3316 | if (!sourceRenderTarget) |
| 3317 | { |
| 3318 | ERR("Failed to retrieve the render target from the frame buffer."); |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 3319 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | ID3D11Texture2D *source = NULL; |
| 3323 | unsigned int sourceSubresource = 0; |
| 3324 | if (sourceRenderTarget->getSamples() > 0) |
| 3325 | { |
| 3326 | ID3D11Texture2D *unresolvedTexture = sourceRenderTarget->getTexture(); |
| 3327 | |
| 3328 | source = resolveMultisampledTexture(unresolvedTexture, sourceRenderTarget->getSubresourceIndex()); |
| 3329 | sourceSubresource = 0; |
| 3330 | |
| 3331 | unresolvedTexture->Release(); |
| 3332 | } |
| 3333 | else |
| 3334 | { |
| 3335 | source = sourceRenderTarget->getTexture(); |
| 3336 | sourceSubresource = sourceRenderTarget->getSubresourceIndex(); |
| 3337 | } |
| 3338 | |
| 3339 | if (!source) |
| 3340 | { |
| 3341 | ERR("Failed to retrieve the render target view from the render target."); |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 3342 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | |
| 3346 | gl::Renderbuffer *drawBuffer = NULL; |
| 3347 | switch (target) |
| 3348 | { |
| 3349 | case BLIT_RENDERTARGET: |
| 3350 | drawBuffer = drawTarget->getColorbuffer(); |
| 3351 | break; |
| 3352 | case BLIT_DEPTHSTENCIL: |
| 3353 | drawBuffer = drawTarget->getDepthOrStencilbuffer(); |
| 3354 | break; |
| 3355 | default: UNREACHABLE(); |
| 3356 | } |
| 3357 | if (!drawBuffer) |
| 3358 | { |
| 3359 | source->Release(); |
| 3360 | ERR("Failed to retrieve the color buffer from the draw buffer."); |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 3361 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | RenderTarget11 *drawRenderTarget = NULL; |
| 3365 | switch (target) |
| 3366 | { |
| 3367 | case BLIT_RENDERTARGET: |
| 3368 | drawRenderTarget = RenderTarget11::makeRenderTarget11(drawBuffer->getRenderTarget()); |
| 3369 | break; |
| 3370 | case BLIT_DEPTHSTENCIL: |
| 3371 | drawRenderTarget = RenderTarget11::makeRenderTarget11(drawBuffer->getDepthStencil()); |
| 3372 | break; |
| 3373 | default: UNREACHABLE(); |
| 3374 | } |
| 3375 | if (!drawRenderTarget) |
| 3376 | { |
| 3377 | source->Release(); |
| 3378 | ERR("Failed to retrieve the render target from the render buffer."); |
shannon.woods@transgaming.com | ea4a0c6 | 2013-02-28 23:06:29 +0000 | [diff] [blame] | 3379 | return gl::error(GL_OUT_OF_MEMORY, false); |
shannon.woods@transgaming.com | 1e1deda | 2013-02-28 23:06:23 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | ID3D11Texture2D *dest = drawRenderTarget->getTexture(); |
| 3383 | unsigned int destSubresource = drawRenderTarget->getSubresourceIndex(); |
| 3384 | |
| 3385 | D3D11_BOX srcBox; |
| 3386 | srcBox.left = readRect.x; |
| 3387 | srcBox.right = readRect.x + readRect.width; |
| 3388 | srcBox.top = readRect.y; |
| 3389 | srcBox.bottom = readRect.y + readRect.height; |
| 3390 | srcBox.front = 0; |
| 3391 | srcBox.back = 1; |
| 3392 | |
| 3393 | mDeviceContext->CopySubresourceRegion(dest, destSubresource, drawRect.x, drawRect.y, 0, |
| 3394 | source, sourceSubresource, &srcBox); |
| 3395 | |
| 3396 | source->Release(); |
| 3397 | dest->Release(); |
| 3398 | |
| 3399 | return true; |
| 3400 | } |
| 3401 | |
shannon.woods@transgaming.com | d67f9ce | 2013-02-28 23:06:09 +0000 | [diff] [blame] | 3402 | ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource) |
| 3403 | { |
| 3404 | D3D11_TEXTURE2D_DESC textureDesc; |
| 3405 | source->GetDesc(&textureDesc); |
| 3406 | |
| 3407 | if (textureDesc.SampleDesc.Count > 1) |
| 3408 | { |
| 3409 | D3D11_TEXTURE2D_DESC resolveDesc; |
| 3410 | resolveDesc.Width = textureDesc.Width; |
| 3411 | resolveDesc.Height = textureDesc.Height; |
| 3412 | resolveDesc.MipLevels = 1; |
| 3413 | resolveDesc.ArraySize = 1; |
| 3414 | resolveDesc.Format = textureDesc.Format; |
| 3415 | resolveDesc.SampleDesc.Count = 1; |
| 3416 | resolveDesc.SampleDesc.Quality = 0; |
| 3417 | resolveDesc.Usage = textureDesc.Usage; |
| 3418 | resolveDesc.BindFlags = textureDesc.BindFlags; |
| 3419 | resolveDesc.CPUAccessFlags = 0; |
| 3420 | resolveDesc.MiscFlags = 0; |
| 3421 | |
| 3422 | ID3D11Texture2D *resolveTexture = NULL; |
| 3423 | HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture); |
| 3424 | if (FAILED(result)) |
| 3425 | { |
| 3426 | ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result); |
| 3427 | return NULL; |
| 3428 | } |
| 3429 | |
| 3430 | mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format); |
| 3431 | return resolveTexture; |
| 3432 | } |
| 3433 | else |
| 3434 | { |
| 3435 | source->AddRef(); |
| 3436 | return source; |
| 3437 | } |
| 3438 | } |
| 3439 | |
shannon.woods@transgaming.com | 9d971ff | 2013-01-25 21:50:53 +0000 | [diff] [blame] | 3440 | } |