зеркало из https://github.com/AvaloniaUI/angle.git
Introduce MRUCache.
This library comes from Chromium's base, and is useful for many use cases in ANGLE. It can replace the custom MRU code we use in the RenderStateCache. It will also be useful for implementing a program binary cache. BUG=angleproject:2044 Change-Id: Iba166fe380d7ed4e3123428b0227b9d299f756d1 Reviewed-on: https://chromium-review.googlesource.com/516384 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Родитель
af713a2408
Коммит
9216a6e26d
2
BUILD.gn
2
BUILD.gn
|
@ -390,7 +390,7 @@ config("libANGLE_config") {
|
|||
static_library("libANGLE") {
|
||||
sources = rebase_path(gles_gypi.libangle_sources, ".", "src")
|
||||
|
||||
include_dirs = []
|
||||
include_dirs = [ "src/third_party/mrucache" ]
|
||||
libs = []
|
||||
defines = []
|
||||
public_deps = [
|
||||
|
|
|
@ -23,6 +23,22 @@ namespace rx
|
|||
{
|
||||
using namespace gl_d3d11;
|
||||
|
||||
namespace
|
||||
{
|
||||
template <typename T>
|
||||
void TrimCache(unsigned int maxStates, unsigned int gcLimit, const char *name, T *cache)
|
||||
{
|
||||
unsigned int kGarbageCollectionLimit = maxStates / 2 + gcLimit;
|
||||
|
||||
if (cache->size() >= kGarbageCollectionLimit)
|
||||
{
|
||||
WARN() << "Overflowed the limit of " << (maxStates / 2) << " " << name
|
||||
<< " states, removing the least recently used to make room.";
|
||||
cache->ShrinkToSize(maxStates / 2);
|
||||
}
|
||||
}
|
||||
} // anonymous namespace
|
||||
|
||||
template <typename T>
|
||||
std::size_t ComputeGenericHash(const T &key)
|
||||
{
|
||||
|
@ -38,23 +54,12 @@ template std::size_t ComputeGenericHash(const rx::d3d11::RasterizerStateKey &);
|
|||
template std::size_t ComputeGenericHash(const gl::DepthStencilState &);
|
||||
template std::size_t ComputeGenericHash(const gl::SamplerState &);
|
||||
|
||||
// MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState,
|
||||
// ID3D11Device::CreateDepthStencilState and ID3D11Device::CreateSamplerState claims the maximum
|
||||
// number of unique states of each type an application can create is 4096
|
||||
// TODO(ShahmeerEsmail): Revisit the cache sizes to make sure they are appropriate for most
|
||||
// scenarios.
|
||||
const unsigned int RenderStateCache::kMaxBlendStates = 2048;
|
||||
const unsigned int RenderStateCache::kMaxRasterizerStates = 2048;
|
||||
const unsigned int RenderStateCache::kMaxDepthStencilStates = 2048;
|
||||
const unsigned int RenderStateCache::kMaxSamplerStates = 2048;
|
||||
|
||||
RenderStateCache::RenderStateCache(Renderer11 *renderer)
|
||||
: mRenderer(renderer),
|
||||
mCounter(0),
|
||||
mBlendStateCache(kMaxBlendStates),
|
||||
mRasterizerStateCache(kMaxRasterizerStates),
|
||||
mDepthStencilStateCache(kMaxDepthStencilStates),
|
||||
mSamplerStateCache(kMaxSamplerStates)
|
||||
mBlendStateCache(kMaxStates),
|
||||
mRasterizerStateCache(kMaxStates),
|
||||
mDepthStencilStateCache(kMaxStates),
|
||||
mSamplerStateCache(kMaxStates)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -64,10 +69,10 @@ RenderStateCache::~RenderStateCache()
|
|||
|
||||
void RenderStateCache::clear()
|
||||
{
|
||||
mBlendStateCache.clear();
|
||||
mRasterizerStateCache.clear();
|
||||
mDepthStencilStateCache.clear();
|
||||
mSamplerStateCache.clear();
|
||||
mBlendStateCache.Clear();
|
||||
mRasterizerStateCache.Clear();
|
||||
mDepthStencilStateCache.Clear();
|
||||
mSamplerStateCache.Clear();
|
||||
}
|
||||
|
||||
// static
|
||||
|
@ -115,68 +120,50 @@ d3d11::BlendStateKey RenderStateCache::GetBlendStateKey(const gl::Framebuffer *f
|
|||
gl::Error RenderStateCache::getBlendState(const d3d11::BlendStateKey &key,
|
||||
ID3D11BlendState **outBlendState)
|
||||
{
|
||||
BlendStateMap::iterator keyIter = mBlendStateCache.find(key);
|
||||
auto keyIter = mBlendStateCache.Get(key);
|
||||
if (keyIter != mBlendStateCache.end())
|
||||
{
|
||||
BlendStateCounterPair &state = keyIter->second;
|
||||
state.second = mCounter++;
|
||||
*outBlendState = state.first.get();
|
||||
*outBlendState = keyIter->second.get();
|
||||
return gl::NoError();
|
||||
}
|
||||
else
|
||||
|
||||
TrimCache(kMaxStates, kGCLimit, "blend", &mBlendStateCache);
|
||||
|
||||
// Create a new blend state and insert it into the cache
|
||||
D3D11_BLEND_DESC blendDesc;
|
||||
D3D11_RENDER_TARGET_BLEND_DESC &rtDesc0 = blendDesc.RenderTarget[0];
|
||||
const gl::BlendState &blendState = key.blendState;
|
||||
|
||||
blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage;
|
||||
blendDesc.IndependentBlendEnable = key.mrt ? TRUE : FALSE;
|
||||
|
||||
rtDesc0 = {};
|
||||
|
||||
if (blendState.blend)
|
||||
{
|
||||
if (mBlendStateCache.size() >= kMaxBlendStates)
|
||||
{
|
||||
WARN() << "Overflowed the limit of " << kMaxBlendStates
|
||||
<< " blend states, removing the least recently used to make room.";
|
||||
|
||||
BlendStateMap::iterator leastRecentlyUsed = mBlendStateCache.begin();
|
||||
for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++)
|
||||
{
|
||||
if (i->second.second < leastRecentlyUsed->second.second)
|
||||
{
|
||||
leastRecentlyUsed = i;
|
||||
}
|
||||
}
|
||||
mBlendStateCache.erase(leastRecentlyUsed);
|
||||
}
|
||||
|
||||
// Create a new blend state and insert it into the cache
|
||||
D3D11_BLEND_DESC blendDesc;
|
||||
D3D11_RENDER_TARGET_BLEND_DESC &rtDesc0 = blendDesc.RenderTarget[0];
|
||||
const gl::BlendState &blendState = key.blendState;
|
||||
|
||||
blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage;
|
||||
blendDesc.IndependentBlendEnable = key.mrt ? TRUE : FALSE;
|
||||
|
||||
rtDesc0 = {};
|
||||
|
||||
if (blendState.blend)
|
||||
{
|
||||
rtDesc0.BlendEnable = true;
|
||||
rtDesc0.SrcBlend = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendRGB, false);
|
||||
rtDesc0.DestBlend = gl_d3d11::ConvertBlendFunc(blendState.destBlendRGB, false);
|
||||
rtDesc0.BlendOp = gl_d3d11::ConvertBlendOp(blendState.blendEquationRGB);
|
||||
rtDesc0.SrcBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendAlpha, true);
|
||||
rtDesc0.DestBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.destBlendAlpha, true);
|
||||
rtDesc0.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha);
|
||||
}
|
||||
|
||||
rtDesc0.RenderTargetWriteMask = key.rtvMasks[0];
|
||||
|
||||
for (unsigned int i = 1; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
|
||||
{
|
||||
blendDesc.RenderTarget[i] = rtDesc0;
|
||||
blendDesc.RenderTarget[i].RenderTargetWriteMask = key.rtvMasks[i];
|
||||
}
|
||||
|
||||
d3d11::BlendState d3dBlendState;
|
||||
ANGLE_TRY(mRenderer->allocateResource(blendDesc, &d3dBlendState));
|
||||
*outBlendState = d3dBlendState.get();
|
||||
mBlendStateCache[key] = std::make_pair(std::move(d3dBlendState), mCounter++);
|
||||
|
||||
return gl::NoError();
|
||||
rtDesc0.BlendEnable = true;
|
||||
rtDesc0.SrcBlend = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendRGB, false);
|
||||
rtDesc0.DestBlend = gl_d3d11::ConvertBlendFunc(blendState.destBlendRGB, false);
|
||||
rtDesc0.BlendOp = gl_d3d11::ConvertBlendOp(blendState.blendEquationRGB);
|
||||
rtDesc0.SrcBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendAlpha, true);
|
||||
rtDesc0.DestBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.destBlendAlpha, true);
|
||||
rtDesc0.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha);
|
||||
}
|
||||
|
||||
rtDesc0.RenderTargetWriteMask = key.rtvMasks[0];
|
||||
|
||||
for (unsigned int i = 1; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
|
||||
{
|
||||
blendDesc.RenderTarget[i] = rtDesc0;
|
||||
blendDesc.RenderTarget[i].RenderTargetWriteMask = key.rtvMasks[i];
|
||||
}
|
||||
|
||||
d3d11::BlendState d3dBlendState;
|
||||
ANGLE_TRY(mRenderer->allocateResource(blendDesc, &d3dBlendState));
|
||||
*outBlendState = d3dBlendState.get();
|
||||
mBlendStateCache.Put(key, std::move(d3dBlendState));
|
||||
|
||||
return gl::NoError();
|
||||
}
|
||||
|
||||
gl::Error RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState, bool scissorEnabled,
|
||||
|
@ -186,98 +173,65 @@ gl::Error RenderStateCache::getRasterizerState(const gl::RasterizerState &raster
|
|||
key.rasterizerState = rasterState;
|
||||
key.scissorEnabled = scissorEnabled;
|
||||
|
||||
RasterizerStateMap::iterator keyIter = mRasterizerStateCache.find(key);
|
||||
auto keyIter = mRasterizerStateCache.Get(key);
|
||||
if (keyIter != mRasterizerStateCache.end())
|
||||
{
|
||||
RasterizerStateCounterPair &state = keyIter->second;
|
||||
state.second = mCounter++;
|
||||
*outRasterizerState = state.first.get();
|
||||
*outRasterizerState = keyIter->second.get();
|
||||
return gl::NoError();
|
||||
}
|
||||
|
||||
TrimCache(kMaxStates, kGCLimit, "rasterizer", &mRasterizerStateCache);
|
||||
|
||||
D3D11_CULL_MODE cullMode =
|
||||
gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode);
|
||||
|
||||
// Disable culling if drawing points
|
||||
if (rasterState.pointDrawMode)
|
||||
{
|
||||
cullMode = D3D11_CULL_NONE;
|
||||
}
|
||||
|
||||
D3D11_RASTERIZER_DESC rasterDesc;
|
||||
rasterDesc.FillMode = D3D11_FILL_SOLID;
|
||||
rasterDesc.CullMode = cullMode;
|
||||
rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? FALSE : TRUE;
|
||||
rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of
|
||||
// zero will preform no clamping, must be tested though.
|
||||
rasterDesc.DepthClipEnable = TRUE;
|
||||
rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE;
|
||||
rasterDesc.MultisampleEnable = rasterState.multiSample;
|
||||
rasterDesc.AntialiasedLineEnable = FALSE;
|
||||
|
||||
if (rasterState.polygonOffsetFill)
|
||||
{
|
||||
rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetFactor;
|
||||
rasterDesc.DepthBias = (INT)rasterState.polygonOffsetUnits;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mRasterizerStateCache.size() >= kMaxRasterizerStates)
|
||||
{
|
||||
WARN() << "Overflowed the limit of " << kMaxRasterizerStates
|
||||
<< " rasterizer states, removing the least recently used to make room.";
|
||||
|
||||
RasterizerStateMap::iterator leastRecentlyUsed = mRasterizerStateCache.begin();
|
||||
for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++)
|
||||
{
|
||||
if (i->second.second < leastRecentlyUsed->second.second)
|
||||
{
|
||||
leastRecentlyUsed = i;
|
||||
}
|
||||
}
|
||||
mRasterizerStateCache.erase(leastRecentlyUsed);
|
||||
}
|
||||
|
||||
D3D11_CULL_MODE cullMode = gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode);
|
||||
|
||||
// Disable culling if drawing points
|
||||
if (rasterState.pointDrawMode)
|
||||
{
|
||||
cullMode = D3D11_CULL_NONE;
|
||||
}
|
||||
|
||||
D3D11_RASTERIZER_DESC rasterDesc;
|
||||
rasterDesc.FillMode = D3D11_FILL_SOLID;
|
||||
rasterDesc.CullMode = cullMode;
|
||||
rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? FALSE: TRUE;
|
||||
rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of zero will preform no clamping, must be tested though.
|
||||
rasterDesc.DepthClipEnable = TRUE;
|
||||
rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE;
|
||||
rasterDesc.MultisampleEnable = rasterState.multiSample;
|
||||
rasterDesc.AntialiasedLineEnable = FALSE;
|
||||
|
||||
if (rasterState.polygonOffsetFill)
|
||||
{
|
||||
rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetFactor;
|
||||
rasterDesc.DepthBias = (INT)rasterState.polygonOffsetUnits;
|
||||
}
|
||||
else
|
||||
{
|
||||
rasterDesc.SlopeScaledDepthBias = 0.0f;
|
||||
rasterDesc.DepthBias = 0;
|
||||
}
|
||||
|
||||
d3d11::RasterizerState dx11RasterizerState;
|
||||
ANGLE_TRY(mRenderer->allocateResource(rasterDesc, &dx11RasterizerState));
|
||||
*outRasterizerState = dx11RasterizerState.get();
|
||||
mRasterizerStateCache.insert(
|
||||
std::make_pair(key, std::make_pair(std::move(dx11RasterizerState), mCounter++)));
|
||||
|
||||
return gl::NoError();
|
||||
rasterDesc.SlopeScaledDepthBias = 0.0f;
|
||||
rasterDesc.DepthBias = 0;
|
||||
}
|
||||
|
||||
d3d11::RasterizerState dx11RasterizerState;
|
||||
ANGLE_TRY(mRenderer->allocateResource(rasterDesc, &dx11RasterizerState));
|
||||
*outRasterizerState = dx11RasterizerState.get();
|
||||
mRasterizerStateCache.Put(key, std::move(dx11RasterizerState));
|
||||
|
||||
return gl::NoError();
|
||||
}
|
||||
|
||||
gl::Error RenderStateCache::getDepthStencilState(const gl::DepthStencilState &glState,
|
||||
ID3D11DepthStencilState **outDSState)
|
||||
{
|
||||
auto keyIter = mDepthStencilStateCache.find(glState);
|
||||
auto keyIter = mDepthStencilStateCache.Get(glState);
|
||||
if (keyIter != mDepthStencilStateCache.end())
|
||||
{
|
||||
DepthStencilStateCounterPair &state = keyIter->second;
|
||||
state.second = mCounter++;
|
||||
*outDSState = state.first.get();
|
||||
*outDSState = keyIter->second.get();
|
||||
return gl::NoError();
|
||||
}
|
||||
|
||||
if (mDepthStencilStateCache.size() >= kMaxDepthStencilStates)
|
||||
{
|
||||
WARN() << "Overflowed the limit of " << kMaxDepthStencilStates
|
||||
<< " depth stencil states, removing the least recently used to make room.";
|
||||
|
||||
auto leastRecentlyUsed = mDepthStencilStateCache.begin();
|
||||
for (auto i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++)
|
||||
{
|
||||
if (i->second.second < leastRecentlyUsed->second.second)
|
||||
{
|
||||
leastRecentlyUsed = i;
|
||||
}
|
||||
}
|
||||
mDepthStencilStateCache.erase(leastRecentlyUsed);
|
||||
}
|
||||
TrimCache(kMaxStates, kGCLimit, "depth stencil", &mDepthStencilStateCache);
|
||||
|
||||
D3D11_DEPTH_STENCIL_DESC dsDesc = {0};
|
||||
dsDesc.DepthEnable = glState.depthTest ? TRUE : FALSE;
|
||||
|
@ -298,77 +252,60 @@ gl::Error RenderStateCache::getDepthStencilState(const gl::DepthStencilState &gl
|
|||
d3d11::DepthStencilState dx11DepthStencilState;
|
||||
ANGLE_TRY(mRenderer->allocateResource(dsDesc, &dx11DepthStencilState));
|
||||
*outDSState = dx11DepthStencilState.get();
|
||||
mDepthStencilStateCache.insert(
|
||||
std::make_pair(glState, std::make_pair(std::move(dx11DepthStencilState), mCounter++)));
|
||||
mDepthStencilStateCache.Put(glState, std::move(dx11DepthStencilState));
|
||||
|
||||
return gl::NoError();
|
||||
}
|
||||
|
||||
gl::Error RenderStateCache::getSamplerState(const gl::SamplerState &samplerState, ID3D11SamplerState **outSamplerState)
|
||||
{
|
||||
SamplerStateMap::iterator keyIter = mSamplerStateCache.find(samplerState);
|
||||
auto keyIter = mSamplerStateCache.Get(samplerState);
|
||||
if (keyIter != mSamplerStateCache.end())
|
||||
{
|
||||
SamplerStateCounterPair &state = keyIter->second;
|
||||
state.second = mCounter++;
|
||||
*outSamplerState = state.first.get();
|
||||
*outSamplerState = keyIter->second.get();
|
||||
return gl::NoError();
|
||||
}
|
||||
else
|
||||
|
||||
TrimCache(kMaxStates, kGCLimit, "sampler stencil", &mSamplerStateCache);
|
||||
|
||||
const auto &featureLevel = mRenderer->getRenderer11DeviceCaps().featureLevel;
|
||||
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
samplerDesc.Filter =
|
||||
gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter,
|
||||
samplerState.maxAnisotropy, samplerState.compareMode);
|
||||
samplerDesc.AddressU = gl_d3d11::ConvertTextureWrap(samplerState.wrapS);
|
||||
samplerDesc.AddressV = gl_d3d11::ConvertTextureWrap(samplerState.wrapT);
|
||||
samplerDesc.AddressW = gl_d3d11::ConvertTextureWrap(samplerState.wrapR);
|
||||
samplerDesc.MipLODBias = 0;
|
||||
samplerDesc.MaxAnisotropy =
|
||||
gl_d3d11::ConvertMaxAnisotropy(samplerState.maxAnisotropy, featureLevel);
|
||||
samplerDesc.ComparisonFunc = gl_d3d11::ConvertComparison(samplerState.compareFunc);
|
||||
samplerDesc.BorderColor[0] = 0.0f;
|
||||
samplerDesc.BorderColor[1] = 0.0f;
|
||||
samplerDesc.BorderColor[2] = 0.0f;
|
||||
samplerDesc.BorderColor[3] = 0.0f;
|
||||
samplerDesc.MinLOD = samplerState.minLod;
|
||||
samplerDesc.MaxLOD = samplerState.maxLod;
|
||||
|
||||
if (mRenderer->getRenderer11DeviceCaps().featureLevel <= D3D_FEATURE_LEVEL_9_3)
|
||||
{
|
||||
if (mSamplerStateCache.size() >= kMaxSamplerStates)
|
||||
{
|
||||
WARN() << "Overflowed the limit of " << kMaxSamplerStates
|
||||
<< " sampler states, removing the least recently used to make room.";
|
||||
// Check that maxLOD is nearly FLT_MAX (1000.0f is the default), since 9_3 doesn't support
|
||||
// anything other than FLT_MAX. Note that Feature Level 9_* only supports GL ES 2.0, so the
|
||||
// consumer of ANGLE can't modify the Max LOD themselves.
|
||||
ASSERT(samplerState.maxLod >= 999.9f);
|
||||
|
||||
SamplerStateMap::iterator leastRecentlyUsed = mSamplerStateCache.begin();
|
||||
for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++)
|
||||
{
|
||||
if (i->second.second < leastRecentlyUsed->second.second)
|
||||
{
|
||||
leastRecentlyUsed = i;
|
||||
}
|
||||
}
|
||||
mSamplerStateCache.erase(leastRecentlyUsed);
|
||||
}
|
||||
|
||||
const auto &featureLevel = mRenderer->getRenderer11DeviceCaps().featureLevel;
|
||||
|
||||
D3D11_SAMPLER_DESC samplerDesc;
|
||||
samplerDesc.Filter = gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter,
|
||||
samplerState.maxAnisotropy, samplerState.compareMode);
|
||||
samplerDesc.AddressU = gl_d3d11::ConvertTextureWrap(samplerState.wrapS);
|
||||
samplerDesc.AddressV = gl_d3d11::ConvertTextureWrap(samplerState.wrapT);
|
||||
samplerDesc.AddressW = gl_d3d11::ConvertTextureWrap(samplerState.wrapR);
|
||||
samplerDesc.MipLODBias = 0;
|
||||
samplerDesc.MaxAnisotropy =
|
||||
gl_d3d11::ConvertMaxAnisotropy(samplerState.maxAnisotropy, featureLevel);
|
||||
samplerDesc.ComparisonFunc = gl_d3d11::ConvertComparison(samplerState.compareFunc);
|
||||
samplerDesc.BorderColor[0] = 0.0f;
|
||||
samplerDesc.BorderColor[1] = 0.0f;
|
||||
samplerDesc.BorderColor[2] = 0.0f;
|
||||
samplerDesc.BorderColor[3] = 0.0f;
|
||||
samplerDesc.MinLOD = samplerState.minLod;
|
||||
samplerDesc.MaxLOD = samplerState.maxLod;
|
||||
|
||||
if (mRenderer->getRenderer11DeviceCaps().featureLevel <= D3D_FEATURE_LEVEL_9_3)
|
||||
{
|
||||
// Check that maxLOD is nearly FLT_MAX (1000.0f is the default), since 9_3 doesn't support anything other than FLT_MAX.
|
||||
// Note that Feature Level 9_* only supports GL ES 2.0, so the consumer of ANGLE can't modify the Max LOD themselves.
|
||||
ASSERT(samplerState.maxLod >= 999.9f);
|
||||
|
||||
// Now just set MaxLOD to FLT_MAX. Other parts of the renderer (e.g. the non-zero max LOD workaround) should take account of this.
|
||||
samplerDesc.MaxLOD = FLT_MAX;
|
||||
}
|
||||
|
||||
d3d11::SamplerState dx11SamplerState;
|
||||
ANGLE_TRY(mRenderer->allocateResource(samplerDesc, &dx11SamplerState));
|
||||
*outSamplerState = dx11SamplerState.get();
|
||||
mSamplerStateCache.insert(
|
||||
std::make_pair(samplerState, std::make_pair(std::move(dx11SamplerState), mCounter++)));
|
||||
|
||||
return gl::NoError();
|
||||
// Now just set MaxLOD to FLT_MAX. Other parts of the renderer (e.g. the non-zero max LOD
|
||||
// workaround) should take account of this.
|
||||
samplerDesc.MaxLOD = FLT_MAX;
|
||||
}
|
||||
|
||||
d3d11::SamplerState dx11SamplerState;
|
||||
ANGLE_TRY(mRenderer->allocateResource(samplerDesc, &dx11SamplerState));
|
||||
*outSamplerState = dx11SamplerState.get();
|
||||
mSamplerStateCache.Put(samplerState, std::move(dx11SamplerState));
|
||||
|
||||
return gl::NoError();
|
||||
}
|
||||
|
||||
} // namespace rx
|
||||
|
|
|
@ -10,10 +10,11 @@
|
|||
#ifndef LIBANGLE_RENDERER_D3D_D3D11_RENDERSTATECACHE_H_
|
||||
#define LIBANGLE_RENDERER_D3D_D3D11_RENDERSTATECACHE_H_
|
||||
|
||||
#include "libANGLE/angletypes.h"
|
||||
#include "libANGLE/Error.h"
|
||||
#include "common/angleutils.h"
|
||||
#include "libANGLE/Error.h"
|
||||
#include "libANGLE/angletypes.h"
|
||||
#include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
|
||||
#include "third_party/mrucache/base/containers/mru_cache.h"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
|
@ -86,36 +87,33 @@ class RenderStateCache : angle::NonCopyable
|
|||
|
||||
private:
|
||||
Renderer11 *mRenderer;
|
||||
unsigned long long mCounter;
|
||||
|
||||
// MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState,
|
||||
// ID3D11Device::CreateDepthStencilState and ID3D11Device::CreateSamplerState claims the maximum
|
||||
// number of unique states of each type an application can create is 4096
|
||||
// TODO(ShahmeerEsmail): Revisit the cache sizes to make sure they are appropriate for most
|
||||
// scenarios.
|
||||
static constexpr unsigned int kMaxStates = 4096;
|
||||
|
||||
// The cache tries to clean up this many states at once.
|
||||
static constexpr unsigned int kGCLimit = 128;
|
||||
|
||||
// Blend state cache
|
||||
static const unsigned int kMaxBlendStates;
|
||||
|
||||
typedef std::pair<d3d11::BlendState, unsigned long long> BlendStateCounterPair;
|
||||
typedef std::unordered_map<d3d11::BlendStateKey, BlendStateCounterPair> BlendStateMap;
|
||||
using BlendStateMap = angle::base::HashingMRUCache<d3d11::BlendStateKey, d3d11::BlendState>;
|
||||
BlendStateMap mBlendStateCache;
|
||||
|
||||
// Rasterizer state cache
|
||||
static const unsigned int kMaxRasterizerStates;
|
||||
|
||||
typedef std::pair<d3d11::RasterizerState, unsigned long long> RasterizerStateCounterPair;
|
||||
typedef std::unordered_map<d3d11::RasterizerStateKey, RasterizerStateCounterPair>
|
||||
RasterizerStateMap;
|
||||
using RasterizerStateMap =
|
||||
angle::base::HashingMRUCache<d3d11::RasterizerStateKey, d3d11::RasterizerState>;
|
||||
RasterizerStateMap mRasterizerStateCache;
|
||||
|
||||
// Depth stencil state cache
|
||||
static const unsigned int kMaxDepthStencilStates;
|
||||
|
||||
typedef std::pair<d3d11::DepthStencilState, unsigned long long> DepthStencilStateCounterPair;
|
||||
typedef std::unordered_map<gl::DepthStencilState, DepthStencilStateCounterPair>
|
||||
DepthStencilStateMap;
|
||||
using DepthStencilStateMap =
|
||||
angle::base::HashingMRUCache<gl::DepthStencilState, d3d11::DepthStencilState>;
|
||||
DepthStencilStateMap mDepthStencilStateCache;
|
||||
|
||||
// Sample state cache
|
||||
static const unsigned int kMaxSamplerStates;
|
||||
|
||||
typedef std::pair<d3d11::SamplerState, unsigned long long> SamplerStateCounterPair;
|
||||
typedef std::unordered_map<gl::SamplerState, SamplerStateCounterPair> SamplerStateMap;
|
||||
using SamplerStateMap = angle::base::HashingMRUCache<gl::SamplerState, d3d11::SamplerState>;
|
||||
SamplerStateMap mSamplerStateCache;
|
||||
};
|
||||
|
||||
|
|
|
@ -265,6 +265,9 @@
|
|||
'third_party/murmurhash/MurmurHash3.cpp',
|
||||
'third_party/murmurhash/MurmurHash3.h',
|
||||
'third_party/trace_event/trace_event.h',
|
||||
'third_party/mrucache/base/containers/mru_cache.h',
|
||||
'third_party/mrucache/base/logging.h',
|
||||
'third_party/mrucache/base/macros.h',
|
||||
],
|
||||
'libangle_d3d_shared_sources':
|
||||
[
|
||||
|
@ -891,6 +894,7 @@
|
|||
'.',
|
||||
'../include',
|
||||
'third_party/khronos',
|
||||
'third_party/mrucache',
|
||||
],
|
||||
'sources':
|
||||
[
|
||||
|
@ -913,6 +917,7 @@
|
|||
[
|
||||
'<(angle_path)/src',
|
||||
'<(angle_path)/include',
|
||||
'<(angle_path)/src/third_party/mrucache',
|
||||
],
|
||||
'defines':
|
||||
[
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
Name: Chromium: base/containers/MRUCache
|
||||
Short Name: base::MRUCache
|
||||
Version:
|
||||
URL: https://chromium.googlesource.com/chromium/src/base/+/master/containers/mru_cache.h
|
||||
SOURCE CODE: Copy the Chromium folder manually into this folder and run git cl format.
|
||||
Date: 24/05/2017
|
||||
Revision: d20ef132b529ecce1032005476f936e05cf708c0 (Chromium)
|
||||
Security Critical: no
|
||||
License: Chromium
|
||||
License File: LICENSE in Chromium/src
|
||||
|
||||
Description:
|
||||
base::MRUCache is a few collections of most-recently-used caching structures.
|
||||
To update the checkout, simply overwrite the folder with Chromium's latest, and apply
|
||||
the appropriate namespace.
|
||||
|
||||
Modifications:
|
||||
|
||||
- base/logging.h defines CHECK to be ASSERT to be compatible with ANGLE.
|
||||
- base/numerics/*.h uses namespace angle::base instead of base:: to avoid ODR
|
||||
violations when ANGLE code is mixed with Chromium code.
|
|
@ -0,0 +1,275 @@
|
|||
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// This file contains a template for a Most Recently Used cache that allows
|
||||
// constant-time access to items using a key, but easy identification of the
|
||||
// least-recently-used items for removal. Each key can only be associated with
|
||||
// one payload item at a time.
|
||||
//
|
||||
// The key object will be stored twice, so it should support efficient copying.
|
||||
//
|
||||
// NOTE: While all operations are O(1), this code is written for
|
||||
// legibility rather than optimality. If future profiling identifies this as
|
||||
// a bottleneck, there is room for smaller values of 1 in the O(1). :]
|
||||
|
||||
#ifndef BASE_CONTAINERS_MRU_CACHE_H_
|
||||
#define BASE_CONTAINERS_MRU_CACHE_H_
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "base/macros.h"
|
||||
|
||||
namespace angle
|
||||
{
|
||||
|
||||
namespace base
|
||||
{
|
||||
|
||||
// MRUCacheBase ----------------------------------------------------------------
|
||||
|
||||
// This template is used to standardize map type containers that can be used
|
||||
// by MRUCacheBase. This level of indirection is necessary because of the way
|
||||
// that template template params and default template params interact.
|
||||
template <class KeyType, class ValueType, class CompareType>
|
||||
struct MRUCacheStandardMap
|
||||
{
|
||||
typedef std::map<KeyType, ValueType, CompareType> Type;
|
||||
};
|
||||
|
||||
// Base class for the MRU cache specializations defined below.
|
||||
template <class KeyType,
|
||||
class PayloadType,
|
||||
class HashOrCompareType,
|
||||
template <typename, typename, typename> class MapType = MRUCacheStandardMap>
|
||||
class MRUCacheBase
|
||||
{
|
||||
public:
|
||||
// The payload of the list. This maintains a copy of the key so we can
|
||||
// efficiently delete things given an element of the list.
|
||||
typedef std::pair<KeyType, PayloadType> value_type;
|
||||
|
||||
private:
|
||||
typedef std::list<value_type> PayloadList;
|
||||
typedef
|
||||
typename MapType<KeyType, typename PayloadList::iterator, HashOrCompareType>::Type KeyIndex;
|
||||
|
||||
public:
|
||||
typedef typename PayloadList::size_type size_type;
|
||||
|
||||
typedef typename PayloadList::iterator iterator;
|
||||
typedef typename PayloadList::const_iterator const_iterator;
|
||||
typedef typename PayloadList::reverse_iterator reverse_iterator;
|
||||
typedef typename PayloadList::const_reverse_iterator const_reverse_iterator;
|
||||
|
||||
enum
|
||||
{
|
||||
NO_AUTO_EVICT = 0
|
||||
};
|
||||
|
||||
// The max_size is the size at which the cache will prune its members to when
|
||||
// a new item is inserted. If the caller wants to manager this itself (for
|
||||
// example, maybe it has special work to do when something is evicted), it
|
||||
// can pass NO_AUTO_EVICT to not restrict the cache size.
|
||||
explicit MRUCacheBase(size_type max_size) : max_size_(max_size) {}
|
||||
|
||||
virtual ~MRUCacheBase() {}
|
||||
|
||||
size_type max_size() const { return max_size_; }
|
||||
|
||||
// Inserts a payload item with the given key. If an existing item has
|
||||
// the same key, it is removed prior to insertion. An iterator indicating the
|
||||
// inserted item will be returned (this will always be the front of the list).
|
||||
//
|
||||
// The payload will be forwarded.
|
||||
template <typename Payload>
|
||||
iterator Put(const KeyType &key, Payload &&payload)
|
||||
{
|
||||
// Remove any existing payload with that key.
|
||||
typename KeyIndex::iterator index_iter = index_.find(key);
|
||||
if (index_iter != index_.end())
|
||||
{
|
||||
// Erase the reference to it. The index reference will be replaced in the
|
||||
// code below.
|
||||
Erase(index_iter->second);
|
||||
}
|
||||
else if (max_size_ != NO_AUTO_EVICT)
|
||||
{
|
||||
// New item is being inserted which might make it larger than the maximum
|
||||
// size: kick the oldest thing out if necessary.
|
||||
ShrinkToSize(max_size_ - 1);
|
||||
}
|
||||
|
||||
ordering_.emplace_front(key, std::forward<Payload>(payload));
|
||||
index_.emplace(key, ordering_.begin());
|
||||
return ordering_.begin();
|
||||
}
|
||||
|
||||
// Retrieves the contents of the given key, or end() if not found. This method
|
||||
// has the side effect of moving the requested item to the front of the
|
||||
// recency list.
|
||||
iterator Get(const KeyType &key)
|
||||
{
|
||||
typename KeyIndex::iterator index_iter = index_.find(key);
|
||||
if (index_iter == index_.end())
|
||||
return end();
|
||||
typename PayloadList::iterator iter = index_iter->second;
|
||||
|
||||
// Move the touched item to the front of the recency ordering.
|
||||
ordering_.splice(ordering_.begin(), ordering_, iter);
|
||||
return ordering_.begin();
|
||||
}
|
||||
|
||||
// Retrieves the payload associated with a given key and returns it via
|
||||
// result without affecting the ordering (unlike Get).
|
||||
iterator Peek(const KeyType &key)
|
||||
{
|
||||
typename KeyIndex::const_iterator index_iter = index_.find(key);
|
||||
if (index_iter == index_.end())
|
||||
return end();
|
||||
return index_iter->second;
|
||||
}
|
||||
|
||||
const_iterator Peek(const KeyType &key) const
|
||||
{
|
||||
typename KeyIndex::const_iterator index_iter = index_.find(key);
|
||||
if (index_iter == index_.end())
|
||||
return end();
|
||||
return index_iter->second;
|
||||
}
|
||||
|
||||
// Exchanges the contents of |this| by the contents of the |other|.
|
||||
void Swap(MRUCacheBase &other)
|
||||
{
|
||||
ordering_.swap(other.ordering_);
|
||||
index_.swap(other.index_);
|
||||
std::swap(max_size_, other.max_size_);
|
||||
}
|
||||
|
||||
// Erases the item referenced by the given iterator. An iterator to the item
|
||||
// following it will be returned. The iterator must be valid.
|
||||
iterator Erase(iterator pos)
|
||||
{
|
||||
index_.erase(pos->first);
|
||||
return ordering_.erase(pos);
|
||||
}
|
||||
|
||||
// MRUCache entries are often processed in reverse order, so we add this
|
||||
// convenience function (not typically defined by STL containers).
|
||||
reverse_iterator Erase(reverse_iterator pos)
|
||||
{
|
||||
// We have to actually give it the incremented iterator to delete, since
|
||||
// the forward iterator that base() returns is actually one past the item
|
||||
// being iterated over.
|
||||
return reverse_iterator(Erase((++pos).base()));
|
||||
}
|
||||
|
||||
// Shrinks the cache so it only holds |new_size| items. If |new_size| is
|
||||
// bigger or equal to the current number of items, this will do nothing.
|
||||
void ShrinkToSize(size_type new_size)
|
||||
{
|
||||
for (size_type i = size(); i > new_size; i--)
|
||||
Erase(rbegin());
|
||||
}
|
||||
|
||||
// Deletes everything from the cache.
|
||||
void Clear()
|
||||
{
|
||||
index_.clear();
|
||||
ordering_.clear();
|
||||
}
|
||||
|
||||
// Returns the number of elements in the cache.
|
||||
size_type size() const
|
||||
{
|
||||
// We don't use ordering_.size() for the return value because
|
||||
// (as a linked list) it can be O(n).
|
||||
DCHECK(index_.size() == ordering_.size());
|
||||
return index_.size();
|
||||
}
|
||||
|
||||
// Allows iteration over the list. Forward iteration starts with the most
|
||||
// recent item and works backwards.
|
||||
//
|
||||
// Note that since these iterators are actually iterators over a list, you
|
||||
// can keep them as you insert or delete things (as long as you don't delete
|
||||
// the one you are pointing to) and they will still be valid.
|
||||
iterator begin() { return ordering_.begin(); }
|
||||
const_iterator begin() const { return ordering_.begin(); }
|
||||
iterator end() { return ordering_.end(); }
|
||||
const_iterator end() const { return ordering_.end(); }
|
||||
|
||||
reverse_iterator rbegin() { return ordering_.rbegin(); }
|
||||
const_reverse_iterator rbegin() const { return ordering_.rbegin(); }
|
||||
reverse_iterator rend() { return ordering_.rend(); }
|
||||
const_reverse_iterator rend() const { return ordering_.rend(); }
|
||||
|
||||
bool empty() const { return ordering_.empty(); }
|
||||
|
||||
private:
|
||||
PayloadList ordering_;
|
||||
KeyIndex index_;
|
||||
|
||||
size_type max_size_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(MRUCacheBase);
|
||||
};
|
||||
|
||||
// MRUCache --------------------------------------------------------------------
|
||||
|
||||
// A container that does not do anything to free its data. Use this when storing
|
||||
// value types (as opposed to pointers) in the list.
|
||||
template <class KeyType, class PayloadType, class CompareType = std::less<KeyType>>
|
||||
class MRUCache : public MRUCacheBase<KeyType, PayloadType, CompareType>
|
||||
{
|
||||
private:
|
||||
using ParentType = MRUCacheBase<KeyType, PayloadType, CompareType>;
|
||||
|
||||
public:
|
||||
// See MRUCacheBase, noting the possibility of using NO_AUTO_EVICT.
|
||||
explicit MRUCache(typename ParentType::size_type max_size) : ParentType(max_size) {}
|
||||
virtual ~MRUCache() {}
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(MRUCache);
|
||||
};
|
||||
|
||||
// HashingMRUCache ------------------------------------------------------------
|
||||
|
||||
template <class KeyType, class ValueType, class HashType>
|
||||
struct MRUCacheHashMap
|
||||
{
|
||||
typedef std::unordered_map<KeyType, ValueType, HashType> Type;
|
||||
};
|
||||
|
||||
// This class is similar to MRUCache, except that it uses std::unordered_map as
|
||||
// the map type instead of std::map. Note that your KeyType must be hashable to
|
||||
// use this cache or you need to provide a hashing class.
|
||||
template <class KeyType, class PayloadType, class HashType = std::hash<KeyType>>
|
||||
class HashingMRUCache : public MRUCacheBase<KeyType, PayloadType, HashType, MRUCacheHashMap>
|
||||
{
|
||||
private:
|
||||
using ParentType = MRUCacheBase<KeyType, PayloadType, HashType, MRUCacheHashMap>;
|
||||
|
||||
public:
|
||||
// See MRUCacheBase, noting the possibility of using NO_AUTO_EVICT.
|
||||
explicit HashingMRUCache(typename ParentType::size_type max_size) : ParentType(max_size) {}
|
||||
virtual ~HashingMRUCache() {}
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(HashingMRUCache);
|
||||
};
|
||||
|
||||
} // namespace base
|
||||
|
||||
} // namespace angle
|
||||
|
||||
#endif // BASE_CONTAINERS_MRU_CACHE_H_
|
|
@ -0,0 +1,26 @@
|
|||
//
|
||||
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
//
|
||||
// logging.h: Compatiblity hacks for importing Chromium's MRUCache.
|
||||
|
||||
#ifndef BASE_LOGGING_H_
|
||||
#define BASE_LOGGING_H_
|
||||
|
||||
#include "common/debug.h"
|
||||
|
||||
#ifndef DCHECK
|
||||
#define DCHECK(X) ASSERT(X)
|
||||
#endif
|
||||
|
||||
#ifndef CHECK
|
||||
#define CHECK(X) ASSERT(X)
|
||||
#endif
|
||||
|
||||
// Unfortunately ANGLE relies on ASSERT being an empty statement, which these libs don't respect.
|
||||
#ifndef NOTREACHED
|
||||
#define NOTREACHED() UNREACHABLE()
|
||||
#endif
|
||||
|
||||
#endif // BASE_LOGGING_H_
|
|
@ -0,0 +1,17 @@
|
|||
//
|
||||
// Copyright 2017 The ANGLE Project Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
//
|
||||
// macros.h: Compatiblity hacks for importing Chromium's MRUCache.
|
||||
|
||||
#ifndef BASE_MACROS_H_
|
||||
#define BASE_MACROS_H_
|
||||
|
||||
// A macro to disallow the copy constructor and operator= functions.
|
||||
// This should be used in the private: declarations for a class.
|
||||
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
||||
TypeName(const TypeName &) = delete; \
|
||||
void operator=(const TypeName &) = delete
|
||||
|
||||
#endif // BASE_MACROS_H_
|
Загрузка…
Ссылка в новой задаче