| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "gpu/command_buffer/service/program_cache.h" | 5 #include "gpu/command_buffer/service/program_cache.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "gpu/command_buffer/service/shader_manager.h" | 9 #include "gpu/command_buffer/service/shader_manager.h" |
| 10 | 10 |
| 11 namespace gpu { | 11 namespace gpu { |
| 12 namespace gles2 { | 12 namespace gles2 { |
| 13 | 13 |
| 14 ProgramCache::ProgramCache() {} | 14 ProgramCache::ProgramCache() {} |
| 15 ProgramCache::~ProgramCache() {} | 15 ProgramCache::~ProgramCache() {} |
| 16 | 16 |
| 17 void ProgramCache::Clear() { | 17 void ProgramCache::Clear() { |
| 18 ClearBackend(); | 18 ClearBackend(); |
| 19 link_status_.clear(); | 19 link_status_.clear(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 ProgramCache::LinkedProgramStatus ProgramCache::GetLinkedProgramStatus( | 22 ProgramCache::LinkedProgramStatus ProgramCache::GetLinkedProgramStatus( |
| 23 const std::string& shader_signature_a, | 23 const std::string& shader_signature_a, |
| 24 const std::string& shader_signature_b, | 24 const std::string& shader_signature_b, |
| 25 const std::map<std::string, GLint>* bind_attrib_location_map) const { | 25 const std::map<std::string, GLint>* bind_attrib_location_map, |
| 26 const std::vector<std::string>& transform_feedback_varyings, |
| 27 GLenum transform_feedback_buffer_mode) const { |
| 26 char a_sha[kHashLength]; | 28 char a_sha[kHashLength]; |
| 27 char b_sha[kHashLength]; | 29 char b_sha[kHashLength]; |
| 28 ComputeShaderHash(shader_signature_a, a_sha); | 30 ComputeShaderHash(shader_signature_a, a_sha); |
| 29 ComputeShaderHash(shader_signature_b, b_sha); | 31 ComputeShaderHash(shader_signature_b, b_sha); |
| 30 | 32 |
| 31 char sha[kHashLength]; | 33 char sha[kHashLength]; |
| 32 ComputeProgramHash(a_sha, | 34 ComputeProgramHash(a_sha, |
| 33 b_sha, | 35 b_sha, |
| 34 bind_attrib_location_map, | 36 bind_attrib_location_map, |
| 37 transform_feedback_varyings, |
| 38 transform_feedback_buffer_mode, |
| 35 sha); | 39 sha); |
| 36 const std::string sha_string(sha, kHashLength); | 40 const std::string sha_string(sha, kHashLength); |
| 37 | 41 |
| 38 LinkStatusMap::const_iterator found = link_status_.find(sha_string); | 42 LinkStatusMap::const_iterator found = link_status_.find(sha_string); |
| 39 if (found == link_status_.end()) { | 43 if (found == link_status_.end()) { |
| 40 return ProgramCache::LINK_UNKNOWN; | 44 return ProgramCache::LINK_UNKNOWN; |
| 41 } else { | 45 } else { |
| 42 return found->second; | 46 return found->second; |
| 43 } | 47 } |
| 44 } | 48 } |
| 45 | 49 |
| 46 void ProgramCache::LinkedProgramCacheSuccess( | 50 void ProgramCache::LinkedProgramCacheSuccess( |
| 47 const std::string& shader_signature_a, | 51 const std::string& shader_signature_a, |
| 48 const std::string& shader_signature_b, | 52 const std::string& shader_signature_b, |
| 49 const LocationMap* bind_attrib_location_map) { | 53 const LocationMap* bind_attrib_location_map, |
| 54 const std::vector<std::string>& transform_feedback_varyings, |
| 55 GLenum transform_feedback_buffer_mode) { |
| 50 char a_sha[kHashLength]; | 56 char a_sha[kHashLength]; |
| 51 char b_sha[kHashLength]; | 57 char b_sha[kHashLength]; |
| 52 ComputeShaderHash(shader_signature_a, a_sha); | 58 ComputeShaderHash(shader_signature_a, a_sha); |
| 53 ComputeShaderHash(shader_signature_b, b_sha); | 59 ComputeShaderHash(shader_signature_b, b_sha); |
| 54 char sha[kHashLength]; | 60 char sha[kHashLength]; |
| 55 ComputeProgramHash(a_sha, | 61 ComputeProgramHash(a_sha, |
| 56 b_sha, | 62 b_sha, |
| 57 bind_attrib_location_map, | 63 bind_attrib_location_map, |
| 64 transform_feedback_varyings, |
| 65 transform_feedback_buffer_mode, |
| 58 sha); | 66 sha); |
| 59 const std::string sha_string(sha, kHashLength); | 67 const std::string sha_string(sha, kHashLength); |
| 60 | 68 |
| 61 LinkedProgramCacheSuccess(sha_string); | 69 LinkedProgramCacheSuccess(sha_string); |
| 62 } | 70 } |
| 63 | 71 |
| 64 void ProgramCache::LinkedProgramCacheSuccess(const std::string& program_hash) { | 72 void ProgramCache::LinkedProgramCacheSuccess(const std::string& program_hash) { |
| 65 link_status_[program_hash] = LINK_SUCCEEDED; | 73 link_status_[program_hash] = LINK_SUCCEEDED; |
| 66 } | 74 } |
| 67 | 75 |
| 68 void ProgramCache::ComputeShaderHash( | 76 void ProgramCache::ComputeShaderHash( |
| 69 const std::string& str, | 77 const std::string& str, |
| 70 char* result) const { | 78 char* result) const { |
| 71 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()), | 79 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(str.c_str()), |
| 72 str.length(), reinterpret_cast<unsigned char*>(result)); | 80 str.length(), reinterpret_cast<unsigned char*>(result)); |
| 73 } | 81 } |
| 74 | 82 |
| 75 void ProgramCache::Evict(const std::string& program_hash) { | 83 void ProgramCache::Evict(const std::string& program_hash) { |
| 76 link_status_.erase(program_hash); | 84 link_status_.erase(program_hash); |
| 77 } | 85 } |
| 78 | 86 |
| 79 namespace { | 87 namespace { |
| 80 size_t CalculateMapSize(const std::map<std::string, GLint>* map) { | 88 size_t CalculateMapSize(const std::map<std::string, GLint>* map) { |
| 81 if (!map) { | 89 if (!map) { |
| 82 return 0; | 90 return 0; |
| 83 } | 91 } |
| 84 std::map<std::string, GLint>::const_iterator it; | |
| 85 size_t total = 0; | 92 size_t total = 0; |
| 86 for (it = map->begin(); it != map->end(); ++it) { | 93 for (auto it = map->begin(); it != map->end(); ++it) { |
| 87 total += 4 + it->first.length(); | 94 total += 4 + it->first.length(); |
| 88 } | 95 } |
| 89 return total; | 96 return total; |
| 90 } | 97 } |
| 98 |
| 99 size_t CalculateVaryingsSize(const std::vector<std::string>& varyings) { |
| 100 size_t total = 0; |
| 101 for (auto& varying : varyings) { |
| 102 total += 1 + varying.length(); |
| 103 } |
| 104 return total; |
| 105 } |
| 91 } // anonymous namespace | 106 } // anonymous namespace |
| 92 | 107 |
| 93 void ProgramCache::ComputeProgramHash( | 108 void ProgramCache::ComputeProgramHash( |
| 94 const char* hashed_shader_0, | 109 const char* hashed_shader_0, |
| 95 const char* hashed_shader_1, | 110 const char* hashed_shader_1, |
| 96 const std::map<std::string, GLint>* bind_attrib_location_map, | 111 const std::map<std::string, GLint>* bind_attrib_location_map, |
| 112 const std::vector<std::string>& transform_feedback_varyings, |
| 113 GLenum transform_feedback_buffer_mode, |
| 97 char* result) const { | 114 char* result) const { |
| 98 const size_t shader0_size = kHashLength; | 115 const size_t shader0_size = kHashLength; |
| 99 const size_t shader1_size = kHashLength; | 116 const size_t shader1_size = kHashLength; |
| 100 const size_t map_size = CalculateMapSize(bind_attrib_location_map); | 117 const size_t map_size = CalculateMapSize(bind_attrib_location_map); |
| 101 const size_t total_size = shader0_size + shader1_size + map_size; | 118 const size_t var_size = CalculateVaryingsSize(transform_feedback_varyings); |
| 119 const size_t total_size = shader0_size + shader1_size + map_size + var_size |
| 120 + sizeof(transform_feedback_buffer_mode); |
| 102 | 121 |
| 103 scoped_ptr<unsigned char[]> buffer(new unsigned char[total_size]); | 122 scoped_ptr<unsigned char[]> buffer(new unsigned char[total_size]); |
| 104 memcpy(buffer.get(), hashed_shader_0, shader0_size); | 123 memcpy(buffer.get(), hashed_shader_0, shader0_size); |
| 105 memcpy(&buffer[shader0_size], hashed_shader_1, shader1_size); | 124 memcpy(&buffer[shader0_size], hashed_shader_1, shader1_size); |
| 125 size_t current_pos = shader0_size + shader1_size; |
| 106 if (map_size != 0) { | 126 if (map_size != 0) { |
| 107 // copy our map | 127 // copy our map |
| 108 size_t current_pos = shader0_size + shader1_size; | 128 for (auto it = bind_attrib_location_map->begin(); |
| 109 std::map<std::string, GLint>::const_iterator it; | |
| 110 for (it = bind_attrib_location_map->begin(); | |
| 111 it != bind_attrib_location_map->end(); | 129 it != bind_attrib_location_map->end(); |
| 112 ++it) { | 130 ++it) { |
| 113 const size_t name_size = it->first.length(); | 131 const size_t name_size = it->first.length(); |
| 114 memcpy(&buffer.get()[current_pos], it->first.c_str(), name_size); | 132 memcpy(&buffer.get()[current_pos], it->first.c_str(), name_size); |
| 115 current_pos += name_size; | 133 current_pos += name_size; |
| 116 const GLint value = it->second; | 134 const GLint value = it->second; |
| 117 buffer[current_pos++] = value >> 24; | 135 buffer[current_pos++] = value >> 24; |
| 118 buffer[current_pos++] = static_cast<unsigned char>(value >> 16); | 136 buffer[current_pos++] = static_cast<unsigned char>(value >> 16); |
| 119 buffer[current_pos++] = static_cast<unsigned char>(value >> 8); | 137 buffer[current_pos++] = static_cast<unsigned char>(value >> 8); |
| 120 buffer[current_pos++] = static_cast<unsigned char>(value); | 138 buffer[current_pos++] = static_cast<unsigned char>(value); |
| 121 } | 139 } |
| 122 } | 140 } |
| 141 |
| 142 if (var_size != 0) { |
| 143 // copy transform feedback varyings |
| 144 for (auto& varying : transform_feedback_varyings) { |
| 145 const size_t name_size = varying.length(); |
| 146 memcpy(&buffer.get()[current_pos], varying.c_str(), name_size); |
| 147 current_pos += name_size; |
| 148 buffer[current_pos++] = ' '; |
| 149 } |
| 150 } |
| 151 memcpy(&buffer[current_pos], &transform_feedback_buffer_mode, |
| 152 sizeof(transform_feedback_buffer_mode)); |
| 123 base::SHA1HashBytes(buffer.get(), | 153 base::SHA1HashBytes(buffer.get(), |
| 124 total_size, reinterpret_cast<unsigned char*>(result)); | 154 total_size, reinterpret_cast<unsigned char*>(result)); |
| 125 } | 155 } |
| 126 | 156 |
| 127 } // namespace gles2 | 157 } // namespace gles2 |
| 128 } // namespace gpu | 158 } // namespace gpu |
| OLD | NEW |