Chromium Code Reviews| 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 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 81 if (!map) { | 89 if (!map) { |
| 82 return 0; | 90 return 0; |
| 83 } | 91 } |
| 84 std::map<std::string, GLint>::const_iterator it; | 92 std::map<std::string, GLint>::const_iterator it; |
| 85 size_t total = 0; | 93 size_t total = 0; |
| 86 for (it = map->begin(); it != map->end(); ++it) { | 94 for (it = map->begin(); it != map->end(); ++it) { |
| 87 total += 4 + it->first.length(); | 95 total += 4 + it->first.length(); |
| 88 } | 96 } |
| 89 return total; | 97 return total; |
| 90 } | 98 } |
| 99 size_t CalculateVaryingsSize(const std::vector<std::string>* varyings) { | |
|
Zhenyao Mo
2015/03/12 23:50:57
nit: a line before this function.
| |
| 100 if (!varyings) { | |
| 101 return 0; | |
| 102 } | |
| 103 std::vector<std::string>::const_iterator it; | |
|
Zhenyao Mo
2015/03/12 23:50:57
No need to expose iterator outside for loop.
| |
| 104 size_t total = 0; | |
| 105 for (it = varyings->begin(); it != varyings->end(); ++it) { | |
| 106 total += it->length(); | |
| 107 } | |
| 108 return total; | |
| 109 } | |
| 91 } // anonymous namespace | 110 } // anonymous namespace |
| 92 | 111 |
| 93 void ProgramCache::ComputeProgramHash( | 112 void ProgramCache::ComputeProgramHash( |
| 94 const char* hashed_shader_0, | 113 const char* hashed_shader_0, |
| 95 const char* hashed_shader_1, | 114 const char* hashed_shader_1, |
| 96 const std::map<std::string, GLint>* bind_attrib_location_map, | 115 const std::map<std::string, GLint>* bind_attrib_location_map, |
| 116 const std::vector<std::string>* transform_feedback_varyings, | |
| 117 GLenum transform_feedback_buffer_mode, | |
| 97 char* result) const { | 118 char* result) const { |
| 98 const size_t shader0_size = kHashLength; | 119 const size_t shader0_size = kHashLength; |
| 99 const size_t shader1_size = kHashLength; | 120 const size_t shader1_size = kHashLength; |
| 100 const size_t map_size = CalculateMapSize(bind_attrib_location_map); | 121 const size_t map_size = CalculateMapSize(bind_attrib_location_map); |
| 101 const size_t total_size = shader0_size + shader1_size + map_size; | 122 const size_t var_size = CalculateVaryingsSize(transform_feedback_varyings); |
| 123 const size_t total_size = shader0_size + shader1_size + map_size + var_size | |
| 124 + sizeof(GLenum); | |
|
Zhenyao Mo
2015/03/12 23:50:57
nit: sizeof(transform_feedback_buffer_mode)
| |
| 102 | 125 |
| 103 scoped_ptr<unsigned char[]> buffer(new unsigned char[total_size]); | 126 scoped_ptr<unsigned char[]> buffer(new unsigned char[total_size]); |
| 104 memcpy(buffer.get(), hashed_shader_0, shader0_size); | 127 memcpy(buffer.get(), hashed_shader_0, shader0_size); |
| 105 memcpy(&buffer[shader0_size], hashed_shader_1, shader1_size); | 128 memcpy(&buffer[shader0_size], hashed_shader_1, shader1_size); |
| 129 size_t current_pos = shader0_size + shader1_size; | |
| 106 if (map_size != 0) { | 130 if (map_size != 0) { |
| 107 // copy our map | 131 // copy our map |
| 108 size_t current_pos = shader0_size + shader1_size; | |
| 109 std::map<std::string, GLint>::const_iterator it; | 132 std::map<std::string, GLint>::const_iterator it; |
| 110 for (it = bind_attrib_location_map->begin(); | 133 for (it = bind_attrib_location_map->begin(); |
| 111 it != bind_attrib_location_map->end(); | 134 it != bind_attrib_location_map->end(); |
| 112 ++it) { | 135 ++it) { |
| 113 const size_t name_size = it->first.length(); | 136 const size_t name_size = it->first.length(); |
| 114 memcpy(&buffer.get()[current_pos], it->first.c_str(), name_size); | 137 memcpy(&buffer.get()[current_pos], it->first.c_str(), name_size); |
| 115 current_pos += name_size; | 138 current_pos += name_size; |
| 116 const GLint value = it->second; | 139 const GLint value = it->second; |
| 117 buffer[current_pos++] = value >> 24; | 140 buffer[current_pos++] = value >> 24; |
| 118 buffer[current_pos++] = static_cast<unsigned char>(value >> 16); | 141 buffer[current_pos++] = static_cast<unsigned char>(value >> 16); |
| 119 buffer[current_pos++] = static_cast<unsigned char>(value >> 8); | 142 buffer[current_pos++] = static_cast<unsigned char>(value >> 8); |
| 120 buffer[current_pos++] = static_cast<unsigned char>(value); | 143 buffer[current_pos++] = static_cast<unsigned char>(value); |
| 121 } | 144 } |
| 122 } | 145 } |
| 146 | |
| 147 if (var_size != 0) { | |
| 148 // copy transform feedback varyings | |
| 149 std::vector<std::string>::const_iterator it; | |
|
Zhenyao Mo
2015/03/12 23:50:57
iterator should not be exposed outside the for loo
| |
| 150 for (it = transform_feedback_varyings->begin(); | |
| 151 it != transform_feedback_varyings->end(); | |
| 152 ++it) { | |
| 153 const size_t name_size = it->length(); | |
| 154 memcpy(&buffer.get()[current_pos], it->c_str(), name_size); | |
|
Zhenyao Mo
2015/03/12 23:50:57
You need to insert a ' ' in between two varyings.
| |
| 155 current_pos += name_size; | |
| 156 } | |
| 157 } | |
| 158 memcpy(&buffer[current_pos], &transform_feedback_buffer_mode, sizeof(GLenum)); | |
| 123 base::SHA1HashBytes(buffer.get(), | 159 base::SHA1HashBytes(buffer.get(), |
| 124 total_size, reinterpret_cast<unsigned char*>(result)); | 160 total_size, reinterpret_cast<unsigned char*>(result)); |
| 125 } | 161 } |
| 126 | 162 |
| 127 } // namespace gles2 | 163 } // namespace gles2 |
| 128 } // namespace gpu | 164 } // namespace gpu |
| OLD | NEW |