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 LIBGLESV2_RENDERER_RENDERSTATECACHE_H_
11#define LIBGLESV2_RENDERER_RENDERSTATECACHE_H_
12
13#include "libGLESv2/angletypes.h"
14#include "libGLESv2/Error.h"
15#include "common/angleutils.h"
16
17#include <unordered_map>
18
19namespace gl
20{
21class Framebuffer;
22}
23
24namespace rx
25{
26class Renderer11;
27
28class RenderStateCache
29{
30  public:
31    RenderStateCache();
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, ID3D11DepthStencilState **outDSState);
40    gl::Error getSamplerState(const gl::SamplerState &samplerState, ID3D11SamplerState **outSamplerState);
41
42  private:
43    DISALLOW_COPY_AND_ASSIGN(RenderStateCache);
44
45    unsigned long long mCounter;
46
47    // Blend state cache
48    struct BlendStateKey
49    {
50        gl::BlendState blendState;
51        bool rtChannels[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT][4];
52    };
53    static std::size_t hashBlendState(const BlendStateKey &blendState);
54    static bool compareBlendStates(const BlendStateKey &a, const BlendStateKey &b);
55    static const unsigned int kMaxBlendStates;
56
57    typedef std::size_t (*BlendStateHashFunction)(const BlendStateKey &);
58    typedef bool (*BlendStateEqualityFunction)(const BlendStateKey &, const BlendStateKey &);
59    typedef std::pair<ID3D11BlendState*, unsigned long long> BlendStateCounterPair;
60    typedef std::unordered_map<BlendStateKey, BlendStateCounterPair, BlendStateHashFunction, BlendStateEqualityFunction> BlendStateMap;
61    BlendStateMap mBlendStateCache;
62
63    // Rasterizer state cache
64    struct RasterizerStateKey
65    {
66        gl::RasterizerState rasterizerState;
67        bool scissorEnabled;
68    };
69    static std::size_t hashRasterizerState(const RasterizerStateKey &rasterState);
70    static bool compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b);
71    static const unsigned int kMaxRasterizerStates;
72
73    typedef std::size_t (*RasterizerStateHashFunction)(const RasterizerStateKey &);
74    typedef bool (*RasterizerStateEqualityFunction)(const RasterizerStateKey &, const RasterizerStateKey &);
75    typedef std::pair<ID3D11RasterizerState*, unsigned long long> RasterizerStateCounterPair;
76    typedef std::unordered_map<RasterizerStateKey, RasterizerStateCounterPair, RasterizerStateHashFunction, RasterizerStateEqualityFunction> RasterizerStateMap;
77    RasterizerStateMap mRasterizerStateCache;
78
79    // Depth stencil state cache
80    static std::size_t hashDepthStencilState(const gl::DepthStencilState &dsState);
81    static bool compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b);
82    static const unsigned int kMaxDepthStencilStates;
83
84    typedef std::size_t (*DepthStencilStateHashFunction)(const gl::DepthStencilState &);
85    typedef bool (*DepthStencilStateEqualityFunction)(const gl::DepthStencilState &, const gl::DepthStencilState &);
86    typedef std::pair<ID3D11DepthStencilState*, unsigned long long> DepthStencilStateCounterPair;
87    typedef std::unordered_map<gl::DepthStencilState,
88                               DepthStencilStateCounterPair,
89                               DepthStencilStateHashFunction,
90                               DepthStencilStateEqualityFunction> DepthStencilStateMap;
91    DepthStencilStateMap mDepthStencilStateCache;
92
93    // Sample state cache
94    static std::size_t hashSamplerState(const gl::SamplerState &samplerState);
95    static bool compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b);
96    static const unsigned int kMaxSamplerStates;
97
98    typedef std::size_t (*SamplerStateHashFunction)(const gl::SamplerState &);
99    typedef bool (*SamplerStateEqualityFunction)(const gl::SamplerState &, const gl::SamplerState &);
100    typedef std::pair<ID3D11SamplerState*, unsigned long long> SamplerStateCounterPair;
101    typedef std::unordered_map<gl::SamplerState,
102                               SamplerStateCounterPair,
103                               SamplerStateHashFunction,
104                               SamplerStateEqualityFunction> SamplerStateMap;
105    SamplerStateMap mSamplerStateCache;
106
107    ID3D11Device *mDevice;
108};
109
110}
111
112#endif // LIBGLESV2_RENDERER_RENDERSTATECACHE_H_