daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // RenderStateCache.cpp: Defines rx::RenderStateCache, a cache of Direct3D render |
| 8 | // state objects. |
| 9 | |
| 10 | #include "libGLESv2/renderer/RenderStateCache.h" |
| 11 | #include "libGLESv2/renderer/renderer11_utils.h" |
| 12 | |
| 13 | #include "common/debug.h" |
| 14 | #include "third_party/murmurhash/MurmurHash3.h" |
| 15 | |
| 16 | namespace rx |
| 17 | { |
| 18 | |
daniel@transgaming.com | c497eba | 2012-11-28 19:39:06 +0000 | [diff] [blame] | 19 | // MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState |
| 20 | // and ID3D11Device::CreateDepthStencilState claims the maximum number of unique states of each |
| 21 | // type an application can create is 4096 |
| 22 | const unsigned int RenderStateCache::kMaxBlendStates = 4096; |
| 23 | const unsigned int RenderStateCache::kMaxRasterizerStates = 4096; |
| 24 | const unsigned int RenderStateCache::kMaxDepthStencilStates = 4096; |
| 25 | |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 26 | RenderStateCache::RenderStateCache() : mDevice(NULL), mCounter(0), |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 27 | mBlendStateCache(kMaxBlendStates, hashBlendState, compareBlendStates), |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 28 | mRasterizerStateCache(kMaxRasterizerStates, hashRasterizerState, compareRasterizerStates), |
| 29 | mDepthStencilStateCache(kMaxDepthStencilStates, hashDepthStencilState, compareDepthStencilStates) |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 30 | { |
| 31 | } |
| 32 | |
| 33 | RenderStateCache::~RenderStateCache() |
| 34 | { |
| 35 | clear(); |
| 36 | } |
| 37 | |
| 38 | void RenderStateCache::initialize(ID3D11Device* device) |
| 39 | { |
| 40 | clear(); |
| 41 | mDevice = device; |
| 42 | } |
| 43 | |
| 44 | void RenderStateCache::clear() |
| 45 | { |
| 46 | for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++) |
| 47 | { |
| 48 | i->second.first->Release(); |
| 49 | } |
| 50 | mBlendStateCache.clear(); |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 51 | |
| 52 | for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++) |
| 53 | { |
| 54 | i->second.first->Release(); |
| 55 | } |
| 56 | mRasterizerStateCache.clear(); |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 57 | |
| 58 | for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++) |
| 59 | { |
| 60 | i->second.first->Release(); |
| 61 | } |
| 62 | mDepthStencilStateCache.clear(); |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | std::size_t RenderStateCache::hashBlendState(const gl::BlendState &blendState) |
| 66 | { |
| 67 | static const unsigned int seed = 0xABCDEF98; |
| 68 | |
| 69 | std::size_t hash = 0; |
| 70 | MurmurHash3_x86_32(&blendState, sizeof(gl::BlendState), seed, &hash); |
| 71 | return hash; |
| 72 | } |
| 73 | |
| 74 | bool RenderStateCache::compareBlendStates(const gl::BlendState &a, const gl::BlendState &b) |
| 75 | { |
| 76 | return memcmp(&a, &b, sizeof(gl::BlendState)) == 0; |
| 77 | } |
| 78 | |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 79 | ID3D11BlendState *RenderStateCache::getBlendState(const gl::BlendState &blendState) |
| 80 | { |
| 81 | if (!mDevice) |
| 82 | { |
| 83 | ERR("RenderStateCache is not initialized."); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | BlendStateMap::iterator i = mBlendStateCache.find(blendState); |
| 88 | if (i != mBlendStateCache.end()) |
| 89 | { |
| 90 | BlendStateCounterPair &state = i->second; |
| 91 | state.first->AddRef(); |
| 92 | state.second = mCounter++; |
| 93 | return state.first; |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | if (mBlendStateCache.size() >= kMaxBlendStates) |
| 98 | { |
| 99 | TRACE("Overflowed the limit of %u blend states, removing the least recently used " |
| 100 | "to make room.", kMaxBlendStates); |
| 101 | |
| 102 | BlendStateMap::iterator leastRecentlyUsed = mBlendStateCache.begin(); |
| 103 | for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++) |
| 104 | { |
| 105 | if (i->second.second < leastRecentlyUsed->second.second) |
| 106 | { |
| 107 | leastRecentlyUsed = i; |
| 108 | } |
| 109 | } |
| 110 | leastRecentlyUsed->second.first->Release(); |
| 111 | mBlendStateCache.erase(leastRecentlyUsed); |
| 112 | } |
| 113 | |
| 114 | // Create a new blend state and insert it into the cache |
| 115 | D3D11_BLEND_DESC blendDesc = { 0 }; |
| 116 | blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage; |
| 117 | blendDesc.IndependentBlendEnable = FALSE; |
| 118 | |
| 119 | for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++) |
| 120 | { |
| 121 | D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = blendDesc.RenderTarget[i]; |
| 122 | |
| 123 | rtBlend.BlendEnable = blendState.blend; |
| 124 | if (blendState.blend) |
| 125 | { |
| 126 | rtBlend.SrcBlend = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendRGB); |
| 127 | rtBlend.DestBlend = gl_d3d11::ConvertBlendFunc(blendState.destBlendRGB); |
| 128 | rtBlend.BlendOp = gl_d3d11::ConvertBlendOp(blendState.blendEquationRGB); |
| 129 | |
| 130 | rtBlend.SrcBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendAlpha); |
| 131 | rtBlend.DestBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.destBlendAlpha); |
| 132 | rtBlend.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha); |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 133 | } |
daniel@transgaming.com | 97b5930 | 2012-11-28 21:05:34 +0000 | [diff] [blame] | 134 | |
| 135 | rtBlend.RenderTargetWriteMask = gl_d3d11::ConvertColorMask(blendState.colorMaskRed, |
| 136 | blendState.colorMaskGreen, |
| 137 | blendState.colorMaskBlue, |
| 138 | blendState.colorMaskAlpha); |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | ID3D11BlendState* dx11BlendState = NULL; |
| 142 | HRESULT result = mDevice->CreateBlendState(&blendDesc, &dx11BlendState); |
| 143 | if (FAILED(result) || !dx11BlendState) |
| 144 | { |
| 145 | ERR("Unable to create a ID3D11BlendState, HRESULT: 0x%X.", result); |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | mBlendStateCache.insert(std::make_pair(blendState, std::make_pair(dx11BlendState, mCounter++))); |
| 150 | |
| 151 | dx11BlendState->AddRef(); |
| 152 | return dx11BlendState; |
| 153 | } |
| 154 | } |
| 155 | |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 156 | std::size_t RenderStateCache::hashRasterizerState(const RasterizerStateKey &rasterState) |
| 157 | { |
| 158 | static const unsigned int seed = 0xABCDEF98; |
| 159 | |
| 160 | std::size_t hash = 0; |
| 161 | MurmurHash3_x86_32(&rasterState, sizeof(RasterizerStateKey), seed, &hash); |
| 162 | return hash; |
| 163 | } |
| 164 | |
| 165 | bool RenderStateCache::compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b) |
| 166 | { |
| 167 | return memcmp(&a, &b, sizeof(RasterizerStateKey)) == 0; |
| 168 | } |
| 169 | |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 170 | ID3D11RasterizerState *RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState, |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame^] | 171 | bool scissorEnabled, unsigned int depthSize) |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 172 | { |
| 173 | if (!mDevice) |
| 174 | { |
| 175 | ERR("RenderStateCache is not initialized."); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | RasterizerStateKey key; |
| 180 | key.rasterizerState = rasterState; |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame^] | 181 | key.scissorEnabled = scissorEnabled; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 182 | key.depthSize = depthSize; |
| 183 | |
| 184 | RasterizerStateMap::iterator i = mRasterizerStateCache.find(key); |
| 185 | if (i != mRasterizerStateCache.end()) |
| 186 | { |
| 187 | RasterizerStateCounterPair &state = i->second; |
| 188 | state.first->AddRef(); |
| 189 | state.second = mCounter++; |
| 190 | return state.first; |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | if (mRasterizerStateCache.size() >= kMaxRasterizerStates) |
| 195 | { |
| 196 | TRACE("Overflowed the limit of %u rasterizer states, removing the least recently used " |
| 197 | "to make room.", kMaxRasterizerStates); |
| 198 | |
| 199 | RasterizerStateMap::iterator leastRecentlyUsed = mRasterizerStateCache.begin(); |
| 200 | for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++) |
| 201 | { |
| 202 | if (i->second.second < leastRecentlyUsed->second.second) |
| 203 | { |
| 204 | leastRecentlyUsed = i; |
| 205 | } |
| 206 | } |
| 207 | leastRecentlyUsed->second.first->Release(); |
| 208 | mRasterizerStateCache.erase(leastRecentlyUsed); |
| 209 | } |
| 210 | |
| 211 | D3D11_RASTERIZER_DESC rasterDesc; |
| 212 | rasterDesc.FillMode = D3D11_FILL_SOLID; |
| 213 | rasterDesc.CullMode = gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode); |
| 214 | rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? TRUE : FALSE; |
| 215 | rasterDesc.DepthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize)); |
| 216 | rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of zero will preform no clamping, must be tested though. |
| 217 | rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetUnits; |
| 218 | rasterDesc.DepthClipEnable = TRUE; |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame^] | 219 | rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 220 | rasterDesc.MultisampleEnable = TRUE; |
| 221 | rasterDesc.AntialiasedLineEnable = FALSE; |
| 222 | |
| 223 | ID3D11RasterizerState* dx11RasterizerState = NULL; |
| 224 | HRESULT result = mDevice->CreateRasterizerState(&rasterDesc, &dx11RasterizerState); |
| 225 | if (FAILED(result) || !dx11RasterizerState) |
| 226 | { |
| 227 | ERR("Unable to create a ID3D11RasterizerState, HRESULT: 0x%X.", result); |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | mRasterizerStateCache.insert(std::make_pair(key, std::make_pair(dx11RasterizerState, mCounter++))); |
| 232 | |
| 233 | dx11RasterizerState->AddRef(); |
| 234 | return dx11RasterizerState; |
| 235 | } |
| 236 | } |
| 237 | |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 238 | std::size_t RenderStateCache::hashDepthStencilState(const gl::DepthStencilState &dsState) |
| 239 | { |
| 240 | static const unsigned int seed = 0xABCDEF98; |
| 241 | |
| 242 | std::size_t hash = 0; |
| 243 | MurmurHash3_x86_32(&dsState, sizeof(gl::DepthStencilState), seed, &hash); |
| 244 | return hash; |
| 245 | } |
| 246 | |
| 247 | bool RenderStateCache::compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b) |
| 248 | { |
| 249 | return memcmp(&a, &b, sizeof(gl::DepthStencilState)) == 0; |
| 250 | } |
| 251 | |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 252 | ID3D11DepthStencilState* RenderStateCache::getDepthStencilState(const gl::DepthStencilState &dsState) |
| 253 | { |
| 254 | if (!mDevice) |
| 255 | { |
| 256 | ERR("RenderStateCache is not initialized."); |
| 257 | return NULL; |
| 258 | } |
| 259 | |
| 260 | DepthStencilStateMap::iterator i = mDepthStencilStateCache.find(dsState); |
| 261 | if (i != mDepthStencilStateCache.end()) |
| 262 | { |
| 263 | DepthStencilStateCounterPair &state = i->second; |
| 264 | state.first->AddRef(); |
| 265 | state.second = mCounter++; |
| 266 | return state.first; |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | if (mDepthStencilStateCache.size() >= kMaxDepthStencilStates) |
| 271 | { |
| 272 | TRACE("Overflowed the limit of %u depth stencil states, removing the least recently used " |
| 273 | "to make room.", kMaxDepthStencilStates); |
| 274 | |
| 275 | DepthStencilStateMap::iterator leastRecentlyUsed = mDepthStencilStateCache.begin(); |
| 276 | for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++) |
| 277 | { |
| 278 | if (i->second.second < leastRecentlyUsed->second.second) |
| 279 | { |
| 280 | leastRecentlyUsed = i; |
| 281 | } |
| 282 | } |
| 283 | leastRecentlyUsed->second.first->Release(); |
| 284 | mDepthStencilStateCache.erase(leastRecentlyUsed); |
| 285 | } |
| 286 | |
| 287 | D3D11_DEPTH_STENCIL_DESC dsDesc = { 0 }; |
| 288 | dsDesc.DepthEnable = dsState.depthTest ? TRUE : FALSE; |
| 289 | dsDesc.DepthWriteMask = gl_d3d11::ConvertDepthMask(dsState.depthMask); |
| 290 | dsDesc.DepthFunc = gl_d3d11::ConvertComparison(dsState.depthFunc); |
| 291 | dsDesc.StencilEnable = dsState.stencilTest ? TRUE : FALSE; |
| 292 | dsDesc.StencilReadMask = gl_d3d11::ConvertStencilMask(dsState.stencilMask); |
| 293 | dsDesc.StencilWriteMask = gl_d3d11::ConvertStencilMask(dsState.stencilWritemask); |
| 294 | dsDesc.FrontFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilFail); |
| 295 | dsDesc.FrontFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthFail); |
| 296 | dsDesc.FrontFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthPass); |
| 297 | dsDesc.FrontFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilFunc); |
| 298 | dsDesc.BackFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackFail); |
| 299 | dsDesc.BackFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthFail); |
| 300 | dsDesc.BackFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthPass); |
| 301 | dsDesc.BackFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilBackFunc); |
| 302 | |
| 303 | ID3D11DepthStencilState* dx11DepthStencilState = NULL; |
| 304 | HRESULT result = mDevice->CreateDepthStencilState(&dsDesc, &dx11DepthStencilState); |
| 305 | if (FAILED(result) || !dx11DepthStencilState) |
| 306 | { |
| 307 | ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result); |
| 308 | return NULL; |
| 309 | } |
| 310 | |
| 311 | mDepthStencilStateCache.insert(std::make_pair(dsState, std::make_pair(dx11DepthStencilState, mCounter++))); |
| 312 | |
| 313 | dx11DepthStencilState->AddRef(); |
| 314 | return dx11DepthStencilState; |
| 315 | } |
| 316 | } |
| 317 | |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 318 | } |