Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / gpu / command_buffer / service / shader_translator.h
blobd936e12161da9924fd7307599bf279a5b93f9047
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 #ifndef GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "gpu/gpu_export.h"
16 #include "third_party/angle/include/GLSLANG/ShaderLang.h"
18 namespace gpu {
19 namespace gles2 {
21 // Translates a GLSL ES 2.0 shader to desktop GLSL shader, or just
22 // validates GLSL ES 2.0 shaders on a true GLSL ES implementation.
23 class ShaderTranslatorInterface {
24 public:
25 enum GlslImplementationType {
26 kGlsl,
27 kGlslES
30 struct VariableInfo {
31 VariableInfo()
32 : type(0),
33 size(0),
34 precision(SH_PRECISION_UNDEFINED),
35 static_use(0) {
38 VariableInfo(int _type, int _size, int _precision,
39 int _static_use, std::string _name)
40 : type(_type),
41 size(_size),
42 precision(_precision),
43 static_use(_static_use),
44 name(_name) {
46 bool operator==(
47 const ShaderTranslatorInterface::VariableInfo& other) const {
48 return type == other.type &&
49 size == other.size &&
50 precision == other.precision &&
51 strcmp(name.c_str(), other.name.c_str()) == 0;
54 int type;
55 int size;
56 int precision;
57 int static_use;
58 std::string name; // name in the original shader source.
61 // Mapping between variable name and info.
62 typedef base::hash_map<std::string, VariableInfo> VariableMap;
63 // Mapping between hashed name and original name.
64 typedef base::hash_map<std::string, std::string> NameMap;
66 // Initializes the translator.
67 // Must be called once before using the translator object.
68 virtual bool Init(
69 #if (ANGLE_SH_VERSION >= 126)
70 sh::GLenum shader_type,
71 #else
72 ShShaderType shader_type,
73 #endif
74 ShShaderSpec shader_spec,
75 const ShBuiltInResources* resources,
76 GlslImplementationType glsl_implementation_type,
77 ShCompileOptions driver_bug_workarounds) = 0;
79 // Translates the given shader source.
80 // Returns true if translation is successful, false otherwise.
81 virtual bool Translate(const char* shader) = 0;
83 // The following functions return results from the last translation.
84 // The results are NULL/empty if the translation was unsuccessful.
85 // A valid info-log is always returned irrespective of whether translation
86 // was successful or not.
87 virtual const char* translated_shader() const = 0;
88 virtual const char* info_log() const = 0;
90 virtual const VariableMap& attrib_map() const = 0;
91 virtual const VariableMap& uniform_map() const = 0;
92 virtual const VariableMap& varying_map() const = 0;
93 virtual const NameMap& name_map() const = 0;
95 // Return a string that is unique for a specfic set of options that would
96 // possibly affect compilation.
97 virtual std::string GetStringForOptionsThatWouldAffectCompilation() const = 0;
99 protected:
100 virtual ~ShaderTranslatorInterface() {}
103 // Implementation of ShaderTranslatorInterface
104 class GPU_EXPORT ShaderTranslator
105 : public base::RefCounted<ShaderTranslator>,
106 NON_EXPORTED_BASE(public ShaderTranslatorInterface) {
107 public:
108 class DestructionObserver {
109 public:
110 DestructionObserver();
111 virtual ~DestructionObserver();
113 virtual void OnDestruct(ShaderTranslator* translator) = 0;
115 private:
116 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
119 ShaderTranslator();
121 // Overridden from ShaderTranslatorInterface.
122 virtual bool Init(
123 #if (ANGLE_SH_VERSION >= 126)
124 sh::GLenum shader_type,
125 #else
126 ShShaderType shader_type,
127 #endif
128 ShShaderSpec shader_spec,
129 const ShBuiltInResources* resources,
130 GlslImplementationType glsl_implementation_type,
131 ShCompileOptions driver_bug_workarounds) OVERRIDE;
133 // Overridden from ShaderTranslatorInterface.
134 virtual bool Translate(const char* shader) OVERRIDE;
136 // Overridden from ShaderTranslatorInterface.
137 virtual const char* translated_shader() const OVERRIDE;
138 virtual const char* info_log() const OVERRIDE;
140 // Overridden from ShaderTranslatorInterface.
141 virtual const VariableMap& attrib_map() const OVERRIDE;
142 virtual const VariableMap& uniform_map() const OVERRIDE;
143 virtual const VariableMap& varying_map() const OVERRIDE;
144 virtual const NameMap& name_map() const OVERRIDE;
146 virtual std::string GetStringForOptionsThatWouldAffectCompilation() const
147 OVERRIDE;
149 void AddDestructionObserver(DestructionObserver* observer);
150 void RemoveDestructionObserver(DestructionObserver* observer);
152 private:
153 friend class base::RefCounted<ShaderTranslator>;
155 virtual ~ShaderTranslator();
156 void ClearResults();
157 int GetCompileOptions() const;
159 ShHandle compiler_;
160 ShBuiltInResources compiler_options_;
161 scoped_ptr<char[]> translated_shader_;
162 scoped_ptr<char[]> info_log_;
163 VariableMap attrib_map_;
164 VariableMap uniform_map_;
165 VariableMap varying_map_;
166 NameMap name_map_;
167 bool implementation_is_glsl_es_;
168 ShCompileOptions driver_bug_workarounds_;
169 ObserverList<DestructionObserver> destruction_observers_;
171 DISALLOW_COPY_AND_ASSIGN(ShaderTranslator);
174 } // namespace gles2
175 } // namespace gpu
177 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_