1//
2// Copyright (c) 2012-2013 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 LIBGLESV2_RENDERER_RENDERSTATECACHE_H_
11#define LIBGLESV2_RENDERER_RENDERSTATECACHE_H_
12
13#include "libGLESv2/angletypes.h"
14#include "common/angleutils.h"
15
16namespace rx
17{
18
19class RenderStateCache
20{
21  public:
22    RenderStateCache();
23    virtual ~RenderStateCache();
24
25    void initialize(ID3D11Device *device);
26    void clear();
27
28    // Increments refcount on the returned blend state, Release() must be called.
29    ID3D11BlendState *getBlendState(const gl::BlendState &blendState);
30    ID3D11RasterizerState *getRasterizerState(const gl::RasterizerState &rasterState,
31                                              bool scissorEnabled, unsigned int depthSize);
32    ID3D11DepthStencilState *getDepthStencilState(const gl::DepthStencilState &dsState);
33    ID3D11SamplerState *getSamplerState(const gl::SamplerState &samplerState);
34
35  private:
36    DISALLOW_COPY_AND_ASSIGN(RenderStateCache);
37
38    unsigned long long mCounter;
39
40    // Blend state cache
41    static std::size_t hashBlendState(const gl::BlendState &blendState);
42    static bool compareBlendStates(const gl::BlendState &a, const gl::BlendState &b);
43    static const unsigned int kMaxBlendStates;
44
45    typedef std::size_t (*BlendStateHashFunction)(const gl::BlendState &);
46    typedef bool (*BlendStateEqualityFunction)(const gl::BlendState &, const gl::BlendState &);
47    typedef std::pair<ID3D11BlendState*, unsigned long long> BlendStateCounterPair;
48    typedef std::unordered_map<gl::BlendState, BlendStateCounterPair, BlendStateHashFunction, BlendStateEqualityFunction> BlendStateMap;
49    BlendStateMap mBlendStateCache;
50
51    // Rasterizer state cache
52    struct RasterizerStateKey
53    {
54        gl::RasterizerState rasterizerState;
55        bool scissorEnabled;
56        unsigned int depthSize;
57    };
58    static std::size_t hashRasterizerState(const RasterizerStateKey &rasterState);
59    static bool compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b);
60    static const unsigned int kMaxRasterizerStates;
61
62    typedef std::size_t (*RasterizerStateHashFunction)(const RasterizerStateKey &);
63    typedef bool (*RasterizerStateEqualityFunction)(const RasterizerStateKey &, const RasterizerStateKey &);
64    typedef std::pair<ID3D11RasterizerState*, unsigned long long> RasterizerStateCounterPair;
65    typedef std::unordered_map<RasterizerStateKey, RasterizerStateCounterPair, RasterizerStateHashFunction, RasterizerStateEqualityFunction> RasterizerStateMap;
66    RasterizerStateMap mRasterizerStateCache;
67
68    // Depth stencil state cache
69    static std::size_t hashDepthStencilState(const gl::DepthStencilState &dsState);
70    static bool compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b);
71    static const unsigned int kMaxDepthStencilStates;
72
73    typedef std::size_t (*DepthStencilStateHashFunction)(const gl::DepthStencilState &);
74    typedef bool (*DepthStencilStateEqualityFunction)(const gl::DepthStencilState &, const gl::DepthStencilState &);
75    typedef std::pair<ID3D11DepthStencilState*, unsigned long long> DepthStencilStateCounterPair;
76    typedef std::unordered_map<gl::DepthStencilState,
77                               DepthStencilStateCounterPair,
78                               DepthStencilStateHashFunction,
79                               DepthStencilStateEqualityFunction> DepthStencilStateMap;
80    DepthStencilStateMap mDepthStencilStateCache;
81
82    // Sample state cache
83    static std::size_t hashSamplerState(const gl::SamplerState &samplerState);
84    static bool compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b);
85    static const unsigned int kMaxSamplerStates;
86
87    typedef std::size_t (*SamplerStateHashFunction)(const gl::SamplerState &);
88    typedef bool (*SamplerStateEqualityFunction)(const gl::SamplerState &, const gl::SamplerState &);
89    typedef std::pair<ID3D11SamplerState*, unsigned long long> SamplerStateCounterPair;
90    typedef std::unordered_map<gl::SamplerState,
91                               SamplerStateCounterPair,
92                               SamplerStateHashFunction,
93                               SamplerStateEqualityFunction> SamplerStateMap;
94    SamplerStateMap mSamplerStateCache;
95
96    ID3D11Device *mDevice;
97};
98
99}
100
101#endif // LIBGLESV2_RENDERER_RENDERSTATECACHE_H_