- add sources.
[platform/framework/web/crosswalk.git] / src / gpu / command_buffer / service / shader_translator.h
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.
4
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_
7
8 #include <string>
9
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_dx11/include/GLSLANG/ShaderLang.h"
17
18 namespace gpu {
19 namespace gles2 {
20
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
28   };
29
30   struct VariableInfo {
31     VariableInfo()
32         : type(0),
33           size(0),
34           precision(SH_PRECISION_UNDEFINED),
35           static_use(0) {
36     }
37
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) {
45     }
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;
52     }
53
54     int type;
55     int size;
56     int precision;
57     int static_use;
58     std::string name;  // name in the original shader source.
59   };
60
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;
65
66   // Initializes the translator.
67   // Must be called once before using the translator object.
68   virtual bool Init(
69       ShShaderType shader_type,
70       ShShaderSpec shader_spec,
71       const ShBuiltInResources* resources,
72       GlslImplementationType glsl_implementation_type,
73       ShCompileOptions driver_bug_workarounds) = 0;
74
75   // Translates the given shader source.
76   // Returns true if translation is successful, false otherwise.
77   virtual bool Translate(const char* shader) = 0;
78
79   // The following functions return results from the last translation.
80   // The results are NULL/empty if the translation was unsuccessful.
81   // A valid info-log is always returned irrespective of whether translation
82   // was successful or not.
83   virtual const char* translated_shader() const = 0;
84   virtual const char* info_log() const = 0;
85
86   virtual const VariableMap& attrib_map() const = 0;
87   virtual const VariableMap& uniform_map() const = 0;
88   virtual const VariableMap& varying_map() const = 0;
89   virtual const NameMap& name_map() const = 0;
90
91   // Return a string that is unique for a specfic set of options that would
92   // possibly effect compilation.
93   virtual std::string GetStringForOptionsThatWouldEffectCompilation() const = 0;
94
95  protected:
96   virtual ~ShaderTranslatorInterface() {}
97 };
98
99 // Implementation of ShaderTranslatorInterface
100 class GPU_EXPORT ShaderTranslator
101     : public base::RefCounted<ShaderTranslator>,
102       NON_EXPORTED_BASE(public ShaderTranslatorInterface) {
103  public:
104   class DestructionObserver {
105    public:
106     DestructionObserver();
107     virtual ~DestructionObserver();
108
109     virtual void OnDestruct(ShaderTranslator* translator) = 0;
110
111    private:
112     DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
113   };
114
115   ShaderTranslator();
116
117   // Overridden from ShaderTranslatorInterface.
118   virtual bool Init(
119       ShShaderType shader_type,
120       ShShaderSpec shader_spec,
121       const ShBuiltInResources* resources,
122       GlslImplementationType glsl_implementation_type,
123       ShCompileOptions driver_bug_workarounds) OVERRIDE;
124
125   // Overridden from ShaderTranslatorInterface.
126   virtual bool Translate(const char* shader) OVERRIDE;
127
128   // Overridden from ShaderTranslatorInterface.
129   virtual const char* translated_shader() const OVERRIDE;
130   virtual const char* info_log() const OVERRIDE;
131
132   // Overridden from ShaderTranslatorInterface.
133   virtual const VariableMap& attrib_map() const OVERRIDE;
134   virtual const VariableMap& uniform_map() const OVERRIDE;
135   virtual const VariableMap& varying_map() const OVERRIDE;
136   virtual const NameMap& name_map() const OVERRIDE;
137
138   virtual std::string GetStringForOptionsThatWouldEffectCompilation() const
139       OVERRIDE;
140
141   void AddDestructionObserver(DestructionObserver* observer);
142   void RemoveDestructionObserver(DestructionObserver* observer);
143
144  private:
145   friend class base::RefCounted<ShaderTranslator>;
146
147   virtual ~ShaderTranslator();
148   void ClearResults();
149   int GetCompileOptions() const;
150
151   ShHandle compiler_;
152   ShBuiltInResources compiler_options_;
153   scoped_ptr<char[]> translated_shader_;
154   scoped_ptr<char[]> info_log_;
155   VariableMap attrib_map_;
156   VariableMap uniform_map_;
157   VariableMap varying_map_;
158   NameMap name_map_;
159   bool implementation_is_glsl_es_;
160   ShCompileOptions driver_bug_workarounds_;
161   ObserverList<DestructionObserver> destruction_observers_;
162
163   DISALLOW_COPY_AND_ASSIGN(ShaderTranslator);
164 };
165
166 }  // namespace gles2
167 }  // namespace gpu
168
169 #endif  // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_
170