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
& shader_signature_a
,
24 const std::string
& shader_signature_b
,
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 {
28 char a_sha
[kHashLength
];
29 char b_sha
[kHashLength
];
30 ComputeShaderHash(shader_signature_a
, a_sha
);
31 ComputeShaderHash(shader_signature_b
, b_sha
);
33 char sha
[kHashLength
];
34 ComputeProgramHash(a_sha
,
36 bind_attrib_location_map
,
37 transform_feedback_varyings
,
38 transform_feedback_buffer_mode
,
40 const std::string
sha_string(sha
, kHashLength
);
42 LinkStatusMap::const_iterator found
= link_status_
.find(sha_string
);
43 if (found
== link_status_
.end()) {
44 return ProgramCache::LINK_UNKNOWN
;
50 void ProgramCache::LinkedProgramCacheSuccess(
51 const std::string
& shader_signature_a
,
52 const std::string
& shader_signature_b
,
53 const LocationMap
* bind_attrib_location_map
,
54 const std::vector
<std::string
>& transform_feedback_varyings
,
55 GLenum transform_feedback_buffer_mode
) {
56 char a_sha
[kHashLength
];
57 char b_sha
[kHashLength
];
58 ComputeShaderHash(shader_signature_a
, a_sha
);
59 ComputeShaderHash(shader_signature_b
, b_sha
);
60 char sha
[kHashLength
];
61 ComputeProgramHash(a_sha
,
63 bind_attrib_location_map
,
64 transform_feedback_varyings
,
65 transform_feedback_buffer_mode
,
67 const std::string
sha_string(sha
, kHashLength
);
69 LinkedProgramCacheSuccess(sha_string
);
72 void ProgramCache::LinkedProgramCacheSuccess(const std::string
& program_hash
) {
73 link_status_
[program_hash
] = LINK_SUCCEEDED
;
76 void ProgramCache::ComputeShaderHash(
77 const std::string
& str
,
79 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(str
.c_str()),
80 str
.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
) {
93 for (auto it
= map
->begin(); it
!= map
->end(); ++it
) {
94 total
+= 4 + it
->first
.length();
99 size_t CalculateVaryingsSize(const std::vector
<std::string
>& varyings
) {
101 for (auto& varying
: varyings
) {
102 total
+= 1 + varying
.length();
106 } // anonymous namespace
108 void ProgramCache::ComputeProgramHash(
109 const char* hashed_shader_0
,
110 const char* hashed_shader_1
,
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
,
114 char* result
) const {
115 const size_t shader0_size
= kHashLength
;
116 const size_t shader1_size
= kHashLength
;
117 const size_t map_size
= CalculateMapSize(bind_attrib_location_map
);
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
);
122 scoped_ptr
<unsigned char[]> buffer(new unsigned char[total_size
]);
123 memcpy(buffer
.get(), hashed_shader_0
, shader0_size
);
124 memcpy(&buffer
[shader0_size
], hashed_shader_1
, shader1_size
);
125 size_t current_pos
= shader0_size
+ shader1_size
;
128 for (auto it
= bind_attrib_location_map
->begin();
129 it
!= bind_attrib_location_map
->end();
131 const size_t name_size
= it
->first
.length();
132 memcpy(&buffer
.get()[current_pos
], it
->first
.c_str(), name_size
);
133 current_pos
+= name_size
;
134 const GLint value
= it
->second
;
135 buffer
[current_pos
++] = value
>> 24;
136 buffer
[current_pos
++] = static_cast<unsigned char>(value
>> 16);
137 buffer
[current_pos
++] = static_cast<unsigned char>(value
>> 8);
138 buffer
[current_pos
++] = static_cast<unsigned char>(value
);
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
++] = ' ';
151 memcpy(&buffer
[current_pos
], &transform_feedback_buffer_mode
,
152 sizeof(transform_feedback_buffer_mode
));
153 base::SHA1HashBytes(buffer
.get(),
154 total_size
, reinterpret_cast<unsigned char*>(result
));