1 // 2 // Copyright 2012 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 "common/angleutils.h" 14 #include "libANGLE/Error.h" 15 #include "libANGLE/SizedMRUCache.h" 16 #include "libANGLE/angletypes.h" 17 #include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h" 18 19 #include <unordered_map> 20 21 namespace std 22 { 23 template <> 24 struct hash<rx::d3d11::BlendStateKey> 25 { 26 size_t operator()(const rx::d3d11::BlendStateKey &key) const 27 { 28 return angle::ComputeGenericHash(key); 29 } 30 }; 31 32 template <> 33 struct hash<rx::d3d11::RasterizerStateKey> 34 { 35 size_t operator()(const rx::d3d11::RasterizerStateKey &key) const 36 { 37 return angle::ComputeGenericHash(key); 38 } 39 }; 40 41 template <> 42 struct hash<gl::DepthStencilState> 43 { 44 size_t operator()(const gl::DepthStencilState &key) const 45 { 46 return angle::ComputeGenericHash(key); 47 } 48 }; 49 50 template <> 51 struct hash<gl::SamplerState> 52 { 53 size_t operator()(const gl::SamplerState &key) const { return angle::ComputeGenericHash(key); } 54 }; 55 } // namespace std 56 57 namespace rx 58 { 59 class Framebuffer11; 60 class Renderer11; 61 62 class RenderStateCache : angle::NonCopyable 63 { 64 public: 65 RenderStateCache(); 66 virtual ~RenderStateCache(); 67 68 void clear(); 69 70 static d3d11::BlendStateKey GetBlendStateKey(const gl::Context *context, 71 Framebuffer11 *framebuffer11, 72 const gl::BlendStateExt &blendStateExt, 73 bool sampleAlphaToCoverage); 74 angle::Result getBlendState(const gl::Context *context, 75 Renderer11 *renderer, 76 const d3d11::BlendStateKey &key, 77 const d3d11::BlendState **outBlendState); 78 angle::Result getRasterizerState(const gl::Context *context, 79 Renderer11 *renderer, 80 const gl::RasterizerState &rasterState, 81 bool scissorEnabled, 82 ID3D11RasterizerState **outRasterizerState); 83 angle::Result getDepthStencilState(const gl::Context *context, 84 Renderer11 *renderer, 85 const gl::DepthStencilState &dsState, 86 const d3d11::DepthStencilState **outDSState); 87 angle::Result getSamplerState(const gl::Context *context, 88 Renderer11 *renderer, 89 const gl::SamplerState &samplerState, 90 ID3D11SamplerState **outSamplerState); 91 92 private: 93 // MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState, 94 // ID3D11Device::CreateDepthStencilState and ID3D11Device::CreateSamplerState claims the maximum 95 // number of unique states of each type an application can create is 4096 96 // TODO(ShahmeerEsmail): Revisit the cache sizes to make sure they are appropriate for most 97 // scenarios. 98 static constexpr unsigned int kMaxStates = 4096; 99 100 // The cache tries to clean up this many states at once. 101 static constexpr unsigned int kGCLimit = 128; 102 103 // Blend state cache 104 using BlendStateMap = angle::base::HashingMRUCache<d3d11::BlendStateKey, d3d11::BlendState>; 105 BlendStateMap mBlendStateCache; 106 107 // Rasterizer state cache 108 using RasterizerStateMap = 109 angle::base::HashingMRUCache<d3d11::RasterizerStateKey, d3d11::RasterizerState>; 110 RasterizerStateMap mRasterizerStateCache; 111 112 // Depth stencil state cache 113 using DepthStencilStateMap = 114 angle::base::HashingMRUCache<gl::DepthStencilState, d3d11::DepthStencilState>; 115 DepthStencilStateMap mDepthStencilStateCache; 116 117 // Sample state cache 118 using SamplerStateMap = angle::base::HashingMRUCache<gl::SamplerState, d3d11::SamplerState>; 119 SamplerStateMap mSamplerStateCache; 120 }; 121 122 } // namespace rx 123 124 #endif // LIBANGLE_RENDERER_D3D_D3D11_RENDERSTATECACHE_H_ 125