apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 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 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 7 | // ShaderCache: Defines rx::ShaderCache, a cache of Direct3D shader objects |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 8 | // keyed by their byte code. |
| 9 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 10 | #ifndef LIBGLESV2_RENDERER_SHADER_CACHE_H_ |
| 11 | #define LIBGLESV2_RENDERER_SHADER_CACHE_H_ |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 12 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 13 | #include "common/debug.h" |
| 14 | |
daniel@transgaming.com | 76d3e6e | 2012-10-31 19:55:33 +0000 | [diff] [blame] | 15 | namespace rx |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 16 | { |
| 17 | template <typename ShaderObject> |
| 18 | class ShaderCache |
| 19 | { |
| 20 | public: |
| 21 | ShaderCache() : mDevice(NULL) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | ~ShaderCache() |
| 26 | { |
| 27 | // Call clear while the device is still valid. |
| 28 | ASSERT(mMap.empty()); |
| 29 | } |
| 30 | |
| 31 | void initialize(IDirect3DDevice9* device) |
| 32 | { |
| 33 | mDevice = device; |
| 34 | } |
| 35 | |
| 36 | ShaderObject *create(const DWORD *function, size_t length) |
| 37 | { |
| 38 | std::string key(reinterpret_cast<const char*>(function), length); |
daniel@transgaming.com | 29ab952 | 2012-08-27 16:25:37 +0000 | [diff] [blame] | 39 | typename Map::iterator it = mMap.find(key); |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 40 | if (it != mMap.end()) |
| 41 | { |
| 42 | it->second->AddRef(); |
| 43 | return it->second; |
| 44 | } |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 45 | |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 46 | ShaderObject *shader; |
| 47 | HRESULT result = createShader(function, &shader); |
| 48 | if (FAILED(result)) |
| 49 | { |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | // Random eviction policy. |
| 54 | if (mMap.size() >= kMaxMapSize) |
| 55 | { |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame^] | 56 | SafeRelease(mMap.begin()->second); |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 57 | mMap.erase(mMap.begin()); |
| 58 | } |
| 59 | |
| 60 | shader->AddRef(); |
| 61 | mMap[key] = shader; |
| 62 | |
| 63 | return shader; |
| 64 | } |
| 65 | |
| 66 | void clear() |
| 67 | { |
daniel@transgaming.com | 29ab952 | 2012-08-27 16:25:37 +0000 | [diff] [blame] | 68 | for (typename Map::iterator it = mMap.begin(); it != mMap.end(); ++it) |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 69 | { |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame^] | 70 | SafeRelease(it->second); |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | mMap.clear(); |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | DISALLOW_COPY_AND_ASSIGN(ShaderCache); |
| 78 | |
| 79 | const static size_t kMaxMapSize = 100; |
| 80 | |
| 81 | HRESULT createShader(const DWORD *function, IDirect3DVertexShader9 **shader) |
| 82 | { |
| 83 | return mDevice->CreateVertexShader(function, shader); |
| 84 | } |
| 85 | |
| 86 | HRESULT createShader(const DWORD *function, IDirect3DPixelShader9 **shader) |
| 87 | { |
| 88 | return mDevice->CreatePixelShader(function, shader); |
| 89 | } |
| 90 | |
daniel@transgaming.com | db2115d | 2012-08-27 16:25:33 +0000 | [diff] [blame] | 91 | #ifndef HASH_MAP |
| 92 | # ifdef _MSC_VER |
| 93 | # define HASH_MAP stdext::hash_map |
| 94 | # else |
| 95 | # define HASH_MAP std::unordered_map |
| 96 | # endif |
| 97 | #endif |
| 98 | |
| 99 | typedef HASH_MAP<std::string, ShaderObject*> Map; |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 100 | Map mMap; |
| 101 | |
| 102 | IDirect3DDevice9 *mDevice; |
| 103 | }; |
| 104 | |
| 105 | typedef ShaderCache<IDirect3DVertexShader9> VertexShaderCache; |
| 106 | typedef ShaderCache<IDirect3DPixelShader9> PixelShaderCache; |
| 107 | |
| 108 | } |
| 109 | |
daniel@transgaming.com | e4733d7 | 2012-10-31 18:07:01 +0000 | [diff] [blame] | 110 | #endif // LIBGLESV2_RENDERER_SHADER_CACHE_H_ |