Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 "gpu/command_buffer/service/program_cache.h" |
| 6 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 7 | #include <string> |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 9 | #include "gpu/command_buffer/service/shader_manager.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 10 | |
| 11 | namespace gpu { |
| 12 | namespace gles2 { |
| 13 | |
| 14 | ProgramCache::ProgramCache() {} |
| 15 | ProgramCache::~ProgramCache() {} |
| 16 | |
| 17 | void ProgramCache::Clear() { |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 18 | ClearBackend(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 19 | link_status_.clear(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 22 | ProgramCache::LinkedProgramStatus ProgramCache::GetLinkedProgramStatus( |
| 23 | const std::string& untranslated_a, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 24 | const ShaderTranslatorInterface* translator_a, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 25 | const std::string& untranslated_b, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 26 | const ShaderTranslatorInterface* translator_b, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 27 | const std::map<std::string, GLint>* bind_attrib_location_map) const { |
| 28 | char a_sha[kHashLength]; |
| 29 | char b_sha[kHashLength]; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 30 | ComputeShaderHash(untranslated_a, translator_a, a_sha); |
| 31 | ComputeShaderHash(untranslated_b, translator_b, b_sha); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 32 | |
| 33 | char sha[kHashLength]; |
| 34 | ComputeProgramHash(a_sha, |
| 35 | b_sha, |
| 36 | bind_attrib_location_map, |
| 37 | sha); |
| 38 | const std::string sha_string(sha, kHashLength); |
| 39 | |
| 40 | LinkStatusMap::const_iterator found = link_status_.find(sha_string); |
| 41 | if (found == link_status_.end()) { |
| 42 | return ProgramCache::LINK_UNKNOWN; |
| 43 | } else { |
| 44 | return found->second; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void ProgramCache::LinkedProgramCacheSuccess( |
| 49 | const std::string& shader_a, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 50 | const ShaderTranslatorInterface* translator_a, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 51 | const std::string& shader_b, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 52 | const ShaderTranslatorInterface* translator_b, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 53 | const LocationMap* bind_attrib_location_map) { |
| 54 | char a_sha[kHashLength]; |
| 55 | char b_sha[kHashLength]; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 56 | ComputeShaderHash(shader_a, translator_a, a_sha); |
| 57 | ComputeShaderHash(shader_b, translator_b, b_sha); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 58 | char sha[kHashLength]; |
| 59 | ComputeProgramHash(a_sha, |
| 60 | b_sha, |
| 61 | bind_attrib_location_map, |
| 62 | sha); |
| 63 | const std::string sha_string(sha, kHashLength); |
| 64 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 65 | LinkedProgramCacheSuccess(sha_string); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 68 | void ProgramCache::LinkedProgramCacheSuccess(const std::string& program_hash) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 69 | link_status_[program_hash] = LINK_SUCCEEDED; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 72 | void ProgramCache::ComputeShaderHash( |
| 73 | const std::string& str, |
| 74 | const ShaderTranslatorInterface* translator, |
| 75 | char* result) const { |
| 76 | std::string s(( |
| 77 | translator ? translator->GetStringForOptionsThatWouldEffectCompilation() : |
| 78 | std::string()) + str); |
| 79 | base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(s.c_str()), |
| 80 | s.length(), reinterpret_cast<unsigned char*>(result)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 83 | void ProgramCache::Evict(const std::string& program_hash) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 84 | link_status_.erase(program_hash); |
| 85 | } |
| 86 | |
| 87 | namespace { |
| 88 | size_t CalculateMapSize(const std::map<std::string, GLint>* map) { |
| 89 | if (!map) { |
| 90 | return 0; |
| 91 | } |
| 92 | std::map<std::string, GLint>::const_iterator it; |
| 93 | size_t total = 0; |
| 94 | for (it = map->begin(); it != map->end(); ++it) { |
| 95 | total += 4 + it->first.length(); |
| 96 | } |
| 97 | return total; |
| 98 | } |
| 99 | } // anonymous namespace |
| 100 | |
| 101 | void ProgramCache::ComputeProgramHash( |
| 102 | const char* hashed_shader_0, |
| 103 | const char* hashed_shader_1, |
| 104 | const std::map<std::string, GLint>* bind_attrib_location_map, |
| 105 | char* result) const { |
| 106 | const size_t shader0_size = kHashLength; |
| 107 | const size_t shader1_size = kHashLength; |
| 108 | const size_t map_size = CalculateMapSize(bind_attrib_location_map); |
| 109 | const size_t total_size = shader0_size + shader1_size + map_size; |
| 110 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 111 | scoped_ptr<unsigned char[]> buffer(new unsigned char[total_size]); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 112 | memcpy(buffer.get(), hashed_shader_0, shader0_size); |
| 113 | memcpy(&buffer[shader0_size], hashed_shader_1, shader1_size); |
| 114 | if (map_size != 0) { |
| 115 | // copy our map |
| 116 | size_t current_pos = shader0_size + shader1_size; |
| 117 | std::map<std::string, GLint>::const_iterator it; |
| 118 | for (it = bind_attrib_location_map->begin(); |
| 119 | it != bind_attrib_location_map->end(); |
| 120 | ++it) { |
| 121 | const size_t name_size = it->first.length(); |
| 122 | memcpy(&buffer.get()[current_pos], it->first.c_str(), name_size); |
| 123 | current_pos += name_size; |
| 124 | const GLint value = it->second; |
| 125 | buffer[current_pos++] = value >> 24; |
| 126 | buffer[current_pos++] = value >> 16; |
| 127 | buffer[current_pos++] = value >> 8; |
| 128 | buffer[current_pos++] = value; |
| 129 | } |
| 130 | } |
| 131 | base::SHA1HashBytes(buffer.get(), |
| 132 | total_size, reinterpret_cast<unsigned char*>(result)); |
| 133 | } |
| 134 | |
| 135 | } // namespace gles2 |
| 136 | } // namespace gpu |