gfx/angle/src/libGLESv2/renderer/ShaderCache.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/angle/src/libGLESv2/renderer/ShaderCache.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +//
     1.5 +// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
     1.6 +// Use of this source code is governed by a BSD-style license that can be
     1.7 +// found in the LICENSE file.
     1.8 +//
     1.9 +
    1.10 +// ShaderCache: Defines rx::ShaderCache, a cache of Direct3D shader objects
    1.11 +// keyed by their byte code.
    1.12 +
    1.13 +#ifndef LIBGLESV2_RENDERER_SHADER_CACHE_H_
    1.14 +#define LIBGLESV2_RENDERER_SHADER_CACHE_H_
    1.15 +
    1.16 +#include "common/debug.h"
    1.17 +
    1.18 +namespace rx
    1.19 +{
    1.20 +template <typename ShaderObject>
    1.21 +class ShaderCache
    1.22 +{
    1.23 +  public:
    1.24 +    ShaderCache() : mDevice(NULL)
    1.25 +    {
    1.26 +    }
    1.27 +
    1.28 +    ~ShaderCache()
    1.29 +    {
    1.30 +        // Call clear while the device is still valid.
    1.31 +        ASSERT(mMap.empty());
    1.32 +    }
    1.33 +
    1.34 +    void initialize(IDirect3DDevice9* device)
    1.35 +    {
    1.36 +        mDevice = device;
    1.37 +    }
    1.38 +
    1.39 +    ShaderObject *create(const DWORD *function, size_t length)
    1.40 +    {
    1.41 +        std::string key(reinterpret_cast<const char*>(function), length);
    1.42 +        typename Map::iterator it = mMap.find(key);
    1.43 +        if (it != mMap.end())
    1.44 +        {
    1.45 +            it->second->AddRef();
    1.46 +            return it->second;
    1.47 +        }
    1.48 +
    1.49 +        ShaderObject *shader;
    1.50 +        HRESULT result = createShader(function, &shader);
    1.51 +        if (FAILED(result))
    1.52 +        {
    1.53 +            return NULL;
    1.54 +        }
    1.55 +
    1.56 +        // Random eviction policy.
    1.57 +        if (mMap.size() >= kMaxMapSize)
    1.58 +        {
    1.59 +            mMap.begin()->second->Release();
    1.60 +            mMap.erase(mMap.begin());
    1.61 +        }
    1.62 +
    1.63 +        shader->AddRef();
    1.64 +        mMap[key] = shader;
    1.65 +
    1.66 +        return shader;
    1.67 +    }
    1.68 +
    1.69 +    void clear()
    1.70 +    {
    1.71 +        for (typename Map::iterator it = mMap.begin(); it != mMap.end(); ++it)
    1.72 +        {
    1.73 +            it->second->Release();
    1.74 +        }
    1.75 +
    1.76 +        mMap.clear();
    1.77 +    }
    1.78 +
    1.79 +  private:
    1.80 +    DISALLOW_COPY_AND_ASSIGN(ShaderCache);
    1.81 +
    1.82 +    const static size_t kMaxMapSize = 100;
    1.83 +
    1.84 +    HRESULT createShader(const DWORD *function, IDirect3DVertexShader9 **shader)
    1.85 +    {
    1.86 +        return mDevice->CreateVertexShader(function, shader);
    1.87 +    }
    1.88 +
    1.89 +    HRESULT createShader(const DWORD *function, IDirect3DPixelShader9 **shader)
    1.90 +    {
    1.91 +        return mDevice->CreatePixelShader(function, shader);
    1.92 +    }
    1.93 +
    1.94 +#ifndef HASH_MAP
    1.95 +# ifdef _MSC_VER
    1.96 +#  define HASH_MAP stdext::hash_map
    1.97 +# else
    1.98 +#  define HASH_MAP std::unordered_map
    1.99 +# endif
   1.100 +#endif
   1.101 +
   1.102 +    typedef HASH_MAP<std::string, ShaderObject*> Map;
   1.103 +    Map mMap;
   1.104 +
   1.105 +    IDirect3DDevice9 *mDevice;
   1.106 +};
   1.107 +
   1.108 +typedef ShaderCache<IDirect3DVertexShader9> VertexShaderCache;
   1.109 +typedef ShaderCache<IDirect3DPixelShader9> PixelShaderCache;
   1.110 +
   1.111 +}
   1.112 +
   1.113 +#endif   // LIBGLESV2_RENDERER_SHADER_CACHE_H_

mercurial