Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_program_cache_H_ | |
| 6 #define GPU_COMMAND_BUFFER_SERVICE_program_cache_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <map> | |
| 10 | |
| 11 #include "base/hash_tables.h" | |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/sha1.h" | |
| 14 #include "gpu/command_buffer/common/gles2_cmd_format.h" | |
| 15 #include "gpu/command_buffer/service/shader_manager.h" | |
| 16 | |
| 17 namespace gpu { | |
| 18 | |
| 19 namespace gles2 { | |
| 20 const uint32 kMaxProgramCacheSizeBytes = 20*1024*1024; | |
| 21 | |
| 22 struct CachedProgramKey { | |
| 23 CachedProgramKey() | |
| 24 : hashed_src_0(NULL), | |
| 25 hashed_src_1(NULL), | |
| 26 bind_attrib_location_map(NULL) { }; | |
| 27 CachedProgramKey( | |
| 28 const unsigned char* _hashed_src_0, | |
| 29 const unsigned char* _hashed_src_1, | |
| 30 const std::map<std::string, GLint>* _bind_attrib_location_map) | |
| 31 : hashed_src_0(_hashed_src_0), | |
| 32 hashed_src_1(_hashed_src_1), | |
| 33 bind_attrib_location_map(_bind_attrib_location_map) { | |
| 34 } | |
| 35 ; | |
| 36 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.
| |
| 37 const unsigned char* hashed_src_1; | |
| 38 const std::map<std::string, GLint>* bind_attrib_location_map; | |
| 39 }; | |
| 40 | |
| 41 namespace { | |
| 42 // sbdm hash function | |
| 43 inline uint32 sbdm(const unsigned char *str) { | |
| 44 unsigned int hash = 0; | |
| 45 int c; | |
| 46 while ((c = *str++)) | |
| 47 hash = c + (hash << 6) + (hash << 16) - hash; | |
| 48 return hash; | |
| 49 } | |
| 50 } // anonymous namespace | |
| 51 | |
| 52 struct CachedShaderKeyHash { | |
| 53 inline size_t operator()(const unsigned char* key) const { | |
| 54 return sbdm(key); | |
| 55 } | |
| 56 }; | |
| 57 | |
| 58 struct CachedShaderKeyEquals { | |
| 59 inline bool operator()(const unsigned char* a, const unsigned char* b) const { | |
| 60 return strcmp(reinterpret_cast<const char*>(a), | |
| 61 reinterpret_cast<const char*>(b)) == 0; | |
| 62 } | |
| 63 }; | |
| 64 | |
| 65 struct CachedProgramKeyHash { | |
| 66 inline size_t operator()(const CachedProgramKey& key) const { | |
| 67 uint32 hash = sbdm(key.hashed_src_0); | |
| 68 hash = (hash << 6) + (hash << 16) - hash + sbdm(key.hashed_src_1); | |
| 69 if(key.bind_attrib_location_map) { | |
| 70 std::map<std::string, GLint>::const_iterator it; | |
| 71 for(it = key.bind_attrib_location_map->begin(); | |
| 72 it != key.bind_attrib_location_map->end(); | |
| 73 it++) { | |
| 74 const unsigned char* key = | |
| 75 reinterpret_cast<const unsigned char*>(it->first.c_str()); | |
| 76 hash = (hash << 6) + (hash << 16) - hash + sbdm(key); | |
| 77 hash = (hash << 6) + (hash << 16) - hash + it->second; | |
| 78 } | |
| 79 } | |
| 80 return hash; | |
| 81 } | |
| 82 }; | |
| 83 | |
| 84 struct CachedProgramKeyEquals { | |
| 85 inline bool operator()(const CachedProgramKey& a, | |
| 86 const CachedProgramKey& b) const { | |
| 87 return strcmp(reinterpret_cast<const char*>(a.hashed_src_0), | |
| 88 reinterpret_cast<const char*>(b.hashed_src_0)) == 0 && | |
| 89 strcmp(reinterpret_cast<const char*>(a.hashed_src_1), | |
| 90 reinterpret_cast<const char*>(b.hashed_src_1)) == 0; | |
| 91 } | |
| 92 }; | |
| 93 | |
| 94 // Program cache base class for caching linked gpu programs | |
| 95 class GPU_EXPORT ProgramCache : public base::SupportsWeakPtr<ProgramCache>{ | |
| 96 public: | |
| 97 enum CompiledShaderStatus { | |
| 98 COMPILATION_UNKNOWN, | |
| 99 COMPILATION_SUCCEEDED | |
| 100 }; | |
| 101 | |
| 102 enum LinkedProgramStatus { | |
| 103 LINK_UNKNOWN, | |
| 104 LINK_SUCCEEDED | |
| 105 }; | |
| 106 | |
| 107 typedef base::hash_map<const unsigned char*, | |
| 108 CompiledShaderStatus, | |
| 109 CachedShaderKeyHash, | |
| 110 CachedShaderKeyEquals> compile_status_map; | |
| 111 typedef base::hash_map<CachedProgramKey, | |
| 112 LinkedProgramStatus, | |
| 113 CachedProgramKeyHash, | |
| 114 CachedProgramKeyEquals> link_status_map; | |
| 115 | |
| 116 ProgramCache() : max_bytes_(kMaxProgramCacheSizeBytes) {} | |
| 117 virtual ~ProgramCache(); | |
| 118 | |
| 119 CompiledShaderStatus GetShaderCompilationStatus( | |
| 120 const std::string& shader_src) const; | |
| 121 void SetShaderCompilationStatus(const std::string& shader_src, | |
| 122 CompiledShaderStatus status); | |
| 123 | |
| 124 LinkedProgramStatus GetLinkedProgramStatus( | |
| 125 const std::string& untranslated_a, | |
| 126 const std::string& untranslated_b, | |
| 127 std::map<std::string, GLint>* bind_attrib_location_map) const; | |
| 128 void SetLinkedProgramStatus( | |
| 129 const std::string& untranslated_a, | |
| 130 const std::string& untranslated_b, | |
| 131 std::map<std::string, GLint>* bind_attrib_location_map, | |
| 132 LinkedProgramStatus status); | |
| 133 | |
| 134 virtual void LoadLinkedProgram( | |
| 135 GLuint program, | |
| 136 ShaderManager::ShaderInfo* shader_a, | |
| 137 ShaderManager::ShaderInfo* shader_b, | |
| 138 std::map<std::string, GLint>* bind_attrib_location_map) const = 0; | |
| 139 virtual void SaveLinkedProgram( | |
| 140 GLuint program, | |
| 141 ShaderManager::ShaderInfo* shader_a, | |
| 142 ShaderManager::ShaderInfo* shader_b, | |
| 143 std::map<std::string, GLint>* bind_attrib_location_map) = 0; | |
| 144 | |
| 145 uint32 max_bytes() { return max_bytes_; } | |
| 146 | |
| 147 protected: | |
| 148 static const uint32 kHashLength = base::kSHA1Length + 1; | |
| 149 void computeHash(const std::string& str, unsigned char* hashResult) const; | |
| 150 | |
| 151 private: | |
| 152 const uint32 max_bytes_; | |
| 153 | |
| 154 compile_status_map shader_status_; | |
| 155 link_status_map link_status_; | |
| 156 | |
| 157 DISALLOW_COPY_AND_ASSIGN(ProgramCache); | |
| 158 }; | |
| 159 } // namespace gles2 | |
| 160 } // namespace gpu | |
| 161 | |
| 162 #endif // GPU_COMMAND_BUFFER_SERVICE_program_cache_H_ | |
| OLD | NEW |