shannon.woods@transgaming.com | bdf2d80 | 2013-02-28 23:16:20 +0000 | [diff] [blame] | 1 | #include "precompiled.h" |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 2 | // |
Nicolas Capens | aea8e94 | 2014-05-09 19:14:08 -0400 | [diff] [blame] | 3 | // Copyright (c) 2012-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +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 | // RenderStateCache.cpp: Defines rx::RenderStateCache, a cache of Direct3D render |
| 9 | // state objects. |
| 10 | |
Geoff Lang | d47e0fc | 2013-08-29 11:40:43 -0400 | [diff] [blame] | 11 | #include "libGLESv2/renderer/d3d11/RenderStateCache.h" |
| 12 | #include "libGLESv2/renderer/d3d11/renderer11_utils.h" |
Geoff Lang | c142e9d | 2013-09-30 15:19:47 -0400 | [diff] [blame] | 13 | #include "libGLESv2/renderer/Renderer.h" |
| 14 | #include "libGLESv2/Framebuffer.h" |
| 15 | #include "libGLESv2/Renderbuffer.h" |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 16 | |
| 17 | #include "common/debug.h" |
| 18 | #include "third_party/murmurhash/MurmurHash3.h" |
| 19 | |
| 20 | namespace rx |
| 21 | { |
| 22 | |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 23 | template <typename mapType> |
| 24 | static void ClearStateMap(mapType &map) |
| 25 | { |
| 26 | for (mapType::iterator i = map.begin(); i != map.end(); i++) |
| 27 | { |
| 28 | SafeRelease(i->second.first); |
| 29 | } |
| 30 | map.clear(); |
| 31 | } |
| 32 | |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 33 | // MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState, |
| 34 | // ID3D11Device::CreateDepthStencilState and ID3D11Device::CreateSamplerState claims the maximum |
| 35 | // number of unique states of each type an application can create is 4096 |
daniel@transgaming.com | c497eba | 2012-11-28 19:39:06 +0000 | [diff] [blame] | 36 | const unsigned int RenderStateCache::kMaxBlendStates = 4096; |
| 37 | const unsigned int RenderStateCache::kMaxRasterizerStates = 4096; |
| 38 | const unsigned int RenderStateCache::kMaxDepthStencilStates = 4096; |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 39 | const unsigned int RenderStateCache::kMaxSamplerStates = 4096; |
daniel@transgaming.com | c497eba | 2012-11-28 19:39:06 +0000 | [diff] [blame] | 40 | |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 41 | RenderStateCache::RenderStateCache() : mDevice(NULL), mCounter(0), |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 42 | mBlendStateCache(kMaxBlendStates, hashBlendState, compareBlendStates), |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 43 | mRasterizerStateCache(kMaxRasterizerStates, hashRasterizerState, compareRasterizerStates), |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 44 | mDepthStencilStateCache(kMaxDepthStencilStates, hashDepthStencilState, compareDepthStencilStates), |
| 45 | mSamplerStateCache(kMaxSamplerStates, hashSamplerState, compareSamplerStates) |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
| 49 | RenderStateCache::~RenderStateCache() |
| 50 | { |
| 51 | clear(); |
| 52 | } |
| 53 | |
daniel@transgaming.com | 3cf8650 | 2013-01-11 04:08:40 +0000 | [diff] [blame] | 54 | void RenderStateCache::initialize(ID3D11Device *device) |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 55 | { |
| 56 | clear(); |
| 57 | mDevice = device; |
| 58 | } |
| 59 | |
| 60 | void RenderStateCache::clear() |
| 61 | { |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 62 | ClearStateMap(mBlendStateCache); |
| 63 | ClearStateMap(mRasterizerStateCache); |
| 64 | ClearStateMap(mDepthStencilStateCache); |
| 65 | ClearStateMap(mSamplerStateCache); |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Geoff Lang | c142e9d | 2013-09-30 15:19:47 -0400 | [diff] [blame] | 68 | std::size_t RenderStateCache::hashBlendState(const BlendStateKey &blendState) |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 69 | { |
| 70 | static const unsigned int seed = 0xABCDEF98; |
| 71 | |
| 72 | std::size_t hash = 0; |
| 73 | MurmurHash3_x86_32(&blendState, sizeof(gl::BlendState), seed, &hash); |
| 74 | return hash; |
| 75 | } |
| 76 | |
Geoff Lang | c142e9d | 2013-09-30 15:19:47 -0400 | [diff] [blame] | 77 | bool RenderStateCache::compareBlendStates(const BlendStateKey &a, const BlendStateKey &b) |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 78 | { |
Geoff Lang | 03ef740 | 2014-02-11 09:40:20 -0500 | [diff] [blame] | 79 | return memcmp(&a, &b, sizeof(BlendStateKey)) == 0; |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Geoff Lang | c142e9d | 2013-09-30 15:19:47 -0400 | [diff] [blame] | 82 | ID3D11BlendState *RenderStateCache::getBlendState(const gl::Framebuffer *framebuffer, const gl::BlendState &blendState) |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 83 | { |
| 84 | if (!mDevice) |
| 85 | { |
| 86 | ERR("RenderStateCache is not initialized."); |
| 87 | return NULL; |
| 88 | } |
| 89 | |
Geoff Lang | c142e9d | 2013-09-30 15:19:47 -0400 | [diff] [blame] | 90 | bool mrt = false; |
| 91 | |
| 92 | BlendStateKey key = { 0 }; |
| 93 | key.blendState = blendState; |
| 94 | for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++) |
| 95 | { |
Jamie Madill | 3c7fa22 | 2014-06-05 13:08:51 -0400 | [diff] [blame] | 96 | gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(i); |
| 97 | if (attachment) |
Geoff Lang | c142e9d | 2013-09-30 15:19:47 -0400 | [diff] [blame] | 98 | { |
| 99 | if (i > 0) |
| 100 | { |
| 101 | mrt = true; |
| 102 | } |
| 103 | |
Jamie Madill | 3c7fa22 | 2014-06-05 13:08:51 -0400 | [diff] [blame] | 104 | key.rtChannels[i][0] = attachment->getRedSize() > 0; |
| 105 | key.rtChannels[i][1] = attachment->getGreenSize() > 0; |
| 106 | key.rtChannels[i][2] = attachment->getBlueSize() > 0; |
| 107 | key.rtChannels[i][3] = attachment->getAlphaSize() > 0; |
Geoff Lang | c142e9d | 2013-09-30 15:19:47 -0400 | [diff] [blame] | 108 | } |
| 109 | else |
| 110 | { |
| 111 | key.rtChannels[i][0] = false; |
| 112 | key.rtChannels[i][1] = false; |
| 113 | key.rtChannels[i][2] = false; |
| 114 | key.rtChannels[i][3] = false; |
| 115 | } |
| 116 | } |
| 117 | |
Geoff Lang | 5d64caf | 2014-02-11 10:23:34 -0500 | [diff] [blame] | 118 | BlendStateMap::iterator keyIter = mBlendStateCache.find(key); |
| 119 | if (keyIter != mBlendStateCache.end()) |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 120 | { |
Geoff Lang | 5d64caf | 2014-02-11 10:23:34 -0500 | [diff] [blame] | 121 | BlendStateCounterPair &state = keyIter->second; |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 122 | state.second = mCounter++; |
| 123 | return state.first; |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | if (mBlendStateCache.size() >= kMaxBlendStates) |
| 128 | { |
| 129 | TRACE("Overflowed the limit of %u blend states, removing the least recently used " |
| 130 | "to make room.", kMaxBlendStates); |
| 131 | |
| 132 | BlendStateMap::iterator leastRecentlyUsed = mBlendStateCache.begin(); |
| 133 | for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++) |
| 134 | { |
| 135 | if (i->second.second < leastRecentlyUsed->second.second) |
| 136 | { |
| 137 | leastRecentlyUsed = i; |
| 138 | } |
| 139 | } |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 140 | SafeRelease(leastRecentlyUsed->second.first); |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 141 | mBlendStateCache.erase(leastRecentlyUsed); |
| 142 | } |
| 143 | |
| 144 | // Create a new blend state and insert it into the cache |
| 145 | D3D11_BLEND_DESC blendDesc = { 0 }; |
| 146 | blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage; |
Geoff Lang | c142e9d | 2013-09-30 15:19:47 -0400 | [diff] [blame] | 147 | blendDesc.IndependentBlendEnable = mrt ? TRUE : FALSE; |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 148 | |
| 149 | for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++) |
| 150 | { |
| 151 | D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = blendDesc.RenderTarget[i]; |
| 152 | |
| 153 | rtBlend.BlendEnable = blendState.blend; |
| 154 | if (blendState.blend) |
| 155 | { |
daniel@transgaming.com | 90c634a | 2013-01-11 04:10:01 +0000 | [diff] [blame] | 156 | rtBlend.SrcBlend = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendRGB, false); |
| 157 | rtBlend.DestBlend = gl_d3d11::ConvertBlendFunc(blendState.destBlendRGB, false); |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 158 | rtBlend.BlendOp = gl_d3d11::ConvertBlendOp(blendState.blendEquationRGB); |
| 159 | |
daniel@transgaming.com | 90c634a | 2013-01-11 04:10:01 +0000 | [diff] [blame] | 160 | rtBlend.SrcBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendAlpha, true); |
| 161 | rtBlend.DestBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.destBlendAlpha, true); |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 162 | rtBlend.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha); |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 163 | } |
daniel@transgaming.com | 97b5930 | 2012-11-28 21:05:34 +0000 | [diff] [blame] | 164 | |
Geoff Lang | c142e9d | 2013-09-30 15:19:47 -0400 | [diff] [blame] | 165 | rtBlend.RenderTargetWriteMask = gl_d3d11::ConvertColorMask(key.rtChannels[i][0] && blendState.colorMaskRed, |
| 166 | key.rtChannels[i][1] && blendState.colorMaskGreen, |
| 167 | key.rtChannels[i][2] && blendState.colorMaskBlue, |
| 168 | key.rtChannels[i][3] && blendState.colorMaskAlpha); |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 169 | } |
| 170 | |
daniel@transgaming.com | 3cf8650 | 2013-01-11 04:08:40 +0000 | [diff] [blame] | 171 | ID3D11BlendState *dx11BlendState = NULL; |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 172 | HRESULT result = mDevice->CreateBlendState(&blendDesc, &dx11BlendState); |
| 173 | if (FAILED(result) || !dx11BlendState) |
| 174 | { |
| 175 | ERR("Unable to create a ID3D11BlendState, HRESULT: 0x%X.", result); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
Geoff Lang | c142e9d | 2013-09-30 15:19:47 -0400 | [diff] [blame] | 179 | mBlendStateCache.insert(std::make_pair(key, std::make_pair(dx11BlendState, mCounter++))); |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 180 | |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 181 | return dx11BlendState; |
| 182 | } |
| 183 | } |
| 184 | |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 185 | std::size_t RenderStateCache::hashRasterizerState(const RasterizerStateKey &rasterState) |
| 186 | { |
| 187 | static const unsigned int seed = 0xABCDEF98; |
| 188 | |
| 189 | std::size_t hash = 0; |
| 190 | MurmurHash3_x86_32(&rasterState, sizeof(RasterizerStateKey), seed, &hash); |
| 191 | return hash; |
| 192 | } |
| 193 | |
| 194 | bool RenderStateCache::compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b) |
| 195 | { |
| 196 | return memcmp(&a, &b, sizeof(RasterizerStateKey)) == 0; |
| 197 | } |
| 198 | |
Nicolas Capens | aea8e94 | 2014-05-09 19:14:08 -0400 | [diff] [blame] | 199 | ID3D11RasterizerState *RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState, bool scissorEnabled) |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 200 | { |
| 201 | if (!mDevice) |
| 202 | { |
| 203 | ERR("RenderStateCache is not initialized."); |
| 204 | return NULL; |
| 205 | } |
| 206 | |
Geoff Lang | c142e9d | 2013-09-30 15:19:47 -0400 | [diff] [blame] | 207 | RasterizerStateKey key = { 0 }; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 208 | key.rasterizerState = rasterState; |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 209 | key.scissorEnabled = scissorEnabled; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 210 | |
Geoff Lang | 5d64caf | 2014-02-11 10:23:34 -0500 | [diff] [blame] | 211 | RasterizerStateMap::iterator keyIter = mRasterizerStateCache.find(key); |
| 212 | if (keyIter != mRasterizerStateCache.end()) |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 213 | { |
Geoff Lang | 5d64caf | 2014-02-11 10:23:34 -0500 | [diff] [blame] | 214 | RasterizerStateCounterPair &state = keyIter->second; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 215 | state.second = mCounter++; |
| 216 | return state.first; |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | if (mRasterizerStateCache.size() >= kMaxRasterizerStates) |
| 221 | { |
| 222 | TRACE("Overflowed the limit of %u rasterizer states, removing the least recently used " |
| 223 | "to make room.", kMaxRasterizerStates); |
| 224 | |
| 225 | RasterizerStateMap::iterator leastRecentlyUsed = mRasterizerStateCache.begin(); |
| 226 | for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++) |
| 227 | { |
| 228 | if (i->second.second < leastRecentlyUsed->second.second) |
| 229 | { |
| 230 | leastRecentlyUsed = i; |
| 231 | } |
| 232 | } |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 233 | SafeRelease(leastRecentlyUsed->second.first); |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 234 | mRasterizerStateCache.erase(leastRecentlyUsed); |
| 235 | } |
| 236 | |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 237 | D3D11_CULL_MODE cullMode = gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode); |
| 238 | |
| 239 | // Disable culling if drawing points |
| 240 | if (rasterState.pointDrawMode) |
| 241 | { |
| 242 | cullMode = D3D11_CULL_NONE; |
| 243 | } |
| 244 | |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 245 | D3D11_RASTERIZER_DESC rasterDesc; |
| 246 | rasterDesc.FillMode = D3D11_FILL_SOLID; |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 247 | rasterDesc.CullMode = cullMode; |
daniel@transgaming.com | 4d4fade | 2013-01-11 04:09:10 +0000 | [diff] [blame] | 248 | rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? FALSE: TRUE; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 249 | rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of zero will preform no clamping, must be tested though. |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 250 | rasterDesc.DepthClipEnable = TRUE; |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 251 | rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE; |
Nicolas Capens | fd39655 | 2013-06-18 21:41:30 -0400 | [diff] [blame] | 252 | rasterDesc.MultisampleEnable = rasterState.multiSample; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 253 | rasterDesc.AntialiasedLineEnable = FALSE; |
| 254 | |
Nicolas Capens | 5075e13 | 2014-05-09 14:40:29 -0400 | [diff] [blame] | 255 | if (rasterState.polygonOffsetFill) |
| 256 | { |
| 257 | rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetFactor; |
Nicolas Capens | aea8e94 | 2014-05-09 19:14:08 -0400 | [diff] [blame] | 258 | rasterDesc.DepthBias = (INT)rasterState.polygonOffsetUnits; |
Nicolas Capens | 5075e13 | 2014-05-09 14:40:29 -0400 | [diff] [blame] | 259 | } |
| 260 | else |
| 261 | { |
| 262 | rasterDesc.SlopeScaledDepthBias = 0.0f; |
Nicolas Capens | aea8e94 | 2014-05-09 19:14:08 -0400 | [diff] [blame] | 263 | rasterDesc.DepthBias = 0; |
Nicolas Capens | 5075e13 | 2014-05-09 14:40:29 -0400 | [diff] [blame] | 264 | } |
| 265 | |
daniel@transgaming.com | 3cf8650 | 2013-01-11 04:08:40 +0000 | [diff] [blame] | 266 | ID3D11RasterizerState *dx11RasterizerState = NULL; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 267 | HRESULT result = mDevice->CreateRasterizerState(&rasterDesc, &dx11RasterizerState); |
| 268 | if (FAILED(result) || !dx11RasterizerState) |
| 269 | { |
| 270 | ERR("Unable to create a ID3D11RasterizerState, HRESULT: 0x%X.", result); |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | mRasterizerStateCache.insert(std::make_pair(key, std::make_pair(dx11RasterizerState, mCounter++))); |
| 275 | |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 276 | return dx11RasterizerState; |
| 277 | } |
| 278 | } |
| 279 | |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 280 | std::size_t RenderStateCache::hashDepthStencilState(const gl::DepthStencilState &dsState) |
| 281 | { |
| 282 | static const unsigned int seed = 0xABCDEF98; |
| 283 | |
| 284 | std::size_t hash = 0; |
| 285 | MurmurHash3_x86_32(&dsState, sizeof(gl::DepthStencilState), seed, &hash); |
| 286 | return hash; |
| 287 | } |
| 288 | |
| 289 | bool RenderStateCache::compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b) |
| 290 | { |
| 291 | return memcmp(&a, &b, sizeof(gl::DepthStencilState)) == 0; |
| 292 | } |
| 293 | |
daniel@transgaming.com | 3cf8650 | 2013-01-11 04:08:40 +0000 | [diff] [blame] | 294 | ID3D11DepthStencilState *RenderStateCache::getDepthStencilState(const gl::DepthStencilState &dsState) |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 295 | { |
| 296 | if (!mDevice) |
| 297 | { |
| 298 | ERR("RenderStateCache is not initialized."); |
| 299 | return NULL; |
| 300 | } |
| 301 | |
Geoff Lang | 5d64caf | 2014-02-11 10:23:34 -0500 | [diff] [blame] | 302 | DepthStencilStateMap::iterator keyIter = mDepthStencilStateCache.find(dsState); |
| 303 | if (keyIter != mDepthStencilStateCache.end()) |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 304 | { |
Geoff Lang | 5d64caf | 2014-02-11 10:23:34 -0500 | [diff] [blame] | 305 | DepthStencilStateCounterPair &state = keyIter->second; |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 306 | state.second = mCounter++; |
| 307 | return state.first; |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | if (mDepthStencilStateCache.size() >= kMaxDepthStencilStates) |
| 312 | { |
| 313 | TRACE("Overflowed the limit of %u depth stencil states, removing the least recently used " |
| 314 | "to make room.", kMaxDepthStencilStates); |
| 315 | |
| 316 | DepthStencilStateMap::iterator leastRecentlyUsed = mDepthStencilStateCache.begin(); |
| 317 | for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++) |
| 318 | { |
| 319 | if (i->second.second < leastRecentlyUsed->second.second) |
| 320 | { |
| 321 | leastRecentlyUsed = i; |
| 322 | } |
| 323 | } |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 324 | SafeRelease(leastRecentlyUsed->second.first); |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 325 | mDepthStencilStateCache.erase(leastRecentlyUsed); |
| 326 | } |
| 327 | |
| 328 | D3D11_DEPTH_STENCIL_DESC dsDesc = { 0 }; |
| 329 | dsDesc.DepthEnable = dsState.depthTest ? TRUE : FALSE; |
| 330 | dsDesc.DepthWriteMask = gl_d3d11::ConvertDepthMask(dsState.depthMask); |
| 331 | dsDesc.DepthFunc = gl_d3d11::ConvertComparison(dsState.depthFunc); |
| 332 | dsDesc.StencilEnable = dsState.stencilTest ? TRUE : FALSE; |
| 333 | dsDesc.StencilReadMask = gl_d3d11::ConvertStencilMask(dsState.stencilMask); |
| 334 | dsDesc.StencilWriteMask = gl_d3d11::ConvertStencilMask(dsState.stencilWritemask); |
| 335 | dsDesc.FrontFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilFail); |
| 336 | dsDesc.FrontFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthFail); |
| 337 | dsDesc.FrontFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthPass); |
| 338 | dsDesc.FrontFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilFunc); |
| 339 | dsDesc.BackFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackFail); |
| 340 | dsDesc.BackFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthFail); |
| 341 | dsDesc.BackFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthPass); |
| 342 | dsDesc.BackFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilBackFunc); |
| 343 | |
daniel@transgaming.com | 3cf8650 | 2013-01-11 04:08:40 +0000 | [diff] [blame] | 344 | ID3D11DepthStencilState *dx11DepthStencilState = NULL; |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 345 | HRESULT result = mDevice->CreateDepthStencilState(&dsDesc, &dx11DepthStencilState); |
| 346 | if (FAILED(result) || !dx11DepthStencilState) |
| 347 | { |
| 348 | ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result); |
| 349 | return NULL; |
| 350 | } |
| 351 | |
| 352 | mDepthStencilStateCache.insert(std::make_pair(dsState, std::make_pair(dx11DepthStencilState, mCounter++))); |
| 353 | |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 354 | return dx11DepthStencilState; |
| 355 | } |
| 356 | } |
| 357 | |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 358 | std::size_t RenderStateCache::hashSamplerState(const gl::SamplerState &samplerState) |
| 359 | { |
| 360 | static const unsigned int seed = 0xABCDEF98; |
| 361 | |
| 362 | std::size_t hash = 0; |
| 363 | MurmurHash3_x86_32(&samplerState, sizeof(gl::SamplerState), seed, &hash); |
| 364 | return hash; |
| 365 | } |
| 366 | |
| 367 | bool RenderStateCache::compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b) |
| 368 | { |
daniel@transgaming.com | cc47bc0 | 2013-01-11 04:09:33 +0000 | [diff] [blame] | 369 | return memcmp(&a, &b, sizeof(gl::SamplerState)) == 0; |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 370 | } |
| 371 | |
daniel@transgaming.com | 3cf8650 | 2013-01-11 04:08:40 +0000 | [diff] [blame] | 372 | ID3D11SamplerState *RenderStateCache::getSamplerState(const gl::SamplerState &samplerState) |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 373 | { |
| 374 | if (!mDevice) |
| 375 | { |
| 376 | ERR("RenderStateCache is not initialized."); |
| 377 | return NULL; |
| 378 | } |
| 379 | |
Geoff Lang | 5d64caf | 2014-02-11 10:23:34 -0500 | [diff] [blame] | 380 | SamplerStateMap::iterator keyIter = mSamplerStateCache.find(samplerState); |
| 381 | if (keyIter != mSamplerStateCache.end()) |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 382 | { |
Geoff Lang | 5d64caf | 2014-02-11 10:23:34 -0500 | [diff] [blame] | 383 | SamplerStateCounterPair &state = keyIter->second; |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 384 | state.second = mCounter++; |
| 385 | return state.first; |
| 386 | } |
| 387 | else |
| 388 | { |
| 389 | if (mSamplerStateCache.size() >= kMaxSamplerStates) |
| 390 | { |
| 391 | TRACE("Overflowed the limit of %u sampler states, removing the least recently used " |
| 392 | "to make room.", kMaxSamplerStates); |
| 393 | |
| 394 | SamplerStateMap::iterator leastRecentlyUsed = mSamplerStateCache.begin(); |
| 395 | for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++) |
| 396 | { |
| 397 | if (i->second.second < leastRecentlyUsed->second.second) |
| 398 | { |
| 399 | leastRecentlyUsed = i; |
| 400 | } |
| 401 | } |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 402 | SafeRelease(leastRecentlyUsed->second.first); |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 403 | mSamplerStateCache.erase(leastRecentlyUsed); |
| 404 | } |
| 405 | |
| 406 | D3D11_SAMPLER_DESC samplerDesc; |
Geoff Lang | c82fc41 | 2013-07-10 14:43:42 -0400 | [diff] [blame] | 407 | samplerDesc.Filter = gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter, |
| 408 | samplerState.maxAnisotropy, samplerState.compareMode); |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 409 | samplerDesc.AddressU = gl_d3d11::ConvertTextureWrap(samplerState.wrapS); |
| 410 | samplerDesc.AddressV = gl_d3d11::ConvertTextureWrap(samplerState.wrapT); |
shannon.woods%transgaming.com@gtempaccount.com | 0b3a8df | 2013-04-13 03:44:51 +0000 | [diff] [blame] | 411 | samplerDesc.AddressW = gl_d3d11::ConvertTextureWrap(samplerState.wrapR); |
Nicolas Capens | e47e736 | 2014-03-27 13:34:56 -0400 | [diff] [blame] | 412 | samplerDesc.MipLODBias = 0; |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 413 | samplerDesc.MaxAnisotropy = samplerState.maxAnisotropy; |
Geoff Lang | c82fc41 | 2013-07-10 14:43:42 -0400 | [diff] [blame] | 414 | samplerDesc.ComparisonFunc = gl_d3d11::ConvertComparison(samplerState.compareFunc); |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 415 | samplerDesc.BorderColor[0] = 0.0f; |
| 416 | samplerDesc.BorderColor[1] = 0.0f; |
| 417 | samplerDesc.BorderColor[2] = 0.0f; |
| 418 | samplerDesc.BorderColor[3] = 0.0f; |
Nicolas Capens | 35adc09 | 2014-04-11 17:49:31 -0400 | [diff] [blame] | 419 | samplerDesc.MinLOD = samplerState.minLod; |
| 420 | samplerDesc.MaxLOD = samplerState.maxLod; |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 421 | |
| 422 | ID3D11SamplerState *dx11SamplerState = NULL; |
| 423 | HRESULT result = mDevice->CreateSamplerState(&samplerDesc, &dx11SamplerState); |
| 424 | if (FAILED(result) || !dx11SamplerState) |
| 425 | { |
| 426 | ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result); |
| 427 | return NULL; |
| 428 | } |
| 429 | |
| 430 | mSamplerStateCache.insert(std::make_pair(samplerState, std::make_pair(dx11SamplerState, mCounter++))); |
| 431 | |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 432 | return dx11SamplerState; |
| 433 | } |
| 434 | } |
| 435 | |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 436 | } |