Added a RenderStateCache class for caching D3D11 blend, depth stencil and rasterizer states.

TRAC #22042 Signed-off-by: Nicolas Capens Signed-off-by: Daniel Koch Author: Geoff Lang git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1432 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent b58cb9b6
......@@ -274,6 +274,8 @@
'libGLESv2/renderer/SwapChain.h',
'libGLESv2/renderer/TextureStorage.cpp',
'libGLESv2/renderer/TextureStorage.h',
'libGLESv2/renderer/RenderStateCache.cpp',
'libGLESv2/renderer/RenderStateCache.h',
'libGLESv2/ResourceManager.cpp',
'libGLESv2/ResourceManager.h',
'libGLESv2/Shader.cpp',
......
......@@ -251,8 +251,9 @@ copy "$(OutDir)libGLESv2.lib" "$(ProjectDir)..\..\lib\$(Configuration)\"
<ClCompile Include="renderer\renderer11_utils.cpp" />
<ClCompile Include="renderer\Renderer9.cpp" />
<ClCompile Include="renderer\Image.cpp" />
<ClCompile Include="renderer\renderer9_utils.cpp" />
<ClCompile Include="renderer\RenderTarget9.cpp" />
<ClCompile Include="renderer\renderer9_utils.cpp" />
<ClCompile Include="renderer\RenderTarget9.cpp" />
<ClCompile Include="renderer\RenderStateCache.cpp" />
<ClCompile Include="renderer\SwapChain.cpp" />
<ClCompile Include="renderer\TextureStorage.cpp" />
<ClCompile Include="ResourceManager.cpp" />
......@@ -289,9 +290,10 @@ copy "$(OutDir)libGLESv2.lib" "$(ProjectDir)..\..\lib\$(Configuration)\"
<ClInclude Include="renderer\Renderer11.h" />
<ClInclude Include="renderer\renderer11_utils.h" />
<ClInclude Include="renderer\Renderer9.h" />
<ClInclude Include="renderer\renderer9_utils.h" />
<ClInclude Include="renderer\renderer9_utils.h" />
<ClInclude Include="renderer\RenderTarget.h" />
<ClInclude Include="renderer\RenderTarget9.h" />
<ClInclude Include="renderer\RenderTarget9.h" />
<ClInclude Include="renderer\RenderStateCache.h" />
<ClInclude Include="renderer\ShaderCache.h" />
<ClInclude Include="renderer\SwapChain.h" />
<ClInclude Include="renderer\TextureStorage.h" />
......
......@@ -111,11 +111,14 @@
<Filter>Renderer</Filter>
</ClCompile>
<ClCompile Include="renderer\Blit.cpp">
<Filter>Renderer</Filter>
<Filter>Renderer</Filter>
</ClCompile>
<ClCompile Include="renderer\RenderTarget9.cpp">
<Filter>Renderer</Filter>
</ClCompile>
<ClCompile Include="renderer\RenderStateCache.cpp">
<Filter>Renderer</Filter>
</ClInclude>
</ClCompile>
<ClCompile Include="..\third_party\murmurhash\MurmurHash3.cpp">
<Filter>Third Party\MurmurHash</Filter>
</ClCompile>
......@@ -234,10 +237,13 @@
</ClInclude>
<ClInclude Include="renderer\RenderTarget.h">
<Filter>Renderer</Filter>
</ClInclude>
</ClInclude>
<ClInclude Include="renderer\RenderTarget9.h">
<Filter>Renderer</Filter>
</ClInclude>
<ClInclude Include="renderer\RenderStateCache.h">
<Filter>Renderer</Filter>
</ClInclude>
<ClInclude Include="..\third_party\murmurhash\MurmurHash3.h">
<Filter>Third Party\MurmurHash</Filter>
</ClInclude>
......
//
// Copyright (c) 2012 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.
//
// RenderStateCache.cpp: Defines rx::RenderStateCache, a cache of Direct3D render
// state objects.
#include "libGLESv2/renderer/RenderStateCache.h"
#include "libGLESv2/renderer/renderer11_utils.h"
#include "common/debug.h"
#include "third_party/murmurhash/MurmurHash3.h"
namespace rx
{
RenderStateCache::RenderStateCache() : mDevice(NULL), mCounter(0),
mBlendStateCache(kMaxBlendStates, hashBlendState, compareBlendStates)
{
}
RenderStateCache::~RenderStateCache()
{
clear();
}
void RenderStateCache::initialize(ID3D11Device* device)
{
clear();
mDevice = device;
}
void RenderStateCache::clear()
{
for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++)
{
i->second.first->Release();
}
mBlendStateCache.clear();
}
std::size_t RenderStateCache::hashBlendState(const gl::BlendState &blendState)
{
static const unsigned int seed = 0xABCDEF98;
std::size_t hash = 0;
MurmurHash3_x86_32(&blendState, sizeof(gl::BlendState), seed, &hash);
return hash;
}
bool RenderStateCache::compareBlendStates(const gl::BlendState &a, const gl::BlendState &b)
{
return memcmp(&a, &b, sizeof(gl::BlendState)) == 0;
}
// MSDN's documentation of ID3D11Device::CreateBlendState claims the maximum number of
// unique blend states an application can create is 4096
const unsigned int RenderStateCache::kMaxBlendStates = 4096;
ID3D11BlendState *RenderStateCache::getBlendState(const gl::BlendState &blendState)
{
if (!mDevice)
{
ERR("RenderStateCache is not initialized.");
return NULL;
}
BlendStateMap::iterator i = mBlendStateCache.find(blendState);
if (i != mBlendStateCache.end())
{
BlendStateCounterPair &state = i->second;
state.first->AddRef();
state.second = mCounter++;
return state.first;
}
else
{
if (mBlendStateCache.size() >= kMaxBlendStates)
{
TRACE("Overflowed the limit of %u blend states, removing the least recently used "
"to make room.", kMaxBlendStates);
BlendStateMap::iterator leastRecentlyUsed = mBlendStateCache.begin();
for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++)
{
if (i->second.second < leastRecentlyUsed->second.second)
{
leastRecentlyUsed = i;
}
}
leastRecentlyUsed->second.first->Release();
mBlendStateCache.erase(leastRecentlyUsed);
}
// Create a new blend state and insert it into the cache
D3D11_BLEND_DESC blendDesc = { 0 };
blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage;
blendDesc.IndependentBlendEnable = FALSE;
for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
{
D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = blendDesc.RenderTarget[i];
rtBlend.BlendEnable = blendState.blend;
if (blendState.blend)
{
rtBlend.SrcBlend = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendRGB);
rtBlend.DestBlend = gl_d3d11::ConvertBlendFunc(blendState.destBlendRGB);
rtBlend.BlendOp = gl_d3d11::ConvertBlendOp(blendState.blendEquationRGB);
rtBlend.SrcBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendAlpha);
rtBlend.DestBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.destBlendAlpha);
rtBlend.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha);
rtBlend.RenderTargetWriteMask = gl_d3d11::ConvertColorMask(blendState.colorMaskRed,
blendState.colorMaskGreen,
blendState.colorMaskBlue,
blendState.colorMaskAlpha);
}
}
ID3D11BlendState* dx11BlendState = NULL;
HRESULT result = mDevice->CreateBlendState(&blendDesc, &dx11BlendState);
if (FAILED(result) || !dx11BlendState)
{
ERR("Unable to create a ID3D11BlendState, HRESULT: 0x%X.", result);
return NULL;
}
mBlendStateCache.insert(std::make_pair(blendState, std::make_pair(dx11BlendState, mCounter++)));
dx11BlendState->AddRef();
return dx11BlendState;
}
}
}
\ No newline at end of file
//
// Copyright (c) 2012 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.
//
// RenderStateCache.h: Defines rx::RenderStateCache, a cache of Direct3D render
// state objects.
#ifndef LIBGLESV2_RENDERER_RENDERSTATECACHE_H_
#define LIBGLESV2_RENDERER_RENDERSTATECACHE_H_
#include "libGLESv2/angletypes.h"
#include "common/angleutils.h"
#include <D3D11.h>
#include <unordered_map>
namespace rx
{
class RenderStateCache
{
public:
RenderStateCache();
virtual ~RenderStateCache();
void initialize(ID3D11Device* device);
void clear();
// Increments refcount on the returned blend state, Release() must be called.
ID3D11BlendState *getBlendState(const gl::BlendState &blendState);
private:
DISALLOW_COPY_AND_ASSIGN(RenderStateCache);
unsigned long long mCounter;
// Blend state cache
static std::size_t hashBlendState(const gl::BlendState &blendState);
static bool compareBlendStates(const gl::BlendState &a, const gl::BlendState &b);
static const unsigned int kMaxBlendStates;
typedef std::size_t (*BlendStateHashFunction)(const gl::BlendState &);
typedef bool (*BlendStateEqualityFunction)(const gl::BlendState &, const gl::BlendState &);
typedef std::pair<ID3D11BlendState*, unsigned long long> BlendStateCounterPair;
typedef std::unordered_map<gl::BlendState, BlendStateCounterPair, BlendStateHashFunction, BlendStateEqualityFunction> BlendStateMap;
BlendStateMap mBlendStateCache;
ID3D11Device* mDevice;
};
}
#endif // LIBGLESV2_RENDERER_RENDERSTATECACHE_H_
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment