blob: 027b2196d1852c108f55564eebc5cf1189fc4dc7 [file] [log] [blame]
daniel@transgaming.com0673d792012-11-28 19:37:44 +00001//
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +00002// Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com0673d792012-11-28 19:37:44 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// RenderStateCache.cpp: Defines rx::RenderStateCache, a cache of Direct3D render
8// state objects.
9
10#include "libGLESv2/renderer/RenderStateCache.h"
11#include "libGLESv2/renderer/renderer11_utils.h"
12
13#include "common/debug.h"
14#include "third_party/murmurhash/MurmurHash3.h"
15
16namespace rx
17{
18
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +000019// MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState,
20// ID3D11Device::CreateDepthStencilState and ID3D11Device::CreateSamplerState claims the maximum
21// number of unique states of each type an application can create is 4096
daniel@transgaming.comc497eba2012-11-28 19:39:06 +000022const unsigned int RenderStateCache::kMaxBlendStates = 4096;
23const unsigned int RenderStateCache::kMaxRasterizerStates = 4096;
24const unsigned int RenderStateCache::kMaxDepthStencilStates = 4096;
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +000025const unsigned int RenderStateCache::kMaxSamplerStates = 4096;
daniel@transgaming.comc497eba2012-11-28 19:39:06 +000026
daniel@transgaming.com0673d792012-11-28 19:37:44 +000027RenderStateCache::RenderStateCache() : mDevice(NULL), mCounter(0),
daniel@transgaming.comed453e02012-11-28 19:38:11 +000028 mBlendStateCache(kMaxBlendStates, hashBlendState, compareBlendStates),
daniel@transgaming.com53926ff2012-11-28 19:38:50 +000029 mRasterizerStateCache(kMaxRasterizerStates, hashRasterizerState, compareRasterizerStates),
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +000030 mDepthStencilStateCache(kMaxDepthStencilStates, hashDepthStencilState, compareDepthStencilStates),
31 mSamplerStateCache(kMaxSamplerStates, hashSamplerState, compareSamplerStates)
daniel@transgaming.com0673d792012-11-28 19:37:44 +000032{
33}
34
35RenderStateCache::~RenderStateCache()
36{
37 clear();
38}
39
40void RenderStateCache::initialize(ID3D11Device* device)
41{
42 clear();
43 mDevice = device;
44}
45
46void RenderStateCache::clear()
47{
48 for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++)
49 {
50 i->second.first->Release();
51 }
52 mBlendStateCache.clear();
daniel@transgaming.comed453e02012-11-28 19:38:11 +000053
54 for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++)
55 {
56 i->second.first->Release();
57 }
58 mRasterizerStateCache.clear();
daniel@transgaming.com53926ff2012-11-28 19:38:50 +000059
60 for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++)
61 {
62 i->second.first->Release();
63 }
64 mDepthStencilStateCache.clear();
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +000065
66 for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++)
67 {
68 i->second.first->Release();
69 }
70 mSamplerStateCache.clear();
daniel@transgaming.com0673d792012-11-28 19:37:44 +000071}
72
73std::size_t RenderStateCache::hashBlendState(const gl::BlendState &blendState)
74{
75 static const unsigned int seed = 0xABCDEF98;
76
77 std::size_t hash = 0;
78 MurmurHash3_x86_32(&blendState, sizeof(gl::BlendState), seed, &hash);
79 return hash;
80}
81
82bool RenderStateCache::compareBlendStates(const gl::BlendState &a, const gl::BlendState &b)
83{
84 return memcmp(&a, &b, sizeof(gl::BlendState)) == 0;
85}
86
daniel@transgaming.com0673d792012-11-28 19:37:44 +000087ID3D11BlendState *RenderStateCache::getBlendState(const gl::BlendState &blendState)
88{
89 if (!mDevice)
90 {
91 ERR("RenderStateCache is not initialized.");
92 return NULL;
93 }
94
95 BlendStateMap::iterator i = mBlendStateCache.find(blendState);
96 if (i != mBlendStateCache.end())
97 {
98 BlendStateCounterPair &state = i->second;
99 state.first->AddRef();
100 state.second = mCounter++;
101 return state.first;
102 }
103 else
104 {
105 if (mBlendStateCache.size() >= kMaxBlendStates)
106 {
107 TRACE("Overflowed the limit of %u blend states, removing the least recently used "
108 "to make room.", kMaxBlendStates);
109
110 BlendStateMap::iterator leastRecentlyUsed = mBlendStateCache.begin();
111 for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++)
112 {
113 if (i->second.second < leastRecentlyUsed->second.second)
114 {
115 leastRecentlyUsed = i;
116 }
117 }
118 leastRecentlyUsed->second.first->Release();
119 mBlendStateCache.erase(leastRecentlyUsed);
120 }
121
122 // Create a new blend state and insert it into the cache
123 D3D11_BLEND_DESC blendDesc = { 0 };
124 blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage;
125 blendDesc.IndependentBlendEnable = FALSE;
126
127 for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
128 {
129 D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = blendDesc.RenderTarget[i];
130
131 rtBlend.BlendEnable = blendState.blend;
132 if (blendState.blend)
133 {
134 rtBlend.SrcBlend = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendRGB);
135 rtBlend.DestBlend = gl_d3d11::ConvertBlendFunc(blendState.destBlendRGB);
136 rtBlend.BlendOp = gl_d3d11::ConvertBlendOp(blendState.blendEquationRGB);
137
138 rtBlend.SrcBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendAlpha);
139 rtBlend.DestBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.destBlendAlpha);
140 rtBlend.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha);
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000141 }
daniel@transgaming.com97b59302012-11-28 21:05:34 +0000142
143 rtBlend.RenderTargetWriteMask = gl_d3d11::ConvertColorMask(blendState.colorMaskRed,
144 blendState.colorMaskGreen,
145 blendState.colorMaskBlue,
146 blendState.colorMaskAlpha);
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000147 }
148
149 ID3D11BlendState* dx11BlendState = NULL;
150 HRESULT result = mDevice->CreateBlendState(&blendDesc, &dx11BlendState);
151 if (FAILED(result) || !dx11BlendState)
152 {
153 ERR("Unable to create a ID3D11BlendState, HRESULT: 0x%X.", result);
154 return NULL;
155 }
156
157 mBlendStateCache.insert(std::make_pair(blendState, std::make_pair(dx11BlendState, mCounter++)));
158
159 dx11BlendState->AddRef();
160 return dx11BlendState;
161 }
162}
163
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000164std::size_t RenderStateCache::hashRasterizerState(const RasterizerStateKey &rasterState)
165{
166 static const unsigned int seed = 0xABCDEF98;
167
168 std::size_t hash = 0;
169 MurmurHash3_x86_32(&rasterState, sizeof(RasterizerStateKey), seed, &hash);
170 return hash;
171}
172
173bool RenderStateCache::compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b)
174{
175 return memcmp(&a, &b, sizeof(RasterizerStateKey)) == 0;
176}
177
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000178ID3D11RasterizerState *RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState,
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000179 bool scissorEnabled, unsigned int depthSize)
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000180{
181 if (!mDevice)
182 {
183 ERR("RenderStateCache is not initialized.");
184 return NULL;
185 }
186
187 RasterizerStateKey key;
188 key.rasterizerState = rasterState;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000189 key.scissorEnabled = scissorEnabled;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000190 key.depthSize = depthSize;
191
192 RasterizerStateMap::iterator i = mRasterizerStateCache.find(key);
193 if (i != mRasterizerStateCache.end())
194 {
195 RasterizerStateCounterPair &state = i->second;
196 state.first->AddRef();
197 state.second = mCounter++;
198 return state.first;
199 }
200 else
201 {
202 if (mRasterizerStateCache.size() >= kMaxRasterizerStates)
203 {
204 TRACE("Overflowed the limit of %u rasterizer states, removing the least recently used "
205 "to make room.", kMaxRasterizerStates);
206
207 RasterizerStateMap::iterator leastRecentlyUsed = mRasterizerStateCache.begin();
208 for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++)
209 {
210 if (i->second.second < leastRecentlyUsed->second.second)
211 {
212 leastRecentlyUsed = i;
213 }
214 }
215 leastRecentlyUsed->second.first->Release();
216 mRasterizerStateCache.erase(leastRecentlyUsed);
217 }
218
219 D3D11_RASTERIZER_DESC rasterDesc;
220 rasterDesc.FillMode = D3D11_FILL_SOLID;
221 rasterDesc.CullMode = gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode);
222 rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? TRUE : FALSE;
223 rasterDesc.DepthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize));
224 rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of zero will preform no clamping, must be tested though.
225 rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetUnits;
226 rasterDesc.DepthClipEnable = TRUE;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000227 rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000228 rasterDesc.MultisampleEnable = TRUE;
229 rasterDesc.AntialiasedLineEnable = FALSE;
230
231 ID3D11RasterizerState* dx11RasterizerState = NULL;
232 HRESULT result = mDevice->CreateRasterizerState(&rasterDesc, &dx11RasterizerState);
233 if (FAILED(result) || !dx11RasterizerState)
234 {
235 ERR("Unable to create a ID3D11RasterizerState, HRESULT: 0x%X.", result);
236 return NULL;
237 }
238
239 mRasterizerStateCache.insert(std::make_pair(key, std::make_pair(dx11RasterizerState, mCounter++)));
240
241 dx11RasterizerState->AddRef();
242 return dx11RasterizerState;
243 }
244}
245
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000246std::size_t RenderStateCache::hashDepthStencilState(const gl::DepthStencilState &dsState)
247{
248 static const unsigned int seed = 0xABCDEF98;
249
250 std::size_t hash = 0;
251 MurmurHash3_x86_32(&dsState, sizeof(gl::DepthStencilState), seed, &hash);
252 return hash;
253}
254
255bool RenderStateCache::compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b)
256{
257 return memcmp(&a, &b, sizeof(gl::DepthStencilState)) == 0;
258}
259
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000260ID3D11DepthStencilState* RenderStateCache::getDepthStencilState(const gl::DepthStencilState &dsState)
261{
262 if (!mDevice)
263 {
264 ERR("RenderStateCache is not initialized.");
265 return NULL;
266 }
267
268 DepthStencilStateMap::iterator i = mDepthStencilStateCache.find(dsState);
269 if (i != mDepthStencilStateCache.end())
270 {
271 DepthStencilStateCounterPair &state = i->second;
272 state.first->AddRef();
273 state.second = mCounter++;
274 return state.first;
275 }
276 else
277 {
278 if (mDepthStencilStateCache.size() >= kMaxDepthStencilStates)
279 {
280 TRACE("Overflowed the limit of %u depth stencil states, removing the least recently used "
281 "to make room.", kMaxDepthStencilStates);
282
283 DepthStencilStateMap::iterator leastRecentlyUsed = mDepthStencilStateCache.begin();
284 for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++)
285 {
286 if (i->second.second < leastRecentlyUsed->second.second)
287 {
288 leastRecentlyUsed = i;
289 }
290 }
291 leastRecentlyUsed->second.first->Release();
292 mDepthStencilStateCache.erase(leastRecentlyUsed);
293 }
294
295 D3D11_DEPTH_STENCIL_DESC dsDesc = { 0 };
296 dsDesc.DepthEnable = dsState.depthTest ? TRUE : FALSE;
297 dsDesc.DepthWriteMask = gl_d3d11::ConvertDepthMask(dsState.depthMask);
298 dsDesc.DepthFunc = gl_d3d11::ConvertComparison(dsState.depthFunc);
299 dsDesc.StencilEnable = dsState.stencilTest ? TRUE : FALSE;
300 dsDesc.StencilReadMask = gl_d3d11::ConvertStencilMask(dsState.stencilMask);
301 dsDesc.StencilWriteMask = gl_d3d11::ConvertStencilMask(dsState.stencilWritemask);
302 dsDesc.FrontFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilFail);
303 dsDesc.FrontFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthFail);
304 dsDesc.FrontFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthPass);
305 dsDesc.FrontFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilFunc);
306 dsDesc.BackFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackFail);
307 dsDesc.BackFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthFail);
308 dsDesc.BackFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthPass);
309 dsDesc.BackFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilBackFunc);
310
311 ID3D11DepthStencilState* dx11DepthStencilState = NULL;
312 HRESULT result = mDevice->CreateDepthStencilState(&dsDesc, &dx11DepthStencilState);
313 if (FAILED(result) || !dx11DepthStencilState)
314 {
315 ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
316 return NULL;
317 }
318
319 mDepthStencilStateCache.insert(std::make_pair(dsState, std::make_pair(dx11DepthStencilState, mCounter++)));
320
321 dx11DepthStencilState->AddRef();
322 return dx11DepthStencilState;
323 }
324}
325
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000326std::size_t RenderStateCache::hashSamplerState(const gl::SamplerState &samplerState)
327{
328 static const unsigned int seed = 0xABCDEF98;
329
330 std::size_t hash = 0;
331 MurmurHash3_x86_32(&samplerState, sizeof(gl::SamplerState), seed, &hash);
332 return hash;
333}
334
335bool RenderStateCache::compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b)
336{
337 return memcmp(&a, &b, sizeof(gl::DepthStencilState)) == 0;
338}
339
340ID3D11SamplerState* RenderStateCache::getSamplerState(const gl::SamplerState &samplerState)
341{
342 if (!mDevice)
343 {
344 ERR("RenderStateCache is not initialized.");
345 return NULL;
346 }
347
348 SamplerStateMap::iterator i = mSamplerStateCache.find(samplerState);
349 if (i != mSamplerStateCache.end())
350 {
351 SamplerStateCounterPair &state = i->second;
352 state.first->AddRef();
353 state.second = mCounter++;
354 return state.first;
355 }
356 else
357 {
358 if (mSamplerStateCache.size() >= kMaxSamplerStates)
359 {
360 TRACE("Overflowed the limit of %u sampler states, removing the least recently used "
361 "to make room.", kMaxSamplerStates);
362
363 SamplerStateMap::iterator leastRecentlyUsed = mSamplerStateCache.begin();
364 for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++)
365 {
366 if (i->second.second < leastRecentlyUsed->second.second)
367 {
368 leastRecentlyUsed = i;
369 }
370 }
371 leastRecentlyUsed->second.first->Release();
372 mSamplerStateCache.erase(leastRecentlyUsed);
373 }
374
375 D3D11_SAMPLER_DESC samplerDesc;
376 samplerDesc.Filter = gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter, samplerState.maxAnisotropy);
377 samplerDesc.AddressU = gl_d3d11::ConvertTextureWrap(samplerState.wrapS);
378 samplerDesc.AddressV = gl_d3d11::ConvertTextureWrap(samplerState.wrapT);
379 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
380 samplerDesc.MipLODBias = static_cast<float>(samplerState.lodOffset);
381 samplerDesc.MaxAnisotropy = samplerState.maxAnisotropy;
382 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
383 samplerDesc.BorderColor[0] = 0.0f;
384 samplerDesc.BorderColor[1] = 0.0f;
385 samplerDesc.BorderColor[2] = 0.0f;
386 samplerDesc.BorderColor[3] = 0.0f;
387 samplerDesc.MinLOD = gl_d3d11::ConvertMinLOD(samplerState.minFilter);
388 samplerDesc.MaxLOD = gl_d3d11::ConvertMaxLOD(samplerState.minFilter);
389
390 ID3D11SamplerState *dx11SamplerState = NULL;
391 HRESULT result = mDevice->CreateSamplerState(&samplerDesc, &dx11SamplerState);
392 if (FAILED(result) || !dx11SamplerState)
393 {
394 ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
395 return NULL;
396 }
397
398 mSamplerStateCache.insert(std::make_pair(samplerState, std::make_pair(dx11SamplerState, mCounter++)));
399
400 dx11SamplerState->AddRef();
401 return dx11SamplerState;
402 }
403}
404
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000405}