Chromium Code Reviews| Index: gpu/command_buffer/service/memory_program_cache.cc |
| diff --git a/gpu/command_buffer/service/memory_program_cache.cc b/gpu/command_buffer/service/memory_program_cache.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..df6bb15aa11a2d4f4863d6afcc8cbe3b68537702 |
| --- /dev/null |
| +++ b/gpu/command_buffer/service/memory_program_cache.cc |
| @@ -0,0 +1,113 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "gpu/command_buffer/service/memory_program_cache.h" |
| +#include "base/bind.h" |
| +#include "base/file_path.h" |
| +#include "base/sha1.h" |
| +#include "net/disk_cache/disk_cache.h" |
| +#include "ui/gl/gl_bindings.h" |
| + |
| +namespace gpu { |
| +namespace gles2 { |
| + |
| +typedef std::map<std::string, GLint> location_map; |
|
greggman
2012/06/19 22:27:50
style: types are CamelCase
Also, this should be i
dmurph
2012/06/23 01:37:28
Done.
|
| + |
| +MemoryProgramCache::MemoryProgramCache() {} |
| + |
| +MemoryProgramCache::~MemoryProgramCache() { |
| + store_map::iterator it; |
| + for(it = store_.begin(); it != store_.end();) { |
| + CachedProgramKey key = it->first; |
| + StoreValue value = it->second; |
| + ++it; |
| + store_.erase(key); |
|
greggman
2012/06/19 22:27:50
I don't think you can delete things like this from
dmurph
2012/06/23 01:37:28
This way works correctly. Although I'm removing t
|
| + delete[] key.hashed_src_0; |
| + delete[] key.hashed_src_1; |
| + delete key.bind_attrib_location_map; |
| + delete[] (char*)value.data; |
| + } |
| +} |
| + |
| +void MemoryProgramCache::LoadLinkedProgram( |
| + GLuint program, |
| + ShaderManager::ShaderInfo* shader_a, |
| + ShaderManager::ShaderInfo* shader_b, |
| + location_map* bind_attrib_location_map) const { |
| + unsigned char sha1_a[kHashLength]; |
| + unsigned char sha1_b[kHashLength]; |
| + computeHash(*shader_a->source(), sha1_a); |
| + computeHash(*shader_b->source(), sha1_b); |
| + |
| + CachedProgramKey key(sha1_a, sha1_b, bind_attrib_location_map); |
| + StoreValue value = store_.find(key)->second; |
| + glProgramBinary(program, value.format, value.data, value.length); |
| + shader_a->SetAttribMap(value.attrib_map_0); |
| + shader_a->SetUniformMap(value.uniform_map_0); |
| + shader_b->SetAttribMap(value.attrib_map_1); |
| + shader_b->SetUniformMap(value.uniform_map_1); |
| +} |
| + |
| +void MemoryProgramCache::SaveLinkedProgram( |
| + GLuint program, |
| + ShaderManager::ShaderInfo* shader_a, |
| + ShaderManager::ShaderInfo* shader_b, |
| + location_map* bind_attrib_location_map) { |
| + GLsizei length; |
| + GLenum format; |
| + GLsizei bufferlength = 0; |
| + glGetProgramiv(program, 0x8741, &bufferlength); |
| + GLvoid* binary = new char[bufferlength]; |
| + glGetProgramBinary(program, |
| + bufferlength, |
| + &length, |
| + &format, |
| + binary); |
| + if(length == 0) { |
| + fprintf(stderr, "got length of 0\n"); |
| + SetLinkedProgramStatus(*shader_a->source(), |
| + *shader_b->source(), |
| + bind_attrib_location_map, |
| + LINK_UNKNOWN); |
| + return; |
| + } |
| + |
| + unsigned char sha1_a[kHashLength]; |
| + unsigned char sha1_b[kHashLength]; |
| + computeHash(*shader_a->source(), sha1_a); |
| + computeHash(*shader_b->source(), sha1_b); |
| + |
| + StoreValue value(length, |
| + format, |
| + binary, |
| + shader_a->attrib_map(), |
| + shader_a->uniform_map(), |
| + shader_b->attrib_map(), |
| + shader_b->uniform_map()); |
| + |
| + CachedProgramKey key(sha1_a, sha1_b, bind_attrib_location_map); |
| + if (store_.find(key) == store_.end()) { |
| + unsigned char* allocated_sha_a = new unsigned char[kHashLength]; |
| + unsigned char* allocated_sha_b = new unsigned char[kHashLength]; |
| + location_map* allocated_location_map = bind_attrib_location_map != NULL ? |
| + new location_map(*bind_attrib_location_map) : |
| + NULL; |
| + memcpy(allocated_sha_a, sha1_a, kHashLength); |
| + memcpy(allocated_sha_b, sha1_b, kHashLength); |
| + |
| + CachedProgramKey allocated_key(allocated_sha_a, |
| + allocated_sha_b, |
| + allocated_location_map); |
| + store_[allocated_key] = value; |
| + } else { |
| + store_[key] = value; |
| + } |
| + SetLinkedProgramStatus(*shader_a->source(), |
| + *shader_b->source(), |
| + bind_attrib_location_map, |
| + LINK_SUCCEEDED); |
| +} |
| + |
| +} // namespace gles2 |
| +} // namespace gpu |