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 #include "program_cache.h" | |
| 6 | |
| 7 #include "base/sha1.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 | |
| 10 namespace gpu { | |
| 11 namespace gles2 { | |
| 12 | |
| 13 namespace { | |
| 14 typedef std::map<std::string, GLint> BindAttribMap; | |
| 15 } // anonymous namespace | |
| 16 | |
| 17 | |
| 18 ProgramCache::~ProgramCache() {} | |
| 19 | |
| 20 void ProgramCache::Clear() { | |
| 21 shader_status_.clear(); | |
| 22 link_status_.clear(); | |
| 23 ClearBackend(); | |
| 24 } | |
| 25 | |
| 26 ProgramCache::CompiledShaderStatus ProgramCache::GetShaderCompilationStatus( | |
| 27 const std::string& shader_src) const { | |
| 28 | |
| 29 char sha[kHashLength]; | |
| 30 ComputeShaderHash(shader_src, sha); | |
| 31 const std::string shaString(sha, kHashLength); | |
| 32 | |
| 33 CompileStatusMap::const_iterator found = shader_status_.find(shaString); | |
| 34 | |
| 35 if (found == shader_status_.end()) { | |
| 36 return ProgramCache::COMPILATION_UNKNOWN; | |
| 37 } else { | |
| 38 return found->second.status; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 void ProgramCache::ShaderCompilationSucceeded( | |
| 43 const std::string& shader_src) { | |
| 44 char sha[kHashLength]; | |
| 45 ComputeShaderHash(shader_src, sha); | |
| 46 const std::string shaString(sha, kHashLength); | |
| 47 | |
| 48 shader_status_[shaString] = CompiledShaderInfo(COMPILATION_SUCCEEDED); | |
| 49 } | |
| 50 | |
| 51 ProgramCache::LinkedProgramStatus ProgramCache::GetLinkedProgramStatus( | |
| 52 const std::string& untranslated_a, | |
| 53 const std::string& untranslated_b, | |
| 54 const BindAttribMap* bind_attrib_location_map) const { | |
| 55 char a_sha[kHashLength]; | |
| 56 char b_sha[kHashLength]; | |
| 57 ComputeShaderHash(untranslated_a, a_sha); | |
| 58 ComputeShaderHash(untranslated_b, b_sha); | |
| 59 | |
| 60 char sha[kHashLength]; | |
| 61 ComputeProgramHash(a_sha, | |
| 62 b_sha, | |
| 63 bind_attrib_location_map, | |
| 64 sha); | |
| 65 const std::string shaString(sha, kHashLength); | |
| 66 | |
| 67 LinkStatusMap::const_iterator found = link_status_.find(shaString); | |
| 68 if (found == link_status_.end()) { | |
| 69 return ProgramCache::LINK_UNKNOWN; | |
| 70 } else { | |
| 71 return found->second; | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 void ProgramCache::LinkedProgramCacheSuccess(const std::string& program_hash, | |
| 76 const std::string& shader_a_hash, | |
| 77 const std::string& shader_b_hash) { | |
| 78 link_status_[program_hash] = LINK_SUCCEEDED; | |
| 79 shader_status_[shader_a_hash].usedInCachedProgram(); | |
| 80 shader_status_[shader_b_hash].usedInCachedProgram(); | |
| 81 } | |
| 82 | |
| 83 void ProgramCache::ComputeShaderHash(const std::string& str, | |
| 84 char* result) const { | |
| 85 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()), | |
| 86 str.length(), reinterpret_cast<unsigned char*>(result)); | |
| 87 } | |
| 88 | |
| 89 void ProgramCache::Evict(const std::string& program_hash, | |
| 90 const std::string& shader_0_hash, | |
| 91 const std::string& shader_1_hash) { | |
| 92 CompiledShaderInfo info0 = shader_status_[shader_0_hash]; | |
| 93 CompiledShaderInfo info1 = shader_status_[shader_1_hash]; | |
| 94 if(--info0.ref_count <= 0) { | |
| 95 shader_status_.erase(shader_0_hash); | |
| 96 } else { | |
| 97 shader_status_[shader_0_hash] = info0; | |
| 98 } | |
| 99 if(--info1.ref_count <= 0) { | |
| 100 shader_status_.erase(shader_1_hash); | |
| 101 } else { | |
| 102 shader_status_[shader_1_hash] = info1; | |
| 103 } | |
| 104 link_status_.erase(program_hash); | |
| 105 } | |
| 106 | |
| 107 namespace { | |
| 108 uint32 calculateMapSize(const BindAttribMap* map) { | |
| 109 if (!map) { | |
| 110 return 0; | |
| 111 } | |
| 112 BindAttribMap::const_iterator it; | |
| 113 uint32 total = 0; | |
| 114 for(it = map->begin(); it != map->end(); ++it) { | |
| 115 total += 4 + it->first.length(); | |
| 116 } | |
| 117 return total; | |
| 118 } | |
| 119 } // anonymous namespace | |
| 120 | |
| 121 void ProgramCache::ComputeProgramHash( | |
| 122 const char* hashed_shader_0, | |
| 123 const char* hashed_shader_1, | |
| 124 const BindAttribMap* bind_attrib_location_map, | |
| 125 char* result) const { | |
| 126 const uint32 shader0Size = kHashLength; | |
| 127 const uint32 shader1Size = kHashLength; | |
| 128 const uint32 mapSize = calculateMapSize(bind_attrib_location_map); | |
| 129 const uint32 totalSize = shader0Size + shader1Size + mapSize; | |
| 130 | |
| 131 scoped_array<char> buffer(new char[totalSize]); | |
| 132 memcpy(buffer.get(), hashed_shader_0, shader0Size); | |
| 133 memcpy(&buffer.get()[shader0Size], hashed_shader_1, shader1Size); | |
| 134 if(mapSize != 0) { | |
| 135 // copy our map | |
| 136 uint32 currPos = shader0Size + shader1Size; | |
| 137 BindAttribMap::const_iterator it; | |
| 138 for(it = bind_attrib_location_map->begin(); | |
| 139 it != bind_attrib_location_map->end(); | |
| 140 ++it) { | |
| 141 const uint32 nameSize = it->first.length(); | |
| 142 memcpy(&buffer.get()[currPos], it->first.c_str(), nameSize); | |
| 143 currPos += nameSize; | |
| 144 const GLint value = it->second; | |
| 145 buffer.get()[currPos++] = (char) (value >> 12); | |
|
greggman
2012/06/25 18:53:03
Is there a reason you're doing 4 bits at a time in
dmurph
2012/06/26 02:32:56
whoops
| |
| 146 buffer.get()[currPos++] = (char) (value >> 8); | |
| 147 buffer.get()[currPos++] = (char) (value >> 4); | |
| 148 buffer.get()[currPos++] = (char) value; | |
| 149 } | |
| 150 } | |
| 151 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(buffer.get()), | |
| 152 totalSize, reinterpret_cast<unsigned char*>(result)); | |
| 153 } | |
| 154 | |
| 155 } // namespace gles2 | |
| 156 } // namespace gpu | |
| OLD | NEW |