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