Hash :
d55e8c1b
Author :
Date :
2012-11-28T21:07:02
Scissor test state is set as part of the Renderer::setScissor method. TRAC #22206 Moved scissorTest out of RasterizerState. Fixes buffer-offscreen-test and buffer-preserve-test CTS regressions. Signed-off-by: Nicolas Capens Signed-off-by: Daniel Koch git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1549 736b8ea6-26fd-11df-bfd4-992fa37f6226
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
//
// 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
{
// MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState
// and ID3D11Device::CreateDepthStencilState claims the maximum number of unique states of each
// type an application can create is 4096
const unsigned int RenderStateCache::kMaxBlendStates = 4096;
const unsigned int RenderStateCache::kMaxRasterizerStates = 4096;
const unsigned int RenderStateCache::kMaxDepthStencilStates = 4096;
RenderStateCache::RenderStateCache() : mDevice(NULL), mCounter(0),
mBlendStateCache(kMaxBlendStates, hashBlendState, compareBlendStates),
mRasterizerStateCache(kMaxRasterizerStates, hashRasterizerState, compareRasterizerStates),
mDepthStencilStateCache(kMaxDepthStencilStates, hashDepthStencilState, compareDepthStencilStates)
{
}
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();
for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++)
{
i->second.first->Release();
}
mRasterizerStateCache.clear();
for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++)
{
i->second.first->Release();
}
mDepthStencilStateCache.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;
}
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;
}
}
std::size_t RenderStateCache::hashRasterizerState(const RasterizerStateKey &rasterState)
{
static const unsigned int seed = 0xABCDEF98;
std::size_t hash = 0;
MurmurHash3_x86_32(&rasterState, sizeof(RasterizerStateKey), seed, &hash);
return hash;
}
bool RenderStateCache::compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b)
{
return memcmp(&a, &b, sizeof(RasterizerStateKey)) == 0;
}
ID3D11RasterizerState *RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState,
bool scissorEnabled, unsigned int depthSize)
{
if (!mDevice)
{
ERR("RenderStateCache is not initialized.");
return NULL;
}
RasterizerStateKey key;
key.rasterizerState = rasterState;
key.scissorEnabled = scissorEnabled;
key.depthSize = depthSize;
RasterizerStateMap::iterator i = mRasterizerStateCache.find(key);
if (i != mRasterizerStateCache.end())
{
RasterizerStateCounterPair &state = i->second;
state.first->AddRef();
state.second = mCounter++;
return state.first;
}
else
{
if (mRasterizerStateCache.size() >= kMaxRasterizerStates)
{
TRACE("Overflowed the limit of %u rasterizer states, removing the least recently used "
"to make room.", kMaxRasterizerStates);
RasterizerStateMap::iterator leastRecentlyUsed = mRasterizerStateCache.begin();
for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++)
{
if (i->second.second < leastRecentlyUsed->second.second)
{
leastRecentlyUsed = i;
}
}
leastRecentlyUsed->second.first->Release();
mRasterizerStateCache.erase(leastRecentlyUsed);
}
D3D11_RASTERIZER_DESC rasterDesc;
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.CullMode = gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode);
rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? TRUE : FALSE;
rasterDesc.DepthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize));
rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of zero will preform no clamping, must be tested though.
rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetUnits;
rasterDesc.DepthClipEnable = TRUE;
rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE;
rasterDesc.MultisampleEnable = TRUE;
rasterDesc.AntialiasedLineEnable = FALSE;
ID3D11RasterizerState* dx11RasterizerState = NULL;
HRESULT result = mDevice->CreateRasterizerState(&rasterDesc, &dx11RasterizerState);
if (FAILED(result) || !dx11RasterizerState)
{
ERR("Unable to create a ID3D11RasterizerState, HRESULT: 0x%X.", result);
return NULL;
}
mRasterizerStateCache.insert(std::make_pair(key, std::make_pair(dx11RasterizerState, mCounter++)));
dx11RasterizerState->AddRef();
return dx11RasterizerState;
}
}
std::size_t RenderStateCache::hashDepthStencilState(const gl::DepthStencilState &dsState)
{
static const unsigned int seed = 0xABCDEF98;
std::size_t hash = 0;
MurmurHash3_x86_32(&dsState, sizeof(gl::DepthStencilState), seed, &hash);
return hash;
}
bool RenderStateCache::compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b)
{
return memcmp(&a, &b, sizeof(gl::DepthStencilState)) == 0;
}
ID3D11DepthStencilState* RenderStateCache::getDepthStencilState(const gl::DepthStencilState &dsState)
{
if (!mDevice)
{
ERR("RenderStateCache is not initialized.");
return NULL;
}
DepthStencilStateMap::iterator i = mDepthStencilStateCache.find(dsState);
if (i != mDepthStencilStateCache.end())
{
DepthStencilStateCounterPair &state = i->second;
state.first->AddRef();
state.second = mCounter++;
return state.first;
}
else
{
if (mDepthStencilStateCache.size() >= kMaxDepthStencilStates)
{
TRACE("Overflowed the limit of %u depth stencil states, removing the least recently used "
"to make room.", kMaxDepthStencilStates);
DepthStencilStateMap::iterator leastRecentlyUsed = mDepthStencilStateCache.begin();
for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++)
{
if (i->second.second < leastRecentlyUsed->second.second)
{
leastRecentlyUsed = i;
}
}
leastRecentlyUsed->second.first->Release();
mDepthStencilStateCache.erase(leastRecentlyUsed);
}
D3D11_DEPTH_STENCIL_DESC dsDesc = { 0 };
dsDesc.DepthEnable = dsState.depthTest ? TRUE : FALSE;
dsDesc.DepthWriteMask = gl_d3d11::ConvertDepthMask(dsState.depthMask);
dsDesc.DepthFunc = gl_d3d11::ConvertComparison(dsState.depthFunc);
dsDesc.StencilEnable = dsState.stencilTest ? TRUE : FALSE;
dsDesc.StencilReadMask = gl_d3d11::ConvertStencilMask(dsState.stencilMask);
dsDesc.StencilWriteMask = gl_d3d11::ConvertStencilMask(dsState.stencilWritemask);
dsDesc.FrontFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilFail);
dsDesc.FrontFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthFail);
dsDesc.FrontFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthPass);
dsDesc.FrontFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilFunc);
dsDesc.BackFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackFail);
dsDesc.BackFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthFail);
dsDesc.BackFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthPass);
dsDesc.BackFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilBackFunc);
ID3D11DepthStencilState* dx11DepthStencilState = NULL;
HRESULT result = mDevice->CreateDepthStencilState(&dsDesc, &dx11DepthStencilState);
if (FAILED(result) || !dx11DepthStencilState)
{
ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
return NULL;
}
mDepthStencilStateCache.insert(std::make_pair(dsState, std::make_pair(dx11DepthStencilState, mCounter++)));
dx11DepthStencilState->AddRef();
return dx11DepthStencilState;
}
}
}