blob: 3fa0e72ad737954b630870eb8d69bbd9104e37fe [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.com0673d792012-11-28 19:37:44 +00002//
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +00003// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com0673d792012-11-28 19:37:44 +00004// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// RenderStateCache.cpp: Defines rx::RenderStateCache, a cache of Direct3D render
9// state objects.
10
11#include "libGLESv2/renderer/RenderStateCache.h"
12#include "libGLESv2/renderer/renderer11_utils.h"
13
14#include "common/debug.h"
15#include "third_party/murmurhash/MurmurHash3.h"
16
17namespace rx
18{
19
Geoff Langea228632013-07-30 15:17:12 -040020template <typename mapType>
21static void ClearStateMap(mapType &map)
22{
23 for (mapType::iterator i = map.begin(); i != map.end(); i++)
24 {
25 SafeRelease(i->second.first);
26 }
27 map.clear();
28}
29
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +000030// MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState,
31// ID3D11Device::CreateDepthStencilState and ID3D11Device::CreateSamplerState claims the maximum
32// number of unique states of each type an application can create is 4096
daniel@transgaming.comc497eba2012-11-28 19:39:06 +000033const unsigned int RenderStateCache::kMaxBlendStates = 4096;
34const unsigned int RenderStateCache::kMaxRasterizerStates = 4096;
35const unsigned int RenderStateCache::kMaxDepthStencilStates = 4096;
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +000036const unsigned int RenderStateCache::kMaxSamplerStates = 4096;
daniel@transgaming.comc497eba2012-11-28 19:39:06 +000037
daniel@transgaming.com0673d792012-11-28 19:37:44 +000038RenderStateCache::RenderStateCache() : mDevice(NULL), mCounter(0),
daniel@transgaming.comed453e02012-11-28 19:38:11 +000039 mBlendStateCache(kMaxBlendStates, hashBlendState, compareBlendStates),
daniel@transgaming.com53926ff2012-11-28 19:38:50 +000040 mRasterizerStateCache(kMaxRasterizerStates, hashRasterizerState, compareRasterizerStates),
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +000041 mDepthStencilStateCache(kMaxDepthStencilStates, hashDepthStencilState, compareDepthStencilStates),
42 mSamplerStateCache(kMaxSamplerStates, hashSamplerState, compareSamplerStates)
daniel@transgaming.com0673d792012-11-28 19:37:44 +000043{
44}
45
46RenderStateCache::~RenderStateCache()
47{
48 clear();
49}
50
daniel@transgaming.com3cf86502013-01-11 04:08:40 +000051void RenderStateCache::initialize(ID3D11Device *device)
daniel@transgaming.com0673d792012-11-28 19:37:44 +000052{
53 clear();
54 mDevice = device;
55}
56
57void RenderStateCache::clear()
58{
Geoff Langea228632013-07-30 15:17:12 -040059 ClearStateMap(mBlendStateCache);
60 ClearStateMap(mRasterizerStateCache);
61 ClearStateMap(mDepthStencilStateCache);
62 ClearStateMap(mSamplerStateCache);
daniel@transgaming.com0673d792012-11-28 19:37:44 +000063}
64
65std::size_t RenderStateCache::hashBlendState(const gl::BlendState &blendState)
66{
67 static const unsigned int seed = 0xABCDEF98;
68
69 std::size_t hash = 0;
70 MurmurHash3_x86_32(&blendState, sizeof(gl::BlendState), seed, &hash);
71 return hash;
72}
73
74bool RenderStateCache::compareBlendStates(const gl::BlendState &a, const gl::BlendState &b)
75{
76 return memcmp(&a, &b, sizeof(gl::BlendState)) == 0;
77}
78
daniel@transgaming.com0673d792012-11-28 19:37:44 +000079ID3D11BlendState *RenderStateCache::getBlendState(const gl::BlendState &blendState)
80{
81 if (!mDevice)
82 {
83 ERR("RenderStateCache is not initialized.");
84 return NULL;
85 }
86
87 BlendStateMap::iterator i = mBlendStateCache.find(blendState);
88 if (i != mBlendStateCache.end())
89 {
90 BlendStateCounterPair &state = i->second;
daniel@transgaming.com0673d792012-11-28 19:37:44 +000091 state.second = mCounter++;
92 return state.first;
93 }
94 else
95 {
96 if (mBlendStateCache.size() >= kMaxBlendStates)
97 {
98 TRACE("Overflowed the limit of %u blend states, removing the least recently used "
99 "to make room.", kMaxBlendStates);
100
101 BlendStateMap::iterator leastRecentlyUsed = mBlendStateCache.begin();
102 for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++)
103 {
104 if (i->second.second < leastRecentlyUsed->second.second)
105 {
106 leastRecentlyUsed = i;
107 }
108 }
Geoff Langea228632013-07-30 15:17:12 -0400109 SafeRelease(leastRecentlyUsed->second.first);
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000110 mBlendStateCache.erase(leastRecentlyUsed);
111 }
112
113 // Create a new blend state and insert it into the cache
114 D3D11_BLEND_DESC blendDesc = { 0 };
115 blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage;
116 blendDesc.IndependentBlendEnable = FALSE;
117
118 for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
119 {
120 D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = blendDesc.RenderTarget[i];
121
122 rtBlend.BlendEnable = blendState.blend;
123 if (blendState.blend)
124 {
daniel@transgaming.com90c634a2013-01-11 04:10:01 +0000125 rtBlend.SrcBlend = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendRGB, false);
126 rtBlend.DestBlend = gl_d3d11::ConvertBlendFunc(blendState.destBlendRGB, false);
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000127 rtBlend.BlendOp = gl_d3d11::ConvertBlendOp(blendState.blendEquationRGB);
128
daniel@transgaming.com90c634a2013-01-11 04:10:01 +0000129 rtBlend.SrcBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendAlpha, true);
130 rtBlend.DestBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.destBlendAlpha, true);
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000131 rtBlend.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha);
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000132 }
daniel@transgaming.com97b59302012-11-28 21:05:34 +0000133
134 rtBlend.RenderTargetWriteMask = gl_d3d11::ConvertColorMask(blendState.colorMaskRed,
135 blendState.colorMaskGreen,
136 blendState.colorMaskBlue,
137 blendState.colorMaskAlpha);
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000138 }
139
daniel@transgaming.com3cf86502013-01-11 04:08:40 +0000140 ID3D11BlendState *dx11BlendState = NULL;
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000141 HRESULT result = mDevice->CreateBlendState(&blendDesc, &dx11BlendState);
142 if (FAILED(result) || !dx11BlendState)
143 {
144 ERR("Unable to create a ID3D11BlendState, HRESULT: 0x%X.", result);
145 return NULL;
146 }
147
148 mBlendStateCache.insert(std::make_pair(blendState, std::make_pair(dx11BlendState, mCounter++)));
149
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000150 return dx11BlendState;
151 }
152}
153
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000154std::size_t RenderStateCache::hashRasterizerState(const RasterizerStateKey &rasterState)
155{
156 static const unsigned int seed = 0xABCDEF98;
157
158 std::size_t hash = 0;
159 MurmurHash3_x86_32(&rasterState, sizeof(RasterizerStateKey), seed, &hash);
160 return hash;
161}
162
163bool RenderStateCache::compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b)
164{
165 return memcmp(&a, &b, sizeof(RasterizerStateKey)) == 0;
166}
167
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000168ID3D11RasterizerState *RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState,
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000169 bool scissorEnabled, unsigned int depthSize)
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000170{
171 if (!mDevice)
172 {
173 ERR("RenderStateCache is not initialized.");
174 return NULL;
175 }
176
177 RasterizerStateKey key;
178 key.rasterizerState = rasterState;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000179 key.scissorEnabled = scissorEnabled;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000180 key.depthSize = depthSize;
181
182 RasterizerStateMap::iterator i = mRasterizerStateCache.find(key);
183 if (i != mRasterizerStateCache.end())
184 {
185 RasterizerStateCounterPair &state = i->second;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000186 state.second = mCounter++;
187 return state.first;
188 }
189 else
190 {
191 if (mRasterizerStateCache.size() >= kMaxRasterizerStates)
192 {
193 TRACE("Overflowed the limit of %u rasterizer states, removing the least recently used "
194 "to make room.", kMaxRasterizerStates);
195
196 RasterizerStateMap::iterator leastRecentlyUsed = mRasterizerStateCache.begin();
197 for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++)
198 {
199 if (i->second.second < leastRecentlyUsed->second.second)
200 {
201 leastRecentlyUsed = i;
202 }
203 }
Geoff Langea228632013-07-30 15:17:12 -0400204 SafeRelease(leastRecentlyUsed->second.first);
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000205 mRasterizerStateCache.erase(leastRecentlyUsed);
206 }
207
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +0000208 D3D11_CULL_MODE cullMode = gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode);
209
210 // Disable culling if drawing points
211 if (rasterState.pointDrawMode)
212 {
213 cullMode = D3D11_CULL_NONE;
214 }
215
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000216 D3D11_RASTERIZER_DESC rasterDesc;
217 rasterDesc.FillMode = D3D11_FILL_SOLID;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +0000218 rasterDesc.CullMode = cullMode;
daniel@transgaming.com4d4fade2013-01-11 04:09:10 +0000219 rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? FALSE: TRUE;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000220 rasterDesc.DepthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize));
221 rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of zero will preform no clamping, must be tested though.
shannon.woods@transgaming.comce3128a2013-02-28 23:14:13 +0000222 rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetFactor;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000223 rasterDesc.DepthClipEnable = TRUE;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000224 rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE;
Nicolas Capensfd396552013-06-18 21:41:30 -0400225 rasterDesc.MultisampleEnable = rasterState.multiSample;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000226 rasterDesc.AntialiasedLineEnable = FALSE;
227
daniel@transgaming.com3cf86502013-01-11 04:08:40 +0000228 ID3D11RasterizerState *dx11RasterizerState = NULL;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000229 HRESULT result = mDevice->CreateRasterizerState(&rasterDesc, &dx11RasterizerState);
230 if (FAILED(result) || !dx11RasterizerState)
231 {
232 ERR("Unable to create a ID3D11RasterizerState, HRESULT: 0x%X.", result);
233 return NULL;
234 }
235
236 mRasterizerStateCache.insert(std::make_pair(key, std::make_pair(dx11RasterizerState, mCounter++)));
237
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000238 return dx11RasterizerState;
239 }
240}
241
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000242std::size_t RenderStateCache::hashDepthStencilState(const gl::DepthStencilState &dsState)
243{
244 static const unsigned int seed = 0xABCDEF98;
245
246 std::size_t hash = 0;
247 MurmurHash3_x86_32(&dsState, sizeof(gl::DepthStencilState), seed, &hash);
248 return hash;
249}
250
251bool RenderStateCache::compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b)
252{
253 return memcmp(&a, &b, sizeof(gl::DepthStencilState)) == 0;
254}
255
daniel@transgaming.com3cf86502013-01-11 04:08:40 +0000256ID3D11DepthStencilState *RenderStateCache::getDepthStencilState(const gl::DepthStencilState &dsState)
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000257{
258 if (!mDevice)
259 {
260 ERR("RenderStateCache is not initialized.");
261 return NULL;
262 }
263
264 DepthStencilStateMap::iterator i = mDepthStencilStateCache.find(dsState);
265 if (i != mDepthStencilStateCache.end())
266 {
267 DepthStencilStateCounterPair &state = i->second;
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000268 state.second = mCounter++;
269 return state.first;
270 }
271 else
272 {
273 if (mDepthStencilStateCache.size() >= kMaxDepthStencilStates)
274 {
275 TRACE("Overflowed the limit of %u depth stencil states, removing the least recently used "
276 "to make room.", kMaxDepthStencilStates);
277
278 DepthStencilStateMap::iterator leastRecentlyUsed = mDepthStencilStateCache.begin();
279 for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++)
280 {
281 if (i->second.second < leastRecentlyUsed->second.second)
282 {
283 leastRecentlyUsed = i;
284 }
285 }
Geoff Langea228632013-07-30 15:17:12 -0400286 SafeRelease(leastRecentlyUsed->second.first);
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000287 mDepthStencilStateCache.erase(leastRecentlyUsed);
288 }
289
290 D3D11_DEPTH_STENCIL_DESC dsDesc = { 0 };
291 dsDesc.DepthEnable = dsState.depthTest ? TRUE : FALSE;
292 dsDesc.DepthWriteMask = gl_d3d11::ConvertDepthMask(dsState.depthMask);
293 dsDesc.DepthFunc = gl_d3d11::ConvertComparison(dsState.depthFunc);
294 dsDesc.StencilEnable = dsState.stencilTest ? TRUE : FALSE;
295 dsDesc.StencilReadMask = gl_d3d11::ConvertStencilMask(dsState.stencilMask);
296 dsDesc.StencilWriteMask = gl_d3d11::ConvertStencilMask(dsState.stencilWritemask);
297 dsDesc.FrontFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilFail);
298 dsDesc.FrontFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthFail);
299 dsDesc.FrontFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthPass);
300 dsDesc.FrontFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilFunc);
301 dsDesc.BackFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackFail);
302 dsDesc.BackFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthFail);
303 dsDesc.BackFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthPass);
304 dsDesc.BackFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilBackFunc);
305
daniel@transgaming.com3cf86502013-01-11 04:08:40 +0000306 ID3D11DepthStencilState *dx11DepthStencilState = NULL;
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000307 HRESULT result = mDevice->CreateDepthStencilState(&dsDesc, &dx11DepthStencilState);
308 if (FAILED(result) || !dx11DepthStencilState)
309 {
310 ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
311 return NULL;
312 }
313
314 mDepthStencilStateCache.insert(std::make_pair(dsState, std::make_pair(dx11DepthStencilState, mCounter++)));
315
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000316 return dx11DepthStencilState;
317 }
318}
319
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000320std::size_t RenderStateCache::hashSamplerState(const gl::SamplerState &samplerState)
321{
322 static const unsigned int seed = 0xABCDEF98;
323
324 std::size_t hash = 0;
325 MurmurHash3_x86_32(&samplerState, sizeof(gl::SamplerState), seed, &hash);
326 return hash;
327}
328
329bool RenderStateCache::compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b)
330{
daniel@transgaming.comcc47bc02013-01-11 04:09:33 +0000331 return memcmp(&a, &b, sizeof(gl::SamplerState)) == 0;
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000332}
333
daniel@transgaming.com3cf86502013-01-11 04:08:40 +0000334ID3D11SamplerState *RenderStateCache::getSamplerState(const gl::SamplerState &samplerState)
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000335{
336 if (!mDevice)
337 {
338 ERR("RenderStateCache is not initialized.");
339 return NULL;
340 }
341
342 SamplerStateMap::iterator i = mSamplerStateCache.find(samplerState);
343 if (i != mSamplerStateCache.end())
344 {
345 SamplerStateCounterPair &state = i->second;
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000346 state.second = mCounter++;
347 return state.first;
348 }
349 else
350 {
351 if (mSamplerStateCache.size() >= kMaxSamplerStates)
352 {
353 TRACE("Overflowed the limit of %u sampler states, removing the least recently used "
354 "to make room.", kMaxSamplerStates);
355
356 SamplerStateMap::iterator leastRecentlyUsed = mSamplerStateCache.begin();
357 for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++)
358 {
359 if (i->second.second < leastRecentlyUsed->second.second)
360 {
361 leastRecentlyUsed = i;
362 }
363 }
Geoff Langea228632013-07-30 15:17:12 -0400364 SafeRelease(leastRecentlyUsed->second.first);
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000365 mSamplerStateCache.erase(leastRecentlyUsed);
366 }
367
368 D3D11_SAMPLER_DESC samplerDesc;
Geoff Langc82fc412013-07-10 14:43:42 -0400369 samplerDesc.Filter = gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter,
370 samplerState.maxAnisotropy, samplerState.compareMode);
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000371 samplerDesc.AddressU = gl_d3d11::ConvertTextureWrap(samplerState.wrapS);
372 samplerDesc.AddressV = gl_d3d11::ConvertTextureWrap(samplerState.wrapT);
shannon.woods%transgaming.com@gtempaccount.com0b3a8df2013-04-13 03:44:51 +0000373 samplerDesc.AddressW = gl_d3d11::ConvertTextureWrap(samplerState.wrapR);
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000374 samplerDesc.MipLODBias = static_cast<float>(samplerState.lodOffset);
375 samplerDesc.MaxAnisotropy = samplerState.maxAnisotropy;
Geoff Langc82fc412013-07-10 14:43:42 -0400376 samplerDesc.ComparisonFunc = gl_d3d11::ConvertComparison(samplerState.compareFunc);
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000377 samplerDesc.BorderColor[0] = 0.0f;
378 samplerDesc.BorderColor[1] = 0.0f;
379 samplerDesc.BorderColor[2] = 0.0f;
380 samplerDesc.BorderColor[3] = 0.0f;
shannon.woods@transgaming.com86758652013-02-28 23:20:26 +0000381 samplerDesc.MinLOD = gl_d3d11::ConvertMinLOD(samplerState.minFilter, samplerState.lodOffset);
382 samplerDesc.MaxLOD = gl_d3d11::ConvertMaxLOD(samplerState.minFilter, samplerState.lodOffset);
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000383
384 ID3D11SamplerState *dx11SamplerState = NULL;
385 HRESULT result = mDevice->CreateSamplerState(&samplerDesc, &dx11SamplerState);
386 if (FAILED(result) || !dx11SamplerState)
387 {
388 ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
389 return NULL;
390 }
391
392 mSamplerStateCache.insert(std::make_pair(samplerState, std::make_pair(dx11SamplerState, mCounter++)));
393
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000394 return dx11SamplerState;
395 }
396}
397
Geoff Langea228632013-07-30 15:17:12 -0400398}