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 | // |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 3 | // Copyright (c) 2012-2013 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 | { |
| 96 | gl::Renderbuffer *renderBuffer = framebuffer->getColorbuffer(i); |
| 97 | if (renderBuffer) |
| 98 | { |
| 99 | if (i > 0) |
| 100 | { |
| 101 | mrt = true; |
| 102 | } |
| 103 | |
| 104 | key.rtChannels[i][0] = renderBuffer->getRedSize() > 0; |
| 105 | key.rtChannels[i][1] = renderBuffer->getGreenSize() > 0; |
| 106 | key.rtChannels[i][2] = renderBuffer->getBlueSize() > 0; |
| 107 | key.rtChannels[i][3] = renderBuffer->getAlphaSize() > 0; |
| 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 | |
| 118 | BlendStateMap::iterator i = mBlendStateCache.find(key); |
daniel@transgaming.com | 0673d79 | 2012-11-28 19:37:44 +0000 | [diff] [blame] | 119 | if (i != mBlendStateCache.end()) |
| 120 | { |
| 121 | BlendStateCounterPair &state = i->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 | |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 199 | ID3D11RasterizerState *RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState, |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 200 | bool scissorEnabled, unsigned int depthSize) |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 201 | { |
| 202 | if (!mDevice) |
| 203 | { |
| 204 | ERR("RenderStateCache is not initialized."); |
| 205 | return NULL; |
| 206 | } |
| 207 | |
Geoff Lang | c142e9d | 2013-09-30 15:19:47 -0400 | [diff] [blame] | 208 | RasterizerStateKey key = { 0 }; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 209 | key.rasterizerState = rasterState; |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 210 | key.scissorEnabled = scissorEnabled; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 211 | key.depthSize = depthSize; |
| 212 | |
| 213 | RasterizerStateMap::iterator i = mRasterizerStateCache.find(key); |
| 214 | if (i != mRasterizerStateCache.end()) |
| 215 | { |
| 216 | RasterizerStateCounterPair &state = i->second; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 217 | state.second = mCounter++; |
| 218 | return state.first; |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | if (mRasterizerStateCache.size() >= kMaxRasterizerStates) |
| 223 | { |
| 224 | TRACE("Overflowed the limit of %u rasterizer states, removing the least recently used " |
| 225 | "to make room.", kMaxRasterizerStates); |
| 226 | |
| 227 | RasterizerStateMap::iterator leastRecentlyUsed = mRasterizerStateCache.begin(); |
| 228 | for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++) |
| 229 | { |
| 230 | if (i->second.second < leastRecentlyUsed->second.second) |
| 231 | { |
| 232 | leastRecentlyUsed = i; |
| 233 | } |
| 234 | } |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 235 | SafeRelease(leastRecentlyUsed->second.first); |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 236 | mRasterizerStateCache.erase(leastRecentlyUsed); |
| 237 | } |
| 238 | |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 239 | D3D11_CULL_MODE cullMode = gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode); |
| 240 | |
| 241 | // Disable culling if drawing points |
| 242 | if (rasterState.pointDrawMode) |
| 243 | { |
| 244 | cullMode = D3D11_CULL_NONE; |
| 245 | } |
| 246 | |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 247 | D3D11_RASTERIZER_DESC rasterDesc; |
| 248 | rasterDesc.FillMode = D3D11_FILL_SOLID; |
shannon.woods@transgaming.com | dd2524c | 2013-02-28 23:04:33 +0000 | [diff] [blame] | 249 | rasterDesc.CullMode = cullMode; |
daniel@transgaming.com | 4d4fade | 2013-01-11 04:09:10 +0000 | [diff] [blame] | 250 | rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? FALSE: TRUE; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 251 | rasterDesc.DepthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize)); |
| 252 | rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of zero will preform no clamping, must be tested though. |
shannon.woods@transgaming.com | ce3128a | 2013-02-28 23:14:13 +0000 | [diff] [blame] | 253 | rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetFactor; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 254 | rasterDesc.DepthClipEnable = TRUE; |
daniel@transgaming.com | d55e8c1 | 2012-11-28 21:07:02 +0000 | [diff] [blame] | 255 | rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE; |
Nicolas Capens | fd39655 | 2013-06-18 21:41:30 -0400 | [diff] [blame] | 256 | rasterDesc.MultisampleEnable = rasterState.multiSample; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 257 | rasterDesc.AntialiasedLineEnable = FALSE; |
| 258 | |
daniel@transgaming.com | 3cf8650 | 2013-01-11 04:08:40 +0000 | [diff] [blame] | 259 | ID3D11RasterizerState *dx11RasterizerState = NULL; |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 260 | HRESULT result = mDevice->CreateRasterizerState(&rasterDesc, &dx11RasterizerState); |
| 261 | if (FAILED(result) || !dx11RasterizerState) |
| 262 | { |
| 263 | ERR("Unable to create a ID3D11RasterizerState, HRESULT: 0x%X.", result); |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | mRasterizerStateCache.insert(std::make_pair(key, std::make_pair(dx11RasterizerState, mCounter++))); |
| 268 | |
daniel@transgaming.com | ed453e0 | 2012-11-28 19:38:11 +0000 | [diff] [blame] | 269 | return dx11RasterizerState; |
| 270 | } |
| 271 | } |
| 272 | |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 273 | std::size_t RenderStateCache::hashDepthStencilState(const gl::DepthStencilState &dsState) |
| 274 | { |
| 275 | static const unsigned int seed = 0xABCDEF98; |
| 276 | |
| 277 | std::size_t hash = 0; |
| 278 | MurmurHash3_x86_32(&dsState, sizeof(gl::DepthStencilState), seed, &hash); |
| 279 | return hash; |
| 280 | } |
| 281 | |
| 282 | bool RenderStateCache::compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b) |
| 283 | { |
| 284 | return memcmp(&a, &b, sizeof(gl::DepthStencilState)) == 0; |
| 285 | } |
| 286 | |
daniel@transgaming.com | 3cf8650 | 2013-01-11 04:08:40 +0000 | [diff] [blame] | 287 | ID3D11DepthStencilState *RenderStateCache::getDepthStencilState(const gl::DepthStencilState &dsState) |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 288 | { |
| 289 | if (!mDevice) |
| 290 | { |
| 291 | ERR("RenderStateCache is not initialized."); |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | DepthStencilStateMap::iterator i = mDepthStencilStateCache.find(dsState); |
| 296 | if (i != mDepthStencilStateCache.end()) |
| 297 | { |
| 298 | DepthStencilStateCounterPair &state = i->second; |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 299 | state.second = mCounter++; |
| 300 | return state.first; |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | if (mDepthStencilStateCache.size() >= kMaxDepthStencilStates) |
| 305 | { |
| 306 | TRACE("Overflowed the limit of %u depth stencil states, removing the least recently used " |
| 307 | "to make room.", kMaxDepthStencilStates); |
| 308 | |
| 309 | DepthStencilStateMap::iterator leastRecentlyUsed = mDepthStencilStateCache.begin(); |
| 310 | for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++) |
| 311 | { |
| 312 | if (i->second.second < leastRecentlyUsed->second.second) |
| 313 | { |
| 314 | leastRecentlyUsed = i; |
| 315 | } |
| 316 | } |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 317 | SafeRelease(leastRecentlyUsed->second.first); |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 318 | mDepthStencilStateCache.erase(leastRecentlyUsed); |
| 319 | } |
| 320 | |
| 321 | D3D11_DEPTH_STENCIL_DESC dsDesc = { 0 }; |
| 322 | dsDesc.DepthEnable = dsState.depthTest ? TRUE : FALSE; |
| 323 | dsDesc.DepthWriteMask = gl_d3d11::ConvertDepthMask(dsState.depthMask); |
| 324 | dsDesc.DepthFunc = gl_d3d11::ConvertComparison(dsState.depthFunc); |
| 325 | dsDesc.StencilEnable = dsState.stencilTest ? TRUE : FALSE; |
| 326 | dsDesc.StencilReadMask = gl_d3d11::ConvertStencilMask(dsState.stencilMask); |
| 327 | dsDesc.StencilWriteMask = gl_d3d11::ConvertStencilMask(dsState.stencilWritemask); |
| 328 | dsDesc.FrontFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilFail); |
| 329 | dsDesc.FrontFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthFail); |
| 330 | dsDesc.FrontFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthPass); |
| 331 | dsDesc.FrontFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilFunc); |
| 332 | dsDesc.BackFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackFail); |
| 333 | dsDesc.BackFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthFail); |
| 334 | dsDesc.BackFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthPass); |
| 335 | dsDesc.BackFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilBackFunc); |
| 336 | |
daniel@transgaming.com | 3cf8650 | 2013-01-11 04:08:40 +0000 | [diff] [blame] | 337 | ID3D11DepthStencilState *dx11DepthStencilState = NULL; |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 338 | HRESULT result = mDevice->CreateDepthStencilState(&dsDesc, &dx11DepthStencilState); |
| 339 | if (FAILED(result) || !dx11DepthStencilState) |
| 340 | { |
| 341 | ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result); |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | mDepthStencilStateCache.insert(std::make_pair(dsState, std::make_pair(dx11DepthStencilState, mCounter++))); |
| 346 | |
daniel@transgaming.com | 53926ff | 2012-11-28 19:38:50 +0000 | [diff] [blame] | 347 | return dx11DepthStencilState; |
| 348 | } |
| 349 | } |
| 350 | |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 351 | std::size_t RenderStateCache::hashSamplerState(const gl::SamplerState &samplerState) |
| 352 | { |
| 353 | static const unsigned int seed = 0xABCDEF98; |
| 354 | |
| 355 | std::size_t hash = 0; |
| 356 | MurmurHash3_x86_32(&samplerState, sizeof(gl::SamplerState), seed, &hash); |
| 357 | return hash; |
| 358 | } |
| 359 | |
| 360 | bool RenderStateCache::compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b) |
| 361 | { |
daniel@transgaming.com | cc47bc0 | 2013-01-11 04:09:33 +0000 | [diff] [blame] | 362 | return memcmp(&a, &b, sizeof(gl::SamplerState)) == 0; |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 363 | } |
| 364 | |
daniel@transgaming.com | 3cf8650 | 2013-01-11 04:08:40 +0000 | [diff] [blame] | 365 | ID3D11SamplerState *RenderStateCache::getSamplerState(const gl::SamplerState &samplerState) |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 366 | { |
| 367 | if (!mDevice) |
| 368 | { |
| 369 | ERR("RenderStateCache is not initialized."); |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | SamplerStateMap::iterator i = mSamplerStateCache.find(samplerState); |
| 374 | if (i != mSamplerStateCache.end()) |
| 375 | { |
| 376 | SamplerStateCounterPair &state = i->second; |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 377 | state.second = mCounter++; |
| 378 | return state.first; |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | if (mSamplerStateCache.size() >= kMaxSamplerStates) |
| 383 | { |
| 384 | TRACE("Overflowed the limit of %u sampler states, removing the least recently used " |
| 385 | "to make room.", kMaxSamplerStates); |
| 386 | |
| 387 | SamplerStateMap::iterator leastRecentlyUsed = mSamplerStateCache.begin(); |
| 388 | for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++) |
| 389 | { |
| 390 | if (i->second.second < leastRecentlyUsed->second.second) |
| 391 | { |
| 392 | leastRecentlyUsed = i; |
| 393 | } |
| 394 | } |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 395 | SafeRelease(leastRecentlyUsed->second.first); |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 396 | mSamplerStateCache.erase(leastRecentlyUsed); |
| 397 | } |
| 398 | |
| 399 | D3D11_SAMPLER_DESC samplerDesc; |
Geoff Lang | c82fc41 | 2013-07-10 14:43:42 -0400 | [diff] [blame] | 400 | samplerDesc.Filter = gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter, |
| 401 | samplerState.maxAnisotropy, samplerState.compareMode); |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 402 | samplerDesc.AddressU = gl_d3d11::ConvertTextureWrap(samplerState.wrapS); |
| 403 | samplerDesc.AddressV = gl_d3d11::ConvertTextureWrap(samplerState.wrapT); |
shannon.woods%transgaming.com@gtempaccount.com | 0b3a8df | 2013-04-13 03:44:51 +0000 | [diff] [blame] | 404 | samplerDesc.AddressW = gl_d3d11::ConvertTextureWrap(samplerState.wrapR); |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 405 | samplerDesc.MipLODBias = static_cast<float>(samplerState.lodOffset); |
| 406 | samplerDesc.MaxAnisotropy = samplerState.maxAnisotropy; |
Geoff Lang | c82fc41 | 2013-07-10 14:43:42 -0400 | [diff] [blame] | 407 | samplerDesc.ComparisonFunc = gl_d3d11::ConvertComparison(samplerState.compareFunc); |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 408 | samplerDesc.BorderColor[0] = 0.0f; |
| 409 | samplerDesc.BorderColor[1] = 0.0f; |
| 410 | samplerDesc.BorderColor[2] = 0.0f; |
| 411 | samplerDesc.BorderColor[3] = 0.0f; |
shannon.woods@transgaming.com | 8675865 | 2013-02-28 23:20:26 +0000 | [diff] [blame] | 412 | samplerDesc.MinLOD = gl_d3d11::ConvertMinLOD(samplerState.minFilter, samplerState.lodOffset); |
| 413 | samplerDesc.MaxLOD = gl_d3d11::ConvertMaxLOD(samplerState.minFilter, samplerState.lodOffset); |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 414 | |
| 415 | ID3D11SamplerState *dx11SamplerState = NULL; |
| 416 | HRESULT result = mDevice->CreateSamplerState(&samplerDesc, &dx11SamplerState); |
| 417 | if (FAILED(result) || !dx11SamplerState) |
| 418 | { |
| 419 | ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result); |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | mSamplerStateCache.insert(std::make_pair(samplerState, std::make_pair(dx11SamplerState, mCounter++))); |
| 424 | |
daniel@transgaming.com | 8e4f552 | 2013-01-11 04:07:53 +0000 | [diff] [blame] | 425 | return dx11SamplerState; |
| 426 | } |
| 427 | } |
| 428 | |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 429 | } |