blob: 79b2a9055306b4364695f01331d79eebe8227196 [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
daniel@transgaming.com3cf86502013-01-11 04:08:40 +000040void RenderStateCache::initialize(ID3D11Device *device)
daniel@transgaming.com0673d792012-11-28 19:37:44 +000041{
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;
daniel@transgaming.com0673d792012-11-28 19:37:44 +000099 state.second = mCounter++;
100 return state.first;
101 }
102 else
103 {
104 if (mBlendStateCache.size() >= kMaxBlendStates)
105 {
106 TRACE("Overflowed the limit of %u blend states, removing the least recently used "
107 "to make room.", kMaxBlendStates);
108
109 BlendStateMap::iterator leastRecentlyUsed = mBlendStateCache.begin();
110 for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++)
111 {
112 if (i->second.second < leastRecentlyUsed->second.second)
113 {
114 leastRecentlyUsed = i;
115 }
116 }
117 leastRecentlyUsed->second.first->Release();
118 mBlendStateCache.erase(leastRecentlyUsed);
119 }
120
121 // Create a new blend state and insert it into the cache
122 D3D11_BLEND_DESC blendDesc = { 0 };
123 blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage;
124 blendDesc.IndependentBlendEnable = FALSE;
125
126 for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
127 {
128 D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = blendDesc.RenderTarget[i];
129
130 rtBlend.BlendEnable = blendState.blend;
131 if (blendState.blend)
132 {
daniel@transgaming.com90c634a2013-01-11 04:10:01 +0000133 rtBlend.SrcBlend = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendRGB, false);
134 rtBlend.DestBlend = gl_d3d11::ConvertBlendFunc(blendState.destBlendRGB, false);
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000135 rtBlend.BlendOp = gl_d3d11::ConvertBlendOp(blendState.blendEquationRGB);
136
daniel@transgaming.com90c634a2013-01-11 04:10:01 +0000137 rtBlend.SrcBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendAlpha, true);
138 rtBlend.DestBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.destBlendAlpha, true);
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000139 rtBlend.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha);
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000140 }
daniel@transgaming.com97b59302012-11-28 21:05:34 +0000141
142 rtBlend.RenderTargetWriteMask = gl_d3d11::ConvertColorMask(blendState.colorMaskRed,
143 blendState.colorMaskGreen,
144 blendState.colorMaskBlue,
145 blendState.colorMaskAlpha);
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000146 }
147
daniel@transgaming.com3cf86502013-01-11 04:08:40 +0000148 ID3D11BlendState *dx11BlendState = NULL;
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000149 HRESULT result = mDevice->CreateBlendState(&blendDesc, &dx11BlendState);
150 if (FAILED(result) || !dx11BlendState)
151 {
152 ERR("Unable to create a ID3D11BlendState, HRESULT: 0x%X.", result);
153 return NULL;
154 }
155
156 mBlendStateCache.insert(std::make_pair(blendState, std::make_pair(dx11BlendState, mCounter++)));
157
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000158 return dx11BlendState;
159 }
160}
161
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000162std::size_t RenderStateCache::hashRasterizerState(const RasterizerStateKey &rasterState)
163{
164 static const unsigned int seed = 0xABCDEF98;
165
166 std::size_t hash = 0;
167 MurmurHash3_x86_32(&rasterState, sizeof(RasterizerStateKey), seed, &hash);
168 return hash;
169}
170
171bool RenderStateCache::compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b)
172{
173 return memcmp(&a, &b, sizeof(RasterizerStateKey)) == 0;
174}
175
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000176ID3D11RasterizerState *RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState,
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000177 bool scissorEnabled, unsigned int depthSize)
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000178{
179 if (!mDevice)
180 {
181 ERR("RenderStateCache is not initialized.");
182 return NULL;
183 }
184
185 RasterizerStateKey key;
186 key.rasterizerState = rasterState;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000187 key.scissorEnabled = scissorEnabled;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000188 key.depthSize = depthSize;
189
190 RasterizerStateMap::iterator i = mRasterizerStateCache.find(key);
191 if (i != mRasterizerStateCache.end())
192 {
193 RasterizerStateCounterPair &state = i->second;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000194 state.second = mCounter++;
195 return state.first;
196 }
197 else
198 {
199 if (mRasterizerStateCache.size() >= kMaxRasterizerStates)
200 {
201 TRACE("Overflowed the limit of %u rasterizer states, removing the least recently used "
202 "to make room.", kMaxRasterizerStates);
203
204 RasterizerStateMap::iterator leastRecentlyUsed = mRasterizerStateCache.begin();
205 for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++)
206 {
207 if (i->second.second < leastRecentlyUsed->second.second)
208 {
209 leastRecentlyUsed = i;
210 }
211 }
212 leastRecentlyUsed->second.first->Release();
213 mRasterizerStateCache.erase(leastRecentlyUsed);
214 }
215
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +0000216 D3D11_CULL_MODE cullMode = gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode);
217
218 // Disable culling if drawing points
219 if (rasterState.pointDrawMode)
220 {
221 cullMode = D3D11_CULL_NONE;
222 }
223
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000224 D3D11_RASTERIZER_DESC rasterDesc;
225 rasterDesc.FillMode = D3D11_FILL_SOLID;
shannon.woods@transgaming.comdd2524c2013-02-28 23:04:33 +0000226 rasterDesc.CullMode = cullMode;
daniel@transgaming.com4d4fade2013-01-11 04:09:10 +0000227 rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? FALSE: TRUE;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000228 rasterDesc.DepthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize));
229 rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of zero will preform no clamping, must be tested though.
230 rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetUnits;
231 rasterDesc.DepthClipEnable = TRUE;
daniel@transgaming.comd55e8c12012-11-28 21:07:02 +0000232 rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000233 rasterDesc.MultisampleEnable = TRUE;
234 rasterDesc.AntialiasedLineEnable = FALSE;
235
daniel@transgaming.com3cf86502013-01-11 04:08:40 +0000236 ID3D11RasterizerState *dx11RasterizerState = NULL;
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000237 HRESULT result = mDevice->CreateRasterizerState(&rasterDesc, &dx11RasterizerState);
238 if (FAILED(result) || !dx11RasterizerState)
239 {
240 ERR("Unable to create a ID3D11RasterizerState, HRESULT: 0x%X.", result);
241 return NULL;
242 }
243
244 mRasterizerStateCache.insert(std::make_pair(key, std::make_pair(dx11RasterizerState, mCounter++)));
245
daniel@transgaming.comed453e02012-11-28 19:38:11 +0000246 return dx11RasterizerState;
247 }
248}
249
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000250std::size_t RenderStateCache::hashDepthStencilState(const gl::DepthStencilState &dsState)
251{
252 static const unsigned int seed = 0xABCDEF98;
253
254 std::size_t hash = 0;
255 MurmurHash3_x86_32(&dsState, sizeof(gl::DepthStencilState), seed, &hash);
256 return hash;
257}
258
259bool RenderStateCache::compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b)
260{
261 return memcmp(&a, &b, sizeof(gl::DepthStencilState)) == 0;
262}
263
daniel@transgaming.com3cf86502013-01-11 04:08:40 +0000264ID3D11DepthStencilState *RenderStateCache::getDepthStencilState(const gl::DepthStencilState &dsState)
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000265{
266 if (!mDevice)
267 {
268 ERR("RenderStateCache is not initialized.");
269 return NULL;
270 }
271
272 DepthStencilStateMap::iterator i = mDepthStencilStateCache.find(dsState);
273 if (i != mDepthStencilStateCache.end())
274 {
275 DepthStencilStateCounterPair &state = i->second;
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000276 state.second = mCounter++;
277 return state.first;
278 }
279 else
280 {
281 if (mDepthStencilStateCache.size() >= kMaxDepthStencilStates)
282 {
283 TRACE("Overflowed the limit of %u depth stencil states, removing the least recently used "
284 "to make room.", kMaxDepthStencilStates);
285
286 DepthStencilStateMap::iterator leastRecentlyUsed = mDepthStencilStateCache.begin();
287 for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++)
288 {
289 if (i->second.second < leastRecentlyUsed->second.second)
290 {
291 leastRecentlyUsed = i;
292 }
293 }
294 leastRecentlyUsed->second.first->Release();
295 mDepthStencilStateCache.erase(leastRecentlyUsed);
296 }
297
298 D3D11_DEPTH_STENCIL_DESC dsDesc = { 0 };
299 dsDesc.DepthEnable = dsState.depthTest ? TRUE : FALSE;
300 dsDesc.DepthWriteMask = gl_d3d11::ConvertDepthMask(dsState.depthMask);
301 dsDesc.DepthFunc = gl_d3d11::ConvertComparison(dsState.depthFunc);
302 dsDesc.StencilEnable = dsState.stencilTest ? TRUE : FALSE;
303 dsDesc.StencilReadMask = gl_d3d11::ConvertStencilMask(dsState.stencilMask);
304 dsDesc.StencilWriteMask = gl_d3d11::ConvertStencilMask(dsState.stencilWritemask);
305 dsDesc.FrontFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilFail);
306 dsDesc.FrontFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthFail);
307 dsDesc.FrontFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthPass);
308 dsDesc.FrontFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilFunc);
309 dsDesc.BackFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackFail);
310 dsDesc.BackFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthFail);
311 dsDesc.BackFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthPass);
312 dsDesc.BackFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilBackFunc);
313
daniel@transgaming.com3cf86502013-01-11 04:08:40 +0000314 ID3D11DepthStencilState *dx11DepthStencilState = NULL;
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000315 HRESULT result = mDevice->CreateDepthStencilState(&dsDesc, &dx11DepthStencilState);
316 if (FAILED(result) || !dx11DepthStencilState)
317 {
318 ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
319 return NULL;
320 }
321
322 mDepthStencilStateCache.insert(std::make_pair(dsState, std::make_pair(dx11DepthStencilState, mCounter++)));
323
daniel@transgaming.com53926ff2012-11-28 19:38:50 +0000324 return dx11DepthStencilState;
325 }
326}
327
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000328std::size_t RenderStateCache::hashSamplerState(const gl::SamplerState &samplerState)
329{
330 static const unsigned int seed = 0xABCDEF98;
331
332 std::size_t hash = 0;
333 MurmurHash3_x86_32(&samplerState, sizeof(gl::SamplerState), seed, &hash);
334 return hash;
335}
336
337bool RenderStateCache::compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b)
338{
daniel@transgaming.comcc47bc02013-01-11 04:09:33 +0000339 return memcmp(&a, &b, sizeof(gl::SamplerState)) == 0;
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000340}
341
daniel@transgaming.com3cf86502013-01-11 04:08:40 +0000342ID3D11SamplerState *RenderStateCache::getSamplerState(const gl::SamplerState &samplerState)
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000343{
344 if (!mDevice)
345 {
346 ERR("RenderStateCache is not initialized.");
347 return NULL;
348 }
349
350 SamplerStateMap::iterator i = mSamplerStateCache.find(samplerState);
351 if (i != mSamplerStateCache.end())
352 {
353 SamplerStateCounterPair &state = i->second;
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000354 state.second = mCounter++;
355 return state.first;
356 }
357 else
358 {
359 if (mSamplerStateCache.size() >= kMaxSamplerStates)
360 {
361 TRACE("Overflowed the limit of %u sampler states, removing the least recently used "
362 "to make room.", kMaxSamplerStates);
363
364 SamplerStateMap::iterator leastRecentlyUsed = mSamplerStateCache.begin();
365 for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++)
366 {
367 if (i->second.second < leastRecentlyUsed->second.second)
368 {
369 leastRecentlyUsed = i;
370 }
371 }
372 leastRecentlyUsed->second.first->Release();
373 mSamplerStateCache.erase(leastRecentlyUsed);
374 }
375
376 D3D11_SAMPLER_DESC samplerDesc;
377 samplerDesc.Filter = gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter, samplerState.maxAnisotropy);
378 samplerDesc.AddressU = gl_d3d11::ConvertTextureWrap(samplerState.wrapS);
379 samplerDesc.AddressV = gl_d3d11::ConvertTextureWrap(samplerState.wrapT);
380 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
381 samplerDesc.MipLODBias = static_cast<float>(samplerState.lodOffset);
382 samplerDesc.MaxAnisotropy = samplerState.maxAnisotropy;
383 samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
384 samplerDesc.BorderColor[0] = 0.0f;
385 samplerDesc.BorderColor[1] = 0.0f;
386 samplerDesc.BorderColor[2] = 0.0f;
387 samplerDesc.BorderColor[3] = 0.0f;
388 samplerDesc.MinLOD = gl_d3d11::ConvertMinLOD(samplerState.minFilter);
389 samplerDesc.MaxLOD = gl_d3d11::ConvertMaxLOD(samplerState.minFilter);
390
391 ID3D11SamplerState *dx11SamplerState = NULL;
392 HRESULT result = mDevice->CreateSamplerState(&samplerDesc, &dx11SamplerState);
393 if (FAILED(result) || !dx11SamplerState)
394 {
395 ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
396 return NULL;
397 }
398
399 mSamplerStateCache.insert(std::make_pair(samplerState, std::make_pair(dx11SamplerState, mCounter++)));
400
daniel@transgaming.com8e4f5522013-01-11 04:07:53 +0000401 return dx11SamplerState;
402 }
403}
404
daniel@transgaming.com0673d792012-11-28 19:37:44 +0000405}