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/shader_manager.h" | 5 #include "gpu/command_buffer/service/shader_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 | 11 |
12 namespace gpu { | 12 namespace gpu { |
13 namespace gles2 { | 13 namespace gles2 { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // Given a variable name | a[0].b.c[0] |, return |a|. | 17 // Given a variable name | a[0].b.c[0] |, return |a|. |
18 std::string GetTopVariableName(const std::string& fullname) { | 18 std::string GetTopVariableName(const std::string& fullname) { |
19 size_t pos = fullname.find_first_of("[."); | 19 size_t pos = fullname.find_first_of("[."); |
20 if (pos == std::string::npos) | 20 if (pos == std::string::npos) |
21 return fullname; | 21 return fullname; |
22 return fullname.substr(0, pos); | 22 return fullname.substr(0, pos); |
23 } | 23 } |
24 | 24 |
25 } // namespace anonymous | 25 } // namespace anonymous |
26 | 26 |
27 Shader::Shader(GLuint service_id, GLenum shader_type) | 27 Shader::Shader(GLuint service_id, GLenum shader_type) |
28 : use_count_(0), | 28 : use_count_(0), |
29 shader_state_(kShaderStateWaiting), | |
29 service_id_(service_id), | 30 service_id_(service_id), |
30 shader_type_(shader_type), | 31 shader_type_(shader_type), |
31 valid_(false) { | 32 valid_(false) { |
32 } | 33 } |
33 | 34 |
34 Shader::~Shader() { | 35 Shader::~Shader() { |
35 } | 36 } |
36 | 37 |
38 void Shader::RequestCompile() { | |
39 shader_state_ = kShaderStateCompileRequested; | |
40 last_compiled_source_ = source_; | |
41 } | |
42 | |
37 void Shader::DoCompile(ShaderTranslatorInterface* translator, | 43 void Shader::DoCompile(ShaderTranslatorInterface* translator, |
38 TranslatedShaderSourceType type) { | 44 TranslatedShaderSourceType type) { |
45 // If compile was not requested yet, we assume this is not a deferred compile. | |
46 // It is up to the caller of the function to check that the shader state | |
47 // is kShaderStateCompileRequested if they want to enforce deferred shader | |
48 // compiles. Otherwise calling DoCompile() will just compile the source that | |
49 // was last set. For deferred compiles we should compile | |
50 // |last_compiled_source_| instead of |source_| because they could have set a | |
51 // new shader source after requesting a compile, in which case the shader at | |
52 // the time of the compile request is the proper source. | |
vmiura
2015/02/04 21:18:56
nit: DCHECK(shader_state_ != kShaderStateCompiled)
David Yen
2015/02/04 22:05:49
After discussing in person, we decided to simplify
| |
53 if (shader_state_ != kShaderStateCompileRequested) { | |
54 last_compiled_source_ = source_; | |
55 } | |
56 | |
57 // Signify the shader has been compiled, whether or not it is valid | |
58 // is dependent on the |valid_| member variable. | |
59 shader_state_ = kShaderStateCompiled; | |
60 | |
39 // Translate GL ES 2.0 shader to Desktop GL shader and pass that to | 61 // Translate GL ES 2.0 shader to Desktop GL shader and pass that to |
40 // glShaderSource and then glCompileShader. | 62 // glShaderSource and then glCompileShader. |
41 const char* source_for_driver = source_.c_str(); | 63 const char* source_for_driver = last_compiled_source_.c_str(); |
42 if (translator) { | 64 if (translator) { |
43 valid_ = translator->Translate(source_, | 65 valid_ = translator->Translate(last_compiled_source_, |
44 &log_info_, | 66 &log_info_, |
45 &translated_source_, | 67 &translated_source_, |
46 &attrib_map_, | 68 &attrib_map_, |
47 &uniform_map_, | 69 &uniform_map_, |
48 &varying_map_, | 70 &varying_map_, |
49 &name_map_); | 71 &name_map_); |
50 if (!valid_) { | 72 if (!valid_) { |
51 return; | 73 return; |
52 } | 74 } |
53 signature_source_ = source_; | |
54 source_for_driver = translated_source_.c_str(); | 75 source_for_driver = translated_source_.c_str(); |
55 } | 76 } |
56 | 77 |
57 glShaderSource(service_id_, 1, &source_for_driver, NULL); | 78 glShaderSource(service_id_, 1, &source_for_driver, NULL); |
58 glCompileShader(service_id_); | 79 glCompileShader(service_id_); |
59 if (type == kANGLE) { | 80 if (type == kANGLE) { |
60 GLint max_len = 0; | 81 GLint max_len = 0; |
61 glGetShaderiv(service_id_, | 82 glGetShaderiv(service_id_, |
62 GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE, | 83 GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE, |
63 &max_len); | 84 &max_len); |
(...skipping 17 matching lines...) Expand all Loading... | |
81 scoped_ptr<char[]> buffer(new char[max_len]); | 102 scoped_ptr<char[]> buffer(new char[max_len]); |
82 GLint len = 0; | 103 GLint len = 0; |
83 glGetShaderInfoLog(service_id_, max_len, &len, buffer.get()); | 104 glGetShaderInfoLog(service_id_, max_len, &len, buffer.get()); |
84 DCHECK(max_len == 0 || len < max_len); | 105 DCHECK(max_len == 0 || len < max_len); |
85 DCHECK(len == 0 || buffer[len] == '\0'); | 106 DCHECK(len == 0 || buffer[len] == '\0'); |
86 valid_ = false; | 107 valid_ = false; |
87 log_info_ = std::string(buffer.get(), len); | 108 log_info_ = std::string(buffer.get(), len); |
88 LOG_IF(ERROR, translator) | 109 LOG_IF(ERROR, translator) |
89 << "Shader translator allowed/produced an invalid shader " | 110 << "Shader translator allowed/produced an invalid shader " |
90 << "unless the driver is buggy:" | 111 << "unless the driver is buggy:" |
91 << "\n--original-shader--\n" << source_ | 112 << "\n--original-shader--\n" << last_compiled_source_ |
92 << "\n--translated-shader--\n" << source_for_driver | 113 << "\n--translated-shader--\n" << source_for_driver |
93 << "\n--info-log--\n" << log_info_; | 114 << "\n--info-log--\n" << log_info_; |
94 } | 115 } |
95 } | 116 } |
96 | 117 |
97 void Shader::IncUseCount() { | 118 void Shader::IncUseCount() { |
98 ++use_count_; | 119 ++use_count_; |
99 } | 120 } |
100 | 121 |
101 void Shader::DecUseCount() { | 122 void Shader::DecUseCount() { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
234 DCHECK(shader); | 255 DCHECK(shader); |
235 DCHECK(IsOwned(shader)); | 256 DCHECK(IsOwned(shader)); |
236 shader->DecUseCount(); | 257 shader->DecUseCount(); |
237 RemoveShader(shader); | 258 RemoveShader(shader); |
238 } | 259 } |
239 | 260 |
240 } // namespace gles2 | 261 } // namespace gles2 |
241 } // namespace gpu | 262 } // namespace gpu |
242 | 263 |
243 | 264 |
OLD | NEW |