Chromium Code Reviews| Index: gpu/command_buffer/service/program_cache.h |
| diff --git a/gpu/command_buffer/service/program_cache.h b/gpu/command_buffer/service/program_cache.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..74c37423f4ad2f452766acf4842d60f8f1794118 |
| --- /dev/null |
| +++ b/gpu/command_buffer/service/program_cache.h |
| @@ -0,0 +1,162 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef GPU_COMMAND_BUFFER_SERVICE_program_cache_H_ |
| +#define GPU_COMMAND_BUFFER_SERVICE_program_cache_H_ |
| +#pragma once |
| + |
| +#include <map> |
| + |
| +#include "base/hash_tables.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/sha1.h" |
| +#include "gpu/command_buffer/common/gles2_cmd_format.h" |
| +#include "gpu/command_buffer/service/shader_manager.h" |
| + |
| +namespace gpu { |
| + |
| +namespace gles2 { |
| +const uint32 kMaxProgramCacheSizeBytes = 20*1024*1024; |
| + |
| +struct CachedProgramKey { |
| + CachedProgramKey() |
| + : hashed_src_0(NULL), |
| + hashed_src_1(NULL), |
| + bind_attrib_location_map(NULL) { }; |
| + CachedProgramKey( |
| + const unsigned char* _hashed_src_0, |
| + const unsigned char* _hashed_src_1, |
| + const std::map<std::string, GLint>* _bind_attrib_location_map) |
| + : hashed_src_0(_hashed_src_0), |
| + hashed_src_1(_hashed_src_1), |
| + bind_attrib_location_map(_bind_attrib_location_map) { |
| + } |
| + ; |
| + const unsigned char* hashed_src_0; |
|
greggman
2012/06/19 22:27:50
what's the point in these being char* instead of s
dmurph
2012/06/23 01:37:28
Done.
|
| + const unsigned char* hashed_src_1; |
| + const std::map<std::string, GLint>* bind_attrib_location_map; |
| +}; |
| + |
| +namespace { |
| +// sbdm hash function |
| +inline uint32 sbdm(const unsigned char *str) { |
| + unsigned int hash = 0; |
| + int c; |
| + while ((c = *str++)) |
| + hash = c + (hash << 6) + (hash << 16) - hash; |
| + return hash; |
| +} |
| +} // anonymous namespace |
| + |
| +struct CachedShaderKeyHash { |
| + inline size_t operator()(const unsigned char* key) const { |
| + return sbdm(key); |
| + } |
| +}; |
| + |
| +struct CachedShaderKeyEquals { |
| + inline bool operator()(const unsigned char* a, const unsigned char* b) const { |
| + return strcmp(reinterpret_cast<const char*>(a), |
| + reinterpret_cast<const char*>(b)) == 0; |
| + } |
| +}; |
| + |
| +struct CachedProgramKeyHash { |
| + inline size_t operator()(const CachedProgramKey& key) const { |
| + uint32 hash = sbdm(key.hashed_src_0); |
| + hash = (hash << 6) + (hash << 16) - hash + sbdm(key.hashed_src_1); |
| + if(key.bind_attrib_location_map) { |
| + std::map<std::string, GLint>::const_iterator it; |
| + for(it = key.bind_attrib_location_map->begin(); |
| + it != key.bind_attrib_location_map->end(); |
| + it++) { |
| + const unsigned char* key = |
| + reinterpret_cast<const unsigned char*>(it->first.c_str()); |
| + hash = (hash << 6) + (hash << 16) - hash + sbdm(key); |
| + hash = (hash << 6) + (hash << 16) - hash + it->second; |
| + } |
| + } |
| + return hash; |
| + } |
| +}; |
| + |
| +struct CachedProgramKeyEquals { |
| + inline bool operator()(const CachedProgramKey& a, |
| + const CachedProgramKey& b) const { |
| + return strcmp(reinterpret_cast<const char*>(a.hashed_src_0), |
| + reinterpret_cast<const char*>(b.hashed_src_0)) == 0 && |
| + strcmp(reinterpret_cast<const char*>(a.hashed_src_1), |
| + reinterpret_cast<const char*>(b.hashed_src_1)) == 0; |
| + } |
| +}; |
| + |
| +// Program cache base class for caching linked gpu programs |
| +class GPU_EXPORT ProgramCache : public base::SupportsWeakPtr<ProgramCache>{ |
| + public: |
| + enum CompiledShaderStatus { |
| + COMPILATION_UNKNOWN, |
| + COMPILATION_SUCCEEDED |
| + }; |
| + |
| + enum LinkedProgramStatus { |
| + LINK_UNKNOWN, |
| + LINK_SUCCEEDED |
| + }; |
| + |
| + typedef base::hash_map<const unsigned char*, |
| + CompiledShaderStatus, |
| + CachedShaderKeyHash, |
| + CachedShaderKeyEquals> compile_status_map; |
| + typedef base::hash_map<CachedProgramKey, |
| + LinkedProgramStatus, |
| + CachedProgramKeyHash, |
| + CachedProgramKeyEquals> link_status_map; |
| + |
| + ProgramCache() : max_bytes_(kMaxProgramCacheSizeBytes) {} |
| + virtual ~ProgramCache(); |
| + |
| + CompiledShaderStatus GetShaderCompilationStatus( |
| + const std::string& shader_src) const; |
| + void SetShaderCompilationStatus(const std::string& shader_src, |
| + CompiledShaderStatus status); |
| + |
| + LinkedProgramStatus GetLinkedProgramStatus( |
| + const std::string& untranslated_a, |
| + const std::string& untranslated_b, |
| + std::map<std::string, GLint>* bind_attrib_location_map) const; |
| + void SetLinkedProgramStatus( |
| + const std::string& untranslated_a, |
| + const std::string& untranslated_b, |
| + std::map<std::string, GLint>* bind_attrib_location_map, |
| + LinkedProgramStatus status); |
| + |
| + virtual void LoadLinkedProgram( |
| + GLuint program, |
| + ShaderManager::ShaderInfo* shader_a, |
| + ShaderManager::ShaderInfo* shader_b, |
| + std::map<std::string, GLint>* bind_attrib_location_map) const = 0; |
| + virtual void SaveLinkedProgram( |
| + GLuint program, |
| + ShaderManager::ShaderInfo* shader_a, |
| + ShaderManager::ShaderInfo* shader_b, |
| + std::map<std::string, GLint>* bind_attrib_location_map) = 0; |
| + |
| + uint32 max_bytes() { return max_bytes_; } |
| + |
| + protected: |
| + static const uint32 kHashLength = base::kSHA1Length + 1; |
| + void computeHash(const std::string& str, unsigned char* hashResult) const; |
| + |
| + private: |
| + const uint32 max_bytes_; |
| + |
| + compile_status_map shader_status_; |
| + link_status_map link_status_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ProgramCache); |
| +}; |
| +} // namespace gles2 |
| +} // namespace gpu |
| + |
| +#endif // GPU_COMMAND_BUFFER_SERVICE_program_cache_H_ |