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.
5 #include "gpu/command_buffer/service/program_cache.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "gpu/command_buffer/service/shader_manager.h"
14 ProgramCache::ProgramCache() {}
15 ProgramCache::~ProgramCache() {}
17 void ProgramCache::Clear() {
22 ProgramCache::LinkedProgramStatus
ProgramCache::GetLinkedProgramStatus(
23 const std::string
& untranslated_a
,
24 const ShaderTranslatorInterface
* translator_a
,
25 const std::string
& untranslated_b
,
26 const ShaderTranslatorInterface
* translator_b
,
27 const std::map
<std::string
, GLint
>* bind_attrib_location_map
) const {
28 char a_sha
[kHashLength
];
29 char b_sha
[kHashLength
];
30 ComputeShaderHash(untranslated_a
, translator_a
, a_sha
);
31 ComputeShaderHash(untranslated_b
, translator_b
, b_sha
);
33 char sha
[kHashLength
];
34 ComputeProgramHash(a_sha
,
36 bind_attrib_location_map
,
38 const std::string
sha_string(sha
, kHashLength
);
40 LinkStatusMap::const_iterator found
= link_status_
.find(sha_string
);
41 if (found
== link_status_
.end()) {
42 return ProgramCache::LINK_UNKNOWN
;
48 void ProgramCache::LinkedProgramCacheSuccess(
49 const std::string
& shader_a
,
50 const ShaderTranslatorInterface
* translator_a
,
51 const std::string
& shader_b
,
52 const ShaderTranslatorInterface
* translator_b
,
53 const LocationMap
* bind_attrib_location_map
) {
54 char a_sha
[kHashLength
];
55 char b_sha
[kHashLength
];
56 ComputeShaderHash(shader_a
, translator_a
, a_sha
);
57 ComputeShaderHash(shader_b
, translator_b
, b_sha
);
58 char sha
[kHashLength
];
59 ComputeProgramHash(a_sha
,
61 bind_attrib_location_map
,
63 const std::string
sha_string(sha
, kHashLength
);
65 LinkedProgramCacheSuccess(sha_string
);
68 void ProgramCache::LinkedProgramCacheSuccess(const std::string
& program_hash
) {
69 link_status_
[program_hash
] = LINK_SUCCEEDED
;
72 void ProgramCache::ComputeShaderHash(
73 const std::string
& str
,
74 const ShaderTranslatorInterface
* translator
,
77 translator
? translator
->GetStringForOptionsThatWouldAffectCompilation() :
78 std::string()) + str
);
79 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(s
.c_str()),
80 s
.length(), reinterpret_cast<unsigned char*>(result
));
83 void ProgramCache::Evict(const std::string
& program_hash
) {
84 link_status_
.erase(program_hash
);
88 size_t CalculateMapSize(const std::map
<std::string
, GLint
>* map
) {
92 std::map
<std::string
, GLint
>::const_iterator it
;
94 for (it
= map
->begin(); it
!= map
->end(); ++it
) {
95 total
+= 4 + it
->first
.length();
99 } // anonymous namespace
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
;
111 scoped_ptr
<unsigned char[]> buffer(new unsigned char[total_size
]);
112 memcpy(buffer
.get(), hashed_shader_0
, shader0_size
);
113 memcpy(&buffer
[shader0_size
], hashed_shader_1
, shader1_size
);
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();
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
++] = static_cast<unsigned char>(value
>> 16);
127 buffer
[current_pos
++] = static_cast<unsigned char>(value
>> 8);
128 buffer
[current_pos
++] = static_cast<unsigned char>(value
);
131 base::SHA1HashBytes(buffer
.get(),
132 total_size
, reinterpret_cast<unsigned char*>(result
));