1 #include "precompiled.h"
3 // Copyright (c) 2012-2014 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"
13 #include "libGLESv2/renderer/Renderer.h"
14 #include "libGLESv2/Framebuffer.h"
15 #include "libGLESv2/Renderbuffer.h"
17 #include "common/debug.h"
18 #include "third_party/murmurhash/MurmurHash3.h"
23 template <typename mapType>
24 static void ClearStateMap(mapType &map)
26 for (mapType::iterator i = map.begin(); i != map.end(); i++)
28 SafeRelease(i->second.first);
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
36 const unsigned int RenderStateCache::kMaxBlendStates = 4096;
37 const unsigned int RenderStateCache::kMaxRasterizerStates = 4096;
38 const unsigned int RenderStateCache::kMaxDepthStencilStates = 4096;
39 const unsigned int RenderStateCache::kMaxSamplerStates = 4096;
41 RenderStateCache::RenderStateCache() : mDevice(NULL), mCounter(0),
42 mBlendStateCache(kMaxBlendStates, hashBlendState, compareBlendStates),
43 mRasterizerStateCache(kMaxRasterizerStates, hashRasterizerState, compareRasterizerStates),
44 mDepthStencilStateCache(kMaxDepthStencilStates, hashDepthStencilState, compareDepthStencilStates),
45 mSamplerStateCache(kMaxSamplerStates, hashSamplerState, compareSamplerStates)
49 RenderStateCache::~RenderStateCache()
54 void RenderStateCache::initialize(ID3D11Device *device)
60 void RenderStateCache::clear()
62 ClearStateMap(mBlendStateCache);
63 ClearStateMap(mRasterizerStateCache);
64 ClearStateMap(mDepthStencilStateCache);
65 ClearStateMap(mSamplerStateCache);
68 std::size_t RenderStateCache::hashBlendState(const BlendStateKey &blendState)
70 static const unsigned int seed = 0xABCDEF98;
73 MurmurHash3_x86_32(&blendState, sizeof(gl::BlendState), seed, &hash);
77 bool RenderStateCache::compareBlendStates(const BlendStateKey &a, const BlendStateKey &b)
79 return memcmp(&a, &b, sizeof(BlendStateKey)) == 0;
82 ID3D11BlendState *RenderStateCache::getBlendState(const gl::Framebuffer *framebuffer, const gl::BlendState &blendState)
86 ERR("RenderStateCache is not initialized.");
92 BlendStateKey key = { 0 };
93 key.blendState = blendState;
94 for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
96 gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(i);
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;
111 key.rtChannels[i][0] = false;
112 key.rtChannels[i][1] = false;
113 key.rtChannels[i][2] = false;
114 key.rtChannels[i][3] = false;
118 BlendStateMap::iterator keyIter = mBlendStateCache.find(key);
119 if (keyIter != mBlendStateCache.end())
121 BlendStateCounterPair &state = keyIter->second;
122 state.second = mCounter++;
127 if (mBlendStateCache.size() >= kMaxBlendStates)
129 TRACE("Overflowed the limit of %u blend states, removing the least recently used "
130 "to make room.", kMaxBlendStates);
132 BlendStateMap::iterator leastRecentlyUsed = mBlendStateCache.begin();
133 for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++)
135 if (i->second.second < leastRecentlyUsed->second.second)
137 leastRecentlyUsed = i;
140 SafeRelease(leastRecentlyUsed->second.first);
141 mBlendStateCache.erase(leastRecentlyUsed);
144 // Create a new blend state and insert it into the cache
145 D3D11_BLEND_DESC blendDesc = { 0 };
146 blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage;
147 blendDesc.IndependentBlendEnable = mrt ? TRUE : FALSE;
149 for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
151 D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = blendDesc.RenderTarget[i];
153 rtBlend.BlendEnable = blendState.blend;
154 if (blendState.blend)
156 rtBlend.SrcBlend = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendRGB, false);
157 rtBlend.DestBlend = gl_d3d11::ConvertBlendFunc(blendState.destBlendRGB, false);
158 rtBlend.BlendOp = gl_d3d11::ConvertBlendOp(blendState.blendEquationRGB);
160 rtBlend.SrcBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendAlpha, true);
161 rtBlend.DestBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.destBlendAlpha, true);
162 rtBlend.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha);
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);
171 ID3D11BlendState *dx11BlendState = NULL;
172 HRESULT result = mDevice->CreateBlendState(&blendDesc, &dx11BlendState);
173 if (FAILED(result) || !dx11BlendState)
175 ERR("Unable to create a ID3D11BlendState, HRESULT: 0x%X.", result);
179 mBlendStateCache.insert(std::make_pair(key, std::make_pair(dx11BlendState, mCounter++)));
181 return dx11BlendState;
185 std::size_t RenderStateCache::hashRasterizerState(const RasterizerStateKey &rasterState)
187 static const unsigned int seed = 0xABCDEF98;
189 std::size_t hash = 0;
190 MurmurHash3_x86_32(&rasterState, sizeof(RasterizerStateKey), seed, &hash);
194 bool RenderStateCache::compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b)
196 return memcmp(&a, &b, sizeof(RasterizerStateKey)) == 0;
199 ID3D11RasterizerState *RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState, bool scissorEnabled)
203 ERR("RenderStateCache is not initialized.");
207 RasterizerStateKey key = { 0 };
208 key.rasterizerState = rasterState;
209 key.scissorEnabled = scissorEnabled;
211 RasterizerStateMap::iterator keyIter = mRasterizerStateCache.find(key);
212 if (keyIter != mRasterizerStateCache.end())
214 RasterizerStateCounterPair &state = keyIter->second;
215 state.second = mCounter++;
220 if (mRasterizerStateCache.size() >= kMaxRasterizerStates)
222 TRACE("Overflowed the limit of %u rasterizer states, removing the least recently used "
223 "to make room.", kMaxRasterizerStates);
225 RasterizerStateMap::iterator leastRecentlyUsed = mRasterizerStateCache.begin();
226 for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++)
228 if (i->second.second < leastRecentlyUsed->second.second)
230 leastRecentlyUsed = i;
233 SafeRelease(leastRecentlyUsed->second.first);
234 mRasterizerStateCache.erase(leastRecentlyUsed);
237 D3D11_CULL_MODE cullMode = gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode);
239 // Disable culling if drawing points
240 if (rasterState.pointDrawMode)
242 cullMode = D3D11_CULL_NONE;
245 D3D11_RASTERIZER_DESC rasterDesc;
246 rasterDesc.FillMode = D3D11_FILL_SOLID;
247 rasterDesc.CullMode = cullMode;
248 rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? FALSE: TRUE;
249 rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of zero will preform no clamping, must be tested though.
250 rasterDesc.DepthClipEnable = TRUE;
251 rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE;
252 rasterDesc.MultisampleEnable = rasterState.multiSample;
253 rasterDesc.AntialiasedLineEnable = FALSE;
255 if (rasterState.polygonOffsetFill)
257 rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetFactor;
258 rasterDesc.DepthBias = (INT)rasterState.polygonOffsetUnits;
262 rasterDesc.SlopeScaledDepthBias = 0.0f;
263 rasterDesc.DepthBias = 0;
266 ID3D11RasterizerState *dx11RasterizerState = NULL;
267 HRESULT result = mDevice->CreateRasterizerState(&rasterDesc, &dx11RasterizerState);
268 if (FAILED(result) || !dx11RasterizerState)
270 ERR("Unable to create a ID3D11RasterizerState, HRESULT: 0x%X.", result);
274 mRasterizerStateCache.insert(std::make_pair(key, std::make_pair(dx11RasterizerState, mCounter++)));
276 return dx11RasterizerState;
280 std::size_t RenderStateCache::hashDepthStencilState(const gl::DepthStencilState &dsState)
282 static const unsigned int seed = 0xABCDEF98;
284 std::size_t hash = 0;
285 MurmurHash3_x86_32(&dsState, sizeof(gl::DepthStencilState), seed, &hash);
289 bool RenderStateCache::compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b)
291 return memcmp(&a, &b, sizeof(gl::DepthStencilState)) == 0;
294 ID3D11DepthStencilState *RenderStateCache::getDepthStencilState(const gl::DepthStencilState &dsState)
298 ERR("RenderStateCache is not initialized.");
302 DepthStencilStateMap::iterator keyIter = mDepthStencilStateCache.find(dsState);
303 if (keyIter != mDepthStencilStateCache.end())
305 DepthStencilStateCounterPair &state = keyIter->second;
306 state.second = mCounter++;
311 if (mDepthStencilStateCache.size() >= kMaxDepthStencilStates)
313 TRACE("Overflowed the limit of %u depth stencil states, removing the least recently used "
314 "to make room.", kMaxDepthStencilStates);
316 DepthStencilStateMap::iterator leastRecentlyUsed = mDepthStencilStateCache.begin();
317 for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++)
319 if (i->second.second < leastRecentlyUsed->second.second)
321 leastRecentlyUsed = i;
324 SafeRelease(leastRecentlyUsed->second.first);
325 mDepthStencilStateCache.erase(leastRecentlyUsed);
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);
344 ID3D11DepthStencilState *dx11DepthStencilState = NULL;
345 HRESULT result = mDevice->CreateDepthStencilState(&dsDesc, &dx11DepthStencilState);
346 if (FAILED(result) || !dx11DepthStencilState)
348 ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
352 mDepthStencilStateCache.insert(std::make_pair(dsState, std::make_pair(dx11DepthStencilState, mCounter++)));
354 return dx11DepthStencilState;
358 std::size_t RenderStateCache::hashSamplerState(const gl::SamplerState &samplerState)
360 static const unsigned int seed = 0xABCDEF98;
362 std::size_t hash = 0;
363 MurmurHash3_x86_32(&samplerState, sizeof(gl::SamplerState), seed, &hash);
367 bool RenderStateCache::compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b)
369 return memcmp(&a, &b, sizeof(gl::SamplerState)) == 0;
372 ID3D11SamplerState *RenderStateCache::getSamplerState(const gl::SamplerState &samplerState)
376 ERR("RenderStateCache is not initialized.");
380 SamplerStateMap::iterator keyIter = mSamplerStateCache.find(samplerState);
381 if (keyIter != mSamplerStateCache.end())
383 SamplerStateCounterPair &state = keyIter->second;
384 state.second = mCounter++;
389 if (mSamplerStateCache.size() >= kMaxSamplerStates)
391 TRACE("Overflowed the limit of %u sampler states, removing the least recently used "
392 "to make room.", kMaxSamplerStates);
394 SamplerStateMap::iterator leastRecentlyUsed = mSamplerStateCache.begin();
395 for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++)
397 if (i->second.second < leastRecentlyUsed->second.second)
399 leastRecentlyUsed = i;
402 SafeRelease(leastRecentlyUsed->second.first);
403 mSamplerStateCache.erase(leastRecentlyUsed);
406 D3D11_SAMPLER_DESC samplerDesc;
407 samplerDesc.Filter = gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter,
408 samplerState.maxAnisotropy, samplerState.compareMode);
409 samplerDesc.AddressU = gl_d3d11::ConvertTextureWrap(samplerState.wrapS);
410 samplerDesc.AddressV = gl_d3d11::ConvertTextureWrap(samplerState.wrapT);
411 samplerDesc.AddressW = gl_d3d11::ConvertTextureWrap(samplerState.wrapR);
412 samplerDesc.MipLODBias = 0;
413 samplerDesc.MaxAnisotropy = samplerState.maxAnisotropy;
414 samplerDesc.ComparisonFunc = gl_d3d11::ConvertComparison(samplerState.compareFunc);
415 samplerDesc.BorderColor[0] = 0.0f;
416 samplerDesc.BorderColor[1] = 0.0f;
417 samplerDesc.BorderColor[2] = 0.0f;
418 samplerDesc.BorderColor[3] = 0.0f;
419 samplerDesc.MinLOD = samplerState.minLod;
420 samplerDesc.MaxLOD = samplerState.maxLod;
422 ID3D11SamplerState *dx11SamplerState = NULL;
423 HRESULT result = mDevice->CreateSamplerState(&samplerDesc, &dx11SamplerState);
424 if (FAILED(result) || !dx11SamplerState)
426 ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
430 mSamplerStateCache.insert(std::make_pair(samplerState, std::make_pair(dx11SamplerState, mCounter++)));
432 return dx11SamplerState;