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