1 //
2 // Copyright (c) 2012-2014 The ANGLE Project Authors. All rights reserved.
3 // 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.h: Defines rx::RenderStateCache, a cache of Direct3D render
8 // state objects.
9 
10 #ifndef LIBANGLE_RENDERER_D3D_D3D11_RENDERSTATECACHE_H_
11 #define LIBANGLE_RENDERER_D3D_D3D11_RENDERSTATECACHE_H_
12 
13 #include "libANGLE/angletypes.h"
14 #include "libANGLE/Error.h"
15 #include "common/angleutils.h"
16 
17 #include <unordered_map>
18 
19 namespace gl
20 {
21 class Framebuffer;
22 }
23 
24 namespace rx
25 {
26 class Renderer11;
27 
28 class RenderStateCache : angle::NonCopyable
29 {
30   public:
31     RenderStateCache(Renderer11 *renderer);
32     virtual ~RenderStateCache();
33 
34     void initialize(ID3D11Device *device);
35     void clear();
36 
37     gl::Error getBlendState(const gl::Framebuffer *framebuffer, const gl::BlendState &blendState, ID3D11BlendState **outBlendState);
38     gl::Error getRasterizerState(const gl::RasterizerState &rasterState, bool scissorEnabled, ID3D11RasterizerState **outRasterizerState);
39     gl::Error getDepthStencilState(const gl::DepthStencilState &dsState,
40                                    bool disableDepth,
41                                    bool disableStencil,
42                                    ID3D11DepthStencilState **outDSState);
43     gl::Error getSamplerState(const gl::SamplerState &samplerState, ID3D11SamplerState **outSamplerState);
44 
45   private:
46     Renderer11 *mRenderer;
47     unsigned long long mCounter;
48 
49     // Blend state cache
50     struct BlendStateKey
51     {
52         gl::BlendState blendState;
53         bool rtChannels[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT][4];
54     };
55     static std::size_t hashBlendState(const BlendStateKey &blendState);
56     static bool compareBlendStates(const BlendStateKey &a, const BlendStateKey &b);
57     static const unsigned int kMaxBlendStates;
58 
59     typedef std::size_t (*BlendStateHashFunction)(const BlendStateKey &);
60     typedef bool (*BlendStateEqualityFunction)(const BlendStateKey &, const BlendStateKey &);
61     typedef std::pair<ID3D11BlendState*, unsigned long long> BlendStateCounterPair;
62     typedef std::unordered_map<BlendStateKey, BlendStateCounterPair, BlendStateHashFunction, BlendStateEqualityFunction> BlendStateMap;
63     BlendStateMap mBlendStateCache;
64 
65     // Rasterizer state cache
66     struct RasterizerStateKey
67     {
68         gl::RasterizerState rasterizerState;
69         bool scissorEnabled;
70     };
71     static std::size_t hashRasterizerState(const RasterizerStateKey &rasterState);
72     static bool compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b);
73     static const unsigned int kMaxRasterizerStates;
74 
75     typedef std::size_t (*RasterizerStateHashFunction)(const RasterizerStateKey &);
76     typedef bool (*RasterizerStateEqualityFunction)(const RasterizerStateKey &, const RasterizerStateKey &);
77     typedef std::pair<ID3D11RasterizerState*, unsigned long long> RasterizerStateCounterPair;
78     typedef std::unordered_map<RasterizerStateKey, RasterizerStateCounterPair, RasterizerStateHashFunction, RasterizerStateEqualityFunction> RasterizerStateMap;
79     RasterizerStateMap mRasterizerStateCache;
80 
81     // Depth stencil state cache
82     static std::size_t hashDepthStencilState(const gl::DepthStencilState &dsState);
83     static bool compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b);
84     static const unsigned int kMaxDepthStencilStates;
85 
86     typedef std::size_t (*DepthStencilStateHashFunction)(const gl::DepthStencilState &);
87     typedef bool (*DepthStencilStateEqualityFunction)(const gl::DepthStencilState &, const gl::DepthStencilState &);
88     typedef std::pair<ID3D11DepthStencilState*, unsigned long long> DepthStencilStateCounterPair;
89     typedef std::unordered_map<gl::DepthStencilState,
90                                DepthStencilStateCounterPair,
91                                DepthStencilStateHashFunction,
92                                DepthStencilStateEqualityFunction> DepthStencilStateMap;
93     DepthStencilStateMap mDepthStencilStateCache;
94 
95     // Sample state cache
96     static std::size_t hashSamplerState(const gl::SamplerState &samplerState);
97     static bool compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b);
98     static const unsigned int kMaxSamplerStates;
99 
100     typedef std::size_t (*SamplerStateHashFunction)(const gl::SamplerState &);
101     typedef bool (*SamplerStateEqualityFunction)(const gl::SamplerState &, const gl::SamplerState &);
102     typedef std::pair<ID3D11SamplerState*, unsigned long long> SamplerStateCounterPair;
103     typedef std::unordered_map<gl::SamplerState,
104                                SamplerStateCounterPair,
105                                SamplerStateHashFunction,
106                                SamplerStateEqualityFunction> SamplerStateMap;
107     SamplerStateMap mSamplerStateCache;
108 
109     ID3D11Device *mDevice;
110 };
111 
112 }
113 
114 #endif // LIBANGLE_RENDERER_D3D_D3D11_RENDERSTATECACHE_H_
115