Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: gpu/command_buffer/service/memory_program_cache.cc

Issue 10534173: GPU Program Caching (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Patch Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "gpu/command_buffer/service/memory_program_cache.h"
6
7 #include "base/sha1.h"
8 #include "gpu/command_buffer/service/gl_utils.h"
9 #include "net/disk_cache/disk_cache.h"
10 #include "ui/gl/gl_bindings.h"
11
12 namespace gpu {
13 namespace gles2 {
14
15 namespace {
16 typedef std::map<std::string, GLint> BindAttribMap;
greggman 2012/06/26 23:00:27 Sorry I didn't make myself clearer last time. It s
dmurph 2012/07/04 00:01:29 Done.
17 } // anonymous namespace
18
19 MemoryProgramCache::~MemoryProgramCache() {
20 }
21
22 void MemoryProgramCache::ClearBackend() {
23 curr_size_bytes_ = 0;
24 store_.clear();
25 eviction_helper_.Clear();
26 }
27
28 bool MemoryProgramCache::LoadLinkedProgram(
29 const GLuint program,
30 ShaderManager::ShaderInfo* shader_a,
31 ShaderManager::ShaderInfo* shader_b,
32 const BindAttribMap* bind_attrib_location_map) const {
33 char a_sha[kHashLength];
34 char b_sha[kHashLength];
35 ComputeShaderHash(*shader_a->source(), a_sha);
36 ComputeShaderHash(*shader_b->source(), b_sha);
37
38 char sha[kHashLength];
39 ComputeProgramHash(a_sha,
40 b_sha,
41 bind_attrib_location_map,
42 sha);
43 const std::string sha_string(sha, kHashLength);
44
45 StoreMap::const_iterator found = store_.find(sha_string);
46 if (found == store_.end()) {
47 return false;
48 }
49 const scoped_refptr<ProgramCacheValue> value = found->second;
greggman 2012/06/26 23:00:27 I think you can just use const ProgramCacheValue*
dmurph 2012/07/04 00:01:29 Done.
50 glProgramBinary(program,
51 value->format,
apatrick_chromium 2012/06/28 21:54:31 Is the format guaranteed to be understood by the c
dmurph 2012/06/28 22:49:54 Well, the format was populated by a corresponding
apatrick_chromium 2012/06/28 23:22:33 I think only on mac, though so far I haven't found
dmurph 2012/07/04 00:01:29 Just to document in the comment thread, this shoul
52 static_cast<const GLvoid*>(value->data.get()),
greggman 2012/06/26 23:00:27 Did the compiler complain without the cast? I woul
dmurph 2012/07/04 00:01:29 Done.
53 value->length);
54 shader_a->SetAttribMap(value->attrib_map_0);
55 shader_a->SetUniformMap(value->uniform_map_0);
56 shader_b->SetAttribMap(value->attrib_map_1);
57 shader_b->SetUniformMap(value->uniform_map_1);
58 return true;
59 }
60
61 void MemoryProgramCache::SaveLinkedProgram(
62 const GLuint program,
63 const ShaderManager::ShaderInfo* shader_a,
64 const ShaderManager::ShaderInfo* shader_b,
65 const BindAttribMap* bind_attrib_location_map) {
66 GLsizei length;
67 GLenum format;
68 GLsizei buffer_length = 0;
69 glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH_OES, &buffer_length);
70 scoped_array<char> binary(new char[buffer_length]);
71 glGetProgramBinary(program,
72 buffer_length,
73 &length,
74 &format,
75 static_cast<GLvoid*>(binary.get()));
76 if (length == 0) {
77 return;
78 }
79
80 char a_sha[kHashLength];
81 char b_sha[kHashLength];
82 ComputeShaderHash(*shader_a->source(), a_sha);
83 ComputeShaderHash(*shader_b->source(), b_sha);
84
85 scoped_refptr<ProgramCacheValue> value(
86 new ProgramCacheValue(length,
87 format,
88 binary.release(),
89 a_sha,
90 shader_a->attrib_map(),
91 shader_a->uniform_map(),
92 b_sha,
93 shader_b->attrib_map(),
94 shader_b->uniform_map()));
95 char sha[kHashLength];
96 ComputeProgramHash(a_sha,
97 b_sha,
98 bind_attrib_location_map,
99 sha);
100 const std::string sha_string(sha, kHashLength);
101
102 if (store_.find(sha_string) != store_.end()) {
103 return;
104 }
105
106 while (curr_size_bytes_ + length > max_size_bytes_) {
107 if (eviction_helper_.IsEmpty()) {
greggman 2012/06/26 23:00:27 If length is > max_size_bytes it so that no matter
dmurph 2012/07/04 00:01:29 Done.
108 return;
109 }
110 const std::string& program = eviction_helper_.PeekKey();
111 const StoreMap::iterator found = store_.find(program);
112 const scoped_refptr<ProgramCacheValue>& evicting = found->second;
greggman 2012/06/26 23:00:27 here too it's safe to just use const ProgramCache
dmurph 2012/07/04 00:01:29 Done.
113 curr_size_bytes_ -= evicting->length;
114 Evict(program, evicting->shader_0_hash, evicting->shader_1_hash);
115 store_.erase(found);
116 eviction_helper_.PopKey();
117 }
118 store_[sha_string] = value;
119 curr_size_bytes_ += length;
120 eviction_helper_.KeyUsed(sha_string);
121 LinkedProgramCacheSuccess(sha_string,
122 std::string(a_sha, kHashLength),
123 std::string(b_sha, kHashLength));
124 }
125
greggman 2012/06/26 23:00:27 You need a memory_program_cache_unittest.cc
126 } // namespace gles2
127 } // namespace gpu
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698