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_
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"
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
{
25 enum GlslImplementationType
{
34 precision(SH_PRECISION_UNDEFINED
),
38 VariableInfo(int _type
, int _size
, int _precision
,
39 int _static_use
, std::string _name
)
42 precision(_precision
),
43 static_use(_static_use
),
47 const ShaderTranslatorInterface::VariableInfo
& other
) const {
48 return type
== other
.type
&&
50 precision
== other
.precision
&&
51 strcmp(name
.c_str(), other
.name
.c_str()) == 0;
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.
69 #if (ANGLE_SH_VERSION >= 126)
70 sh::GLenum shader_type
,
72 ShShaderType shader_type
,
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;
100 virtual ~ShaderTranslatorInterface() {}
103 // Implementation of ShaderTranslatorInterface
104 class GPU_EXPORT ShaderTranslator
105 : public base::RefCounted
<ShaderTranslator
>,
106 NON_EXPORTED_BASE(public ShaderTranslatorInterface
) {
108 class DestructionObserver
{
110 DestructionObserver();
111 virtual ~DestructionObserver();
113 virtual void OnDestruct(ShaderTranslator
* translator
) = 0;
116 DISALLOW_COPY_AND_ASSIGN(DestructionObserver
);
121 // Overridden from ShaderTranslatorInterface.
123 #if (ANGLE_SH_VERSION >= 126)
124 sh::GLenum shader_type
,
126 ShShaderType shader_type
,
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
149 void AddDestructionObserver(DestructionObserver
* observer
);
150 void RemoveDestructionObserver(DestructionObserver
* observer
);
153 friend class base::RefCounted
<ShaderTranslator
>;
155 virtual ~ShaderTranslator();
157 int GetCompileOptions() const;
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_
;
167 bool implementation_is_glsl_es_
;
168 ShCompileOptions driver_bug_workarounds_
;
169 ObserverList
<DestructionObserver
> destruction_observers_
;
171 DISALLOW_COPY_AND_ASSIGN(ShaderTranslator
);
177 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_