1 #include "precompiled.h"
3 // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
8 // RenderStateCache.cpp: Defines rx::RenderStateCache, a cache of Direct3D render
11 #include "libGLESv2/renderer/d3d11/RenderStateCache.h"
12 #include "libGLESv2/renderer/d3d11/renderer11_utils.h"
14 #include "libGLESv2/Framebuffer.h"
15 #include "libGLESv2/Renderbuffer.h"
16 #include "libGLESv2/utilities.h"
17 #include "common/debug.h"
18 #include "third_party/murmurhash/MurmurHash3.h"
23 // MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState,
24 // ID3D11Device::CreateDepthStencilState and ID3D11Device::CreateSamplerState claims the maximum
25 // number of unique states of each type an application can create is 4096
26 const unsigned int RenderStateCache::kMaxBlendStates = 4096;
27 const unsigned int RenderStateCache::kMaxRasterizerStates = 4096;
28 const unsigned int RenderStateCache::kMaxDepthStencilStates = 4096;
29 const unsigned int RenderStateCache::kMaxSamplerStates = 4096;
31 RenderStateCache::RenderStateCache() : mDevice(NULL), mCounter(0),
32 mBlendStateCache(kMaxBlendStates, hashBlendState, compareBlendStates),
33 mRasterizerStateCache(kMaxRasterizerStates, hashRasterizerState, compareRasterizerStates),
34 mDepthStencilStateCache(kMaxDepthStencilStates, hashDepthStencilState, compareDepthStencilStates),
35 mSamplerStateCache(kMaxSamplerStates, hashSamplerState, compareSamplerStates)
39 RenderStateCache::~RenderStateCache()
44 void RenderStateCache::initialize(ID3D11Device *device)
50 void RenderStateCache::clear()
52 for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++)
54 i->second.first->Release();
56 mBlendStateCache.clear();
58 for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++)
60 i->second.first->Release();
62 mRasterizerStateCache.clear();
64 for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++)
66 i->second.first->Release();
68 mDepthStencilStateCache.clear();
70 for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++)
72 i->second.first->Release();
74 mSamplerStateCache.clear();
77 std::size_t RenderStateCache::hashBlendState(const BlendStateKey &blendState)
79 static const unsigned int seed = 0xABCDEF98;
82 MurmurHash3_x86_32(&blendState, sizeof(BlendStateKey), seed, &hash);
86 bool RenderStateCache::compareBlendStates(const BlendStateKey &a, const BlendStateKey &b)
88 return memcmp(&a, &b, sizeof(gl::BlendState)) == 0;
91 ID3D11BlendState *RenderStateCache::getBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState)
95 ERR("RenderStateCache is not initialized.");
101 BlendStateKey key = { 0 };
102 key.blendState = blendState;
103 for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
105 gl::Renderbuffer *renderBuffer = framebuffer->getColorbuffer(i);
113 GLenum internalFormat = renderBuffer->getInternalFormat();
114 key.rtChannels[i][0] = gl::GetRedSize(internalFormat) > 0;
115 key.rtChannels[i][1] = gl::GetGreenSize(internalFormat) > 0;
116 key.rtChannels[i][2] = gl::GetBlueSize(internalFormat) > 0;;
117 key.rtChannels[i][3] = gl::GetAlphaSize(internalFormat) > 0;
121 key.rtChannels[i][0] = false;
122 key.rtChannels[i][1] = false;
123 key.rtChannels[i][2] = false;
124 key.rtChannels[i][3] = false;
128 BlendStateMap::iterator i = mBlendStateCache.find(key);
129 if (i != mBlendStateCache.end())
131 BlendStateCounterPair &state = i->second;
132 state.second = mCounter++;
137 if (mBlendStateCache.size() >= kMaxBlendStates)
139 TRACE("Overflowed the limit of %u blend states, removing the least recently used "
140 "to make room.", kMaxBlendStates);
142 BlendStateMap::iterator leastRecentlyUsed = mBlendStateCache.begin();
143 for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++)
145 if (i->second.second < leastRecentlyUsed->second.second)
147 leastRecentlyUsed = i;
150 leastRecentlyUsed->second.first->Release();
151 mBlendStateCache.erase(leastRecentlyUsed);
154 // Create a new blend state and insert it into the cache
155 D3D11_BLEND_DESC blendDesc = { 0 };
156 blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage;
157 blendDesc.IndependentBlendEnable = mrt ? TRUE : FALSE;
159 for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
161 D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = blendDesc.RenderTarget[i];
163 rtBlend.BlendEnable = blendState.blend;
164 if (blendState.blend)
166 rtBlend.SrcBlend = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendRGB, false);
167 rtBlend.DestBlend = gl_d3d11::ConvertBlendFunc(blendState.destBlendRGB, false);
168 rtBlend.BlendOp = gl_d3d11::ConvertBlendOp(blendState.blendEquationRGB);
170 rtBlend.SrcBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendAlpha, true);
171 rtBlend.DestBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.destBlendAlpha, true);
172 rtBlend.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha);
175 rtBlend.RenderTargetWriteMask = gl_d3d11::ConvertColorMask(key.rtChannels[i][0] && blendState.colorMaskRed,
176 key.rtChannels[i][1] && blendState.colorMaskGreen,
177 key.rtChannels[i][2] && blendState.colorMaskBlue,
178 key.rtChannels[i][3] && blendState.colorMaskAlpha);
181 ID3D11BlendState *dx11BlendState = NULL;
182 HRESULT result = mDevice->CreateBlendState(&blendDesc, &dx11BlendState);
183 if (FAILED(result) || !dx11BlendState)
185 ERR("Unable to create a ID3D11BlendState, HRESULT: 0x%X.", result);
189 mBlendStateCache.insert(std::make_pair(key, std::make_pair(dx11BlendState, mCounter++)));
191 return dx11BlendState;
195 std::size_t RenderStateCache::hashRasterizerState(const RasterizerStateKey &rasterState)
197 static const unsigned int seed = 0xABCDEF98;
199 std::size_t hash = 0;
200 MurmurHash3_x86_32(&rasterState, sizeof(RasterizerStateKey), seed, &hash);
204 bool RenderStateCache::compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b)
206 return memcmp(&a, &b, sizeof(RasterizerStateKey)) == 0;
209 ID3D11RasterizerState *RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState,
210 bool scissorEnabled, unsigned int depthSize)
214 ERR("RenderStateCache is not initialized.");
218 RasterizerStateKey key;
219 key.rasterizerState = rasterState;
220 key.scissorEnabled = scissorEnabled;
221 key.depthSize = depthSize;
223 RasterizerStateMap::iterator i = mRasterizerStateCache.find(key);
224 if (i != mRasterizerStateCache.end())
226 RasterizerStateCounterPair &state = i->second;
227 state.second = mCounter++;
232 if (mRasterizerStateCache.size() >= kMaxRasterizerStates)
234 TRACE("Overflowed the limit of %u rasterizer states, removing the least recently used "
235 "to make room.", kMaxRasterizerStates);
237 RasterizerStateMap::iterator leastRecentlyUsed = mRasterizerStateCache.begin();
238 for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++)
240 if (i->second.second < leastRecentlyUsed->second.second)
242 leastRecentlyUsed = i;
245 leastRecentlyUsed->second.first->Release();
246 mRasterizerStateCache.erase(leastRecentlyUsed);
249 D3D11_CULL_MODE cullMode = gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode);
251 // Disable culling if drawing points
252 if (rasterState.pointDrawMode)
254 cullMode = D3D11_CULL_NONE;
257 D3D11_RASTERIZER_DESC rasterDesc;
258 rasterDesc.FillMode = D3D11_FILL_SOLID;
259 rasterDesc.CullMode = cullMode;
260 rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? FALSE: TRUE;
261 rasterDesc.DepthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize));
262 rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of zero will preform no clamping, must be tested though.
263 rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetFactor;
264 rasterDesc.DepthClipEnable = TRUE;
265 rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE;
266 rasterDesc.MultisampleEnable = rasterState.multiSample;
267 rasterDesc.AntialiasedLineEnable = FALSE;
269 ID3D11RasterizerState *dx11RasterizerState = NULL;
270 HRESULT result = mDevice->CreateRasterizerState(&rasterDesc, &dx11RasterizerState);
271 if (FAILED(result) || !dx11RasterizerState)
273 ERR("Unable to create a ID3D11RasterizerState, HRESULT: 0x%X.", result);
277 mRasterizerStateCache.insert(std::make_pair(key, std::make_pair(dx11RasterizerState, mCounter++)));
279 return dx11RasterizerState;
283 std::size_t RenderStateCache::hashDepthStencilState(const gl::DepthStencilState &dsState)
285 static const unsigned int seed = 0xABCDEF98;
287 std::size_t hash = 0;
288 MurmurHash3_x86_32(&dsState, sizeof(gl::DepthStencilState), seed, &hash);
292 bool RenderStateCache::compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b)
294 return memcmp(&a, &b, sizeof(gl::DepthStencilState)) == 0;
297 ID3D11DepthStencilState *RenderStateCache::getDepthStencilState(const gl::DepthStencilState &dsState)
301 ERR("RenderStateCache is not initialized.");
305 DepthStencilStateMap::iterator i = mDepthStencilStateCache.find(dsState);
306 if (i != mDepthStencilStateCache.end())
308 DepthStencilStateCounterPair &state = i->second;
309 state.second = mCounter++;
314 if (mDepthStencilStateCache.size() >= kMaxDepthStencilStates)
316 TRACE("Overflowed the limit of %u depth stencil states, removing the least recently used "
317 "to make room.", kMaxDepthStencilStates);
319 DepthStencilStateMap::iterator leastRecentlyUsed = mDepthStencilStateCache.begin();
320 for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++)
322 if (i->second.second < leastRecentlyUsed->second.second)
324 leastRecentlyUsed = i;
327 leastRecentlyUsed->second.first->Release();
328 mDepthStencilStateCache.erase(leastRecentlyUsed);
331 D3D11_DEPTH_STENCIL_DESC dsDesc = { 0 };
332 dsDesc.DepthEnable = dsState.depthTest ? TRUE : FALSE;
333 dsDesc.DepthWriteMask = gl_d3d11::ConvertDepthMask(dsState.depthMask);
334 dsDesc.DepthFunc = gl_d3d11::ConvertComparison(dsState.depthFunc);
335 dsDesc.StencilEnable = dsState.stencilTest ? TRUE : FALSE;
336 dsDesc.StencilReadMask = gl_d3d11::ConvertStencilMask(dsState.stencilMask);
337 dsDesc.StencilWriteMask = gl_d3d11::ConvertStencilMask(dsState.stencilWritemask);
338 dsDesc.FrontFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilFail);
339 dsDesc.FrontFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthFail);
340 dsDesc.FrontFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthPass);
341 dsDesc.FrontFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilFunc);
342 dsDesc.BackFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackFail);
343 dsDesc.BackFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthFail);
344 dsDesc.BackFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthPass);
345 dsDesc.BackFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilBackFunc);
347 ID3D11DepthStencilState *dx11DepthStencilState = NULL;
348 HRESULT result = mDevice->CreateDepthStencilState(&dsDesc, &dx11DepthStencilState);
349 if (FAILED(result) || !dx11DepthStencilState)
351 ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
355 mDepthStencilStateCache.insert(std::make_pair(dsState, std::make_pair(dx11DepthStencilState, mCounter++)));
357 return dx11DepthStencilState;
361 std::size_t RenderStateCache::hashSamplerState(const gl::SamplerState &samplerState)
363 static const unsigned int seed = 0xABCDEF98;
365 std::size_t hash = 0;
366 MurmurHash3_x86_32(&samplerState, sizeof(gl::SamplerState), seed, &hash);
370 bool RenderStateCache::compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b)
372 return memcmp(&a, &b, sizeof(gl::SamplerState)) == 0;
375 ID3D11SamplerState *RenderStateCache::getSamplerState(const gl::SamplerState &samplerState)
379 ERR("RenderStateCache is not initialized.");
383 SamplerStateMap::iterator i = mSamplerStateCache.find(samplerState);
384 if (i != mSamplerStateCache.end())
386 SamplerStateCounterPair &state = i->second;
387 state.second = mCounter++;
392 if (mSamplerStateCache.size() >= kMaxSamplerStates)
394 TRACE("Overflowed the limit of %u sampler states, removing the least recently used "
395 "to make room.", kMaxSamplerStates);
397 SamplerStateMap::iterator leastRecentlyUsed = mSamplerStateCache.begin();
398 for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++)
400 if (i->second.second < leastRecentlyUsed->second.second)
402 leastRecentlyUsed = i;
405 leastRecentlyUsed->second.first->Release();
406 mSamplerStateCache.erase(leastRecentlyUsed);
409 D3D11_SAMPLER_DESC samplerDesc;
410 samplerDesc.Filter = gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter, samplerState.maxAnisotropy);
411 samplerDesc.AddressU = gl_d3d11::ConvertTextureWrap(samplerState.wrapS);
412 samplerDesc.AddressV = gl_d3d11::ConvertTextureWrap(samplerState.wrapT);
413 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
414 samplerDesc.MipLODBias = static_cast<float>(samplerState.lodOffset);
415 samplerDesc.MaxAnisotropy = samplerState.maxAnisotropy;
416 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
417 samplerDesc.BorderColor[0] = 0.0f;
418 samplerDesc.BorderColor[1] = 0.0f;
419 samplerDesc.BorderColor[2] = 0.0f;
420 samplerDesc.BorderColor[3] = 0.0f;
421 samplerDesc.MinLOD = gl_d3d11::ConvertMinLOD(samplerState.minFilter, samplerState.lodOffset);
422 samplerDesc.MaxLOD = gl_d3d11::ConvertMaxLOD(samplerState.minFilter, samplerState.lodOffset);
424 ID3D11SamplerState *dx11SamplerState = NULL;
425 HRESULT result = mDevice->CreateSamplerState(&samplerDesc, &dx11SamplerState);
426 if (FAILED(result) || !dx11SamplerState)
428 ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
432 mSamplerStateCache.insert(std::make_pair(samplerState, std::make_pair(dx11SamplerState, mCounter++)));
434 return dx11SamplerState;