Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / src / libGLESv2 / renderer / d3d11 / RenderStateCache.cpp
1 #include "precompiled.h"
2 //
3 // Copyright (c) 2012-2014 The ANGLE Project Authors. All rights reserved.
4 // 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/d3d11/RenderStateCache.h"
12 #include "libGLESv2/renderer/d3d11/renderer11_utils.h"
13 #include "libGLESv2/renderer/Renderer.h"
14 #include "libGLESv2/Framebuffer.h"
15 #include "libGLESv2/Renderbuffer.h"
16
17 #include "common/debug.h"
18 #include "third_party/murmurhash/MurmurHash3.h"
19
20 namespace rx
21 {
22
23 template <typename mapType>
24 static void ClearStateMap(mapType &map)
25 {
26     for (mapType::iterator i = map.begin(); i != map.end(); i++)
27     {
28         SafeRelease(i->second.first);
29     }
30     map.clear();
31 }
32
33 // MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState,
34 // ID3D11Device::CreateDepthStencilState and ID3D11Device::CreateSamplerState claims the maximum
35 // number of unique states of each type an application can create is 4096
36 const unsigned int RenderStateCache::kMaxBlendStates = 4096;
37 const unsigned int RenderStateCache::kMaxRasterizerStates = 4096;
38 const unsigned int RenderStateCache::kMaxDepthStencilStates = 4096;
39 const unsigned int RenderStateCache::kMaxSamplerStates = 4096;
40
41 RenderStateCache::RenderStateCache() : mDevice(NULL), mCounter(0),
42                                        mBlendStateCache(kMaxBlendStates, hashBlendState, compareBlendStates),
43                                        mRasterizerStateCache(kMaxRasterizerStates, hashRasterizerState, compareRasterizerStates),
44                                        mDepthStencilStateCache(kMaxDepthStencilStates, hashDepthStencilState, compareDepthStencilStates),
45                                        mSamplerStateCache(kMaxSamplerStates, hashSamplerState, compareSamplerStates)
46 {
47 }
48
49 RenderStateCache::~RenderStateCache()
50 {
51     clear();
52 }
53
54 void RenderStateCache::initialize(ID3D11Device *device)
55 {
56     clear();
57     mDevice = device;
58 }
59
60 void RenderStateCache::clear()
61 {
62     ClearStateMap(mBlendStateCache);
63     ClearStateMap(mRasterizerStateCache);
64     ClearStateMap(mDepthStencilStateCache);
65     ClearStateMap(mSamplerStateCache);
66 }
67
68 std::size_t RenderStateCache::hashBlendState(const BlendStateKey &blendState)
69 {
70     static const unsigned int seed = 0xABCDEF98;
71
72     std::size_t hash = 0;
73     MurmurHash3_x86_32(&blendState, sizeof(gl::BlendState), seed, &hash);
74     return hash;
75 }
76
77 bool RenderStateCache::compareBlendStates(const BlendStateKey &a, const BlendStateKey &b)
78 {
79     return memcmp(&a, &b, sizeof(BlendStateKey)) == 0;
80 }
81
82 ID3D11BlendState *RenderStateCache::getBlendState(const gl::Framebuffer *framebuffer, const gl::BlendState &blendState)
83 {
84     if (!mDevice)
85     {
86         ERR("RenderStateCache is not initialized.");
87         return NULL;
88     }
89
90     bool mrt = false;
91
92     BlendStateKey key = { 0 };
93     key.blendState = blendState;
94     for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
95     {
96         gl::FramebufferAttachment *attachment = framebuffer->getColorbuffer(i);
97         if (attachment)
98         {
99             if (i > 0)
100             {
101                 mrt = true;
102             }
103
104             key.rtChannels[i][0] = attachment->getRedSize()   > 0;
105             key.rtChannels[i][1] = attachment->getGreenSize() > 0;
106             key.rtChannels[i][2] = attachment->getBlueSize()  > 0;
107             key.rtChannels[i][3] = attachment->getAlphaSize() > 0;
108         }
109         else
110         {
111             key.rtChannels[i][0] = false;
112             key.rtChannels[i][1] = false;
113             key.rtChannels[i][2] = false;
114             key.rtChannels[i][3] = false;
115         }
116     }
117
118     BlendStateMap::iterator keyIter = mBlendStateCache.find(key);
119     if (keyIter != mBlendStateCache.end())
120     {
121         BlendStateCounterPair &state = keyIter->second;
122         state.second = mCounter++;
123         return state.first;
124     }
125     else
126     {
127         if (mBlendStateCache.size() >= kMaxBlendStates)
128         {
129             TRACE("Overflowed the limit of %u blend states, removing the least recently used "
130                   "to make room.", kMaxBlendStates);
131
132             BlendStateMap::iterator leastRecentlyUsed = mBlendStateCache.begin();
133             for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++)
134             {
135                 if (i->second.second < leastRecentlyUsed->second.second)
136                 {
137                     leastRecentlyUsed = i;
138                 }
139             }
140             SafeRelease(leastRecentlyUsed->second.first);
141             mBlendStateCache.erase(leastRecentlyUsed);
142         }
143
144         // Create a new blend state and insert it into the cache
145         D3D11_BLEND_DESC blendDesc = { 0 };
146         blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage;
147         blendDesc.IndependentBlendEnable = mrt ? TRUE : FALSE;
148
149         for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++)
150         {
151             D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = blendDesc.RenderTarget[i];
152
153             rtBlend.BlendEnable = blendState.blend;
154             if (blendState.blend)
155             {
156                 rtBlend.SrcBlend = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendRGB, false);
157                 rtBlend.DestBlend = gl_d3d11::ConvertBlendFunc(blendState.destBlendRGB, false);
158                 rtBlend.BlendOp = gl_d3d11::ConvertBlendOp(blendState.blendEquationRGB);
159
160                 rtBlend.SrcBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendAlpha, true);
161                 rtBlend.DestBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.destBlendAlpha, true);
162                 rtBlend.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha);
163             }
164
165             rtBlend.RenderTargetWriteMask = gl_d3d11::ConvertColorMask(key.rtChannels[i][0] && blendState.colorMaskRed,
166                                                                        key.rtChannels[i][1] && blendState.colorMaskGreen,
167                                                                        key.rtChannels[i][2] && blendState.colorMaskBlue,
168                                                                        key.rtChannels[i][3] && blendState.colorMaskAlpha);
169         }
170
171         ID3D11BlendState *dx11BlendState = NULL;
172         HRESULT result = mDevice->CreateBlendState(&blendDesc, &dx11BlendState);
173         if (FAILED(result) || !dx11BlendState)
174         {
175             ERR("Unable to create a ID3D11BlendState, HRESULT: 0x%X.", result);
176             return NULL;
177         }
178
179         mBlendStateCache.insert(std::make_pair(key, std::make_pair(dx11BlendState, mCounter++)));
180
181         return dx11BlendState;
182     }
183 }
184
185 std::size_t RenderStateCache::hashRasterizerState(const RasterizerStateKey &rasterState)
186 {
187     static const unsigned int seed = 0xABCDEF98;
188
189     std::size_t hash = 0;
190     MurmurHash3_x86_32(&rasterState, sizeof(RasterizerStateKey), seed, &hash);
191     return hash;
192 }
193
194 bool RenderStateCache::compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b)
195 {
196     return memcmp(&a, &b, sizeof(RasterizerStateKey)) == 0;
197 }
198
199 ID3D11RasterizerState *RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState, bool scissorEnabled)
200 {
201     if (!mDevice)
202     {
203         ERR("RenderStateCache is not initialized.");
204         return NULL;
205     }
206
207     RasterizerStateKey key = { 0 };
208     key.rasterizerState = rasterState;
209     key.scissorEnabled = scissorEnabled;
210
211     RasterizerStateMap::iterator keyIter = mRasterizerStateCache.find(key);
212     if (keyIter != mRasterizerStateCache.end())
213     {
214         RasterizerStateCounterPair &state = keyIter->second;
215         state.second = mCounter++;
216         return state.first;
217     }
218     else
219     {
220         if (mRasterizerStateCache.size() >= kMaxRasterizerStates)
221         {
222             TRACE("Overflowed the limit of %u rasterizer states, removing the least recently used "
223                   "to make room.", kMaxRasterizerStates);
224
225             RasterizerStateMap::iterator leastRecentlyUsed = mRasterizerStateCache.begin();
226             for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++)
227             {
228                 if (i->second.second < leastRecentlyUsed->second.second)
229                 {
230                     leastRecentlyUsed = i;
231                 }
232             }
233             SafeRelease(leastRecentlyUsed->second.first);
234             mRasterizerStateCache.erase(leastRecentlyUsed);
235         }
236
237         D3D11_CULL_MODE cullMode = gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode);
238
239         // Disable culling if drawing points
240         if (rasterState.pointDrawMode)
241         {
242             cullMode = D3D11_CULL_NONE;
243         }
244
245         D3D11_RASTERIZER_DESC rasterDesc;
246         rasterDesc.FillMode = D3D11_FILL_SOLID;
247         rasterDesc.CullMode = cullMode;
248         rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? FALSE: TRUE;
249         rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of zero will preform no clamping, must be tested though.
250         rasterDesc.DepthClipEnable = TRUE;
251         rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE;
252         rasterDesc.MultisampleEnable = rasterState.multiSample;
253         rasterDesc.AntialiasedLineEnable = FALSE;
254
255         if (rasterState.polygonOffsetFill)
256         {
257             rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetFactor;
258             rasterDesc.DepthBias = (INT)rasterState.polygonOffsetUnits;
259         }
260         else
261         {
262             rasterDesc.SlopeScaledDepthBias = 0.0f;
263             rasterDesc.DepthBias = 0;
264         }
265
266         ID3D11RasterizerState *dx11RasterizerState = NULL;
267         HRESULT result = mDevice->CreateRasterizerState(&rasterDesc, &dx11RasterizerState);
268         if (FAILED(result) || !dx11RasterizerState)
269         {
270             ERR("Unable to create a ID3D11RasterizerState, HRESULT: 0x%X.", result);
271             return NULL;
272         }
273
274         mRasterizerStateCache.insert(std::make_pair(key, std::make_pair(dx11RasterizerState, mCounter++)));
275
276         return dx11RasterizerState;
277     }
278 }
279
280 std::size_t RenderStateCache::hashDepthStencilState(const gl::DepthStencilState &dsState)
281 {
282     static const unsigned int seed = 0xABCDEF98;
283
284     std::size_t hash = 0;
285     MurmurHash3_x86_32(&dsState, sizeof(gl::DepthStencilState), seed, &hash);
286     return hash;
287 }
288
289 bool RenderStateCache::compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b)
290 {
291     return memcmp(&a, &b, sizeof(gl::DepthStencilState)) == 0;
292 }
293
294 ID3D11DepthStencilState *RenderStateCache::getDepthStencilState(const gl::DepthStencilState &dsState)
295 {
296     if (!mDevice)
297     {
298         ERR("RenderStateCache is not initialized.");
299         return NULL;
300     }
301
302     DepthStencilStateMap::iterator keyIter = mDepthStencilStateCache.find(dsState);
303     if (keyIter != mDepthStencilStateCache.end())
304     {
305         DepthStencilStateCounterPair &state = keyIter->second;
306         state.second = mCounter++;
307         return state.first;
308     }
309     else
310     {
311         if (mDepthStencilStateCache.size() >= kMaxDepthStencilStates)
312         {
313             TRACE("Overflowed the limit of %u depth stencil states, removing the least recently used "
314                   "to make room.", kMaxDepthStencilStates);
315
316             DepthStencilStateMap::iterator leastRecentlyUsed = mDepthStencilStateCache.begin();
317             for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++)
318             {
319                 if (i->second.second < leastRecentlyUsed->second.second)
320                 {
321                     leastRecentlyUsed = i;
322                 }
323             }
324             SafeRelease(leastRecentlyUsed->second.first);
325             mDepthStencilStateCache.erase(leastRecentlyUsed);
326         }
327
328         D3D11_DEPTH_STENCIL_DESC dsDesc = { 0 };
329         dsDesc.DepthEnable = dsState.depthTest ? TRUE : FALSE;
330         dsDesc.DepthWriteMask = gl_d3d11::ConvertDepthMask(dsState.depthMask);
331         dsDesc.DepthFunc = gl_d3d11::ConvertComparison(dsState.depthFunc);
332         dsDesc.StencilEnable = dsState.stencilTest ? TRUE : FALSE;
333         dsDesc.StencilReadMask = gl_d3d11::ConvertStencilMask(dsState.stencilMask);
334         dsDesc.StencilWriteMask = gl_d3d11::ConvertStencilMask(dsState.stencilWritemask);
335         dsDesc.FrontFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilFail);
336         dsDesc.FrontFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthFail);
337         dsDesc.FrontFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthPass);
338         dsDesc.FrontFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilFunc);
339         dsDesc.BackFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackFail);
340         dsDesc.BackFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthFail);
341         dsDesc.BackFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthPass);
342         dsDesc.BackFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilBackFunc);
343
344         ID3D11DepthStencilState *dx11DepthStencilState = NULL;
345         HRESULT result = mDevice->CreateDepthStencilState(&dsDesc, &dx11DepthStencilState);
346         if (FAILED(result) || !dx11DepthStencilState)
347         {
348             ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
349             return NULL;
350         }
351
352         mDepthStencilStateCache.insert(std::make_pair(dsState, std::make_pair(dx11DepthStencilState, mCounter++)));
353
354         return dx11DepthStencilState;
355     }
356 }
357
358 std::size_t RenderStateCache::hashSamplerState(const gl::SamplerState &samplerState)
359 {
360     static const unsigned int seed = 0xABCDEF98;
361
362     std::size_t hash = 0;
363     MurmurHash3_x86_32(&samplerState, sizeof(gl::SamplerState), seed, &hash);
364     return hash;
365 }
366
367 bool RenderStateCache::compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b)
368 {
369     return memcmp(&a, &b, sizeof(gl::SamplerState)) == 0;
370 }
371
372 ID3D11SamplerState *RenderStateCache::getSamplerState(const gl::SamplerState &samplerState)
373 {
374     if (!mDevice)
375     {
376         ERR("RenderStateCache is not initialized.");
377         return NULL;
378     }
379
380     SamplerStateMap::iterator keyIter = mSamplerStateCache.find(samplerState);
381     if (keyIter != mSamplerStateCache.end())
382     {
383         SamplerStateCounterPair &state = keyIter->second;
384         state.second = mCounter++;
385         return state.first;
386     }
387     else
388     {
389         if (mSamplerStateCache.size() >= kMaxSamplerStates)
390         {
391             TRACE("Overflowed the limit of %u sampler states, removing the least recently used "
392                   "to make room.", kMaxSamplerStates);
393
394             SamplerStateMap::iterator leastRecentlyUsed = mSamplerStateCache.begin();
395             for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++)
396             {
397                 if (i->second.second < leastRecentlyUsed->second.second)
398                 {
399                     leastRecentlyUsed = i;
400                 }
401             }
402             SafeRelease(leastRecentlyUsed->second.first);
403             mSamplerStateCache.erase(leastRecentlyUsed);
404         }
405
406         D3D11_SAMPLER_DESC samplerDesc;
407         samplerDesc.Filter = gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter,
408                                                      samplerState.maxAnisotropy, samplerState.compareMode);
409         samplerDesc.AddressU = gl_d3d11::ConvertTextureWrap(samplerState.wrapS);
410         samplerDesc.AddressV = gl_d3d11::ConvertTextureWrap(samplerState.wrapT);
411         samplerDesc.AddressW = gl_d3d11::ConvertTextureWrap(samplerState.wrapR);
412         samplerDesc.MipLODBias = 0;
413         samplerDesc.MaxAnisotropy = samplerState.maxAnisotropy;
414         samplerDesc.ComparisonFunc = gl_d3d11::ConvertComparison(samplerState.compareFunc);
415         samplerDesc.BorderColor[0] = 0.0f;
416         samplerDesc.BorderColor[1] = 0.0f;
417         samplerDesc.BorderColor[2] = 0.0f;
418         samplerDesc.BorderColor[3] = 0.0f;
419         samplerDesc.MinLOD = samplerState.minLod;
420         samplerDesc.MaxLOD = samplerState.maxLod;
421
422         ID3D11SamplerState *dx11SamplerState = NULL;
423         HRESULT result = mDevice->CreateSamplerState(&samplerDesc, &dx11SamplerState);
424         if (FAILED(result) || !dx11SamplerState)
425         {
426             ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result);
427             return NULL;
428         }
429
430         mSamplerStateCache.insert(std::make_pair(samplerState, std::make_pair(dx11SamplerState, mCounter++)));
431
432         return dx11SamplerState;
433     }
434 }
435
436 }