Upstream version 11.40.277.0
[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/include/GLSLANG/ShaderLang.h"
17
18 namespace gpu {
19 namespace gles2 {
20
21 // Mapping between variable name and info.
22 typedef base::hash_map<std::string, sh::Attribute> AttributeMap;
23 typedef base::hash_map<std::string, sh::Uniform> UniformMap;
24 typedef base::hash_map<std::string, sh::Varying> VaryingMap;
25 // Mapping between hashed name and original name.
26 typedef base::hash_map<std::string, std::string> NameMap;
27
28 // Translates a GLSL ES 2.0 shader to desktop GLSL shader, or just
29 // validates GLSL ES 2.0 shaders on a true GLSL ES implementation.
30 class ShaderTranslatorInterface {
31  public:
32   enum GlslImplementationType {
33     kGlsl,
34     kGlslES
35   };
36
37   // Initializes the translator.
38   // Must be called once before using the translator object.
39   virtual bool Init(
40       sh::GLenum shader_type,
41       ShShaderSpec shader_spec,
42       const ShBuiltInResources* resources,
43       GlslImplementationType glsl_implementation_type,
44       ShCompileOptions driver_bug_workarounds) = 0;
45
46   // Translates the given shader source.
47   // Returns true if translation is successful, false otherwise.
48   // Always fill |info_log| if it's non-null.
49   // Upon success, fill |translated_shader|, |attrib_map|, |uniform_map|,
50   // |varying_map|, and |name_map| if they are non-null.
51   virtual bool Translate(const std::string& shader_source,
52                          std::string* info_log,
53                          std::string* translated_shader,
54                          AttributeMap* attrib_map,
55                          UniformMap* uniform_map,
56                          VaryingMap* varying_map,
57                          NameMap* name_map) const = 0;
58
59   // Return a string that is unique for a specfic set of options that would
60   // possibly affect compilation.
61   virtual std::string GetStringForOptionsThatWouldAffectCompilation() const = 0;
62
63  protected:
64   virtual ~ShaderTranslatorInterface() {}
65 };
66
67 // Implementation of ShaderTranslatorInterface
68 class GPU_EXPORT ShaderTranslator
69     : public base::RefCounted<ShaderTranslator>,
70       NON_EXPORTED_BASE(public ShaderTranslatorInterface) {
71  public:
72   class DestructionObserver {
73    public:
74     DestructionObserver();
75     virtual ~DestructionObserver();
76
77     virtual void OnDestruct(ShaderTranslator* translator) = 0;
78
79    private:
80     DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
81   };
82
83   ShaderTranslator();
84
85   // Overridden from ShaderTranslatorInterface.
86   bool Init(sh::GLenum shader_type,
87             ShShaderSpec shader_spec,
88             const ShBuiltInResources* resources,
89             GlslImplementationType glsl_implementation_type,
90             ShCompileOptions driver_bug_workarounds) override;
91
92   // Overridden from ShaderTranslatorInterface.
93   bool Translate(const std::string& shader_source,
94                  std::string* info_log,
95                  std::string* translated_source,
96                  AttributeMap* attrib_map,
97                  UniformMap* uniform_map,
98                  VaryingMap* varying_map,
99                  NameMap* name_map) const override;
100
101   std::string GetStringForOptionsThatWouldAffectCompilation() const override;
102
103   void AddDestructionObserver(DestructionObserver* observer);
104   void RemoveDestructionObserver(DestructionObserver* observer);
105
106  private:
107   friend class base::RefCounted<ShaderTranslator>;
108
109   ~ShaderTranslator() override;
110   int GetCompileOptions() const;
111
112   ShHandle compiler_;
113   ShBuiltInResources compiler_options_;
114   bool implementation_is_glsl_es_;
115   ShCompileOptions driver_bug_workarounds_;
116   ObserverList<DestructionObserver> destruction_observers_;
117
118   DISALLOW_COPY_AND_ASSIGN(ShaderTranslator);
119 };
120
121 }  // namespace gles2
122 }  // namespace gpu
123
124 #endif  // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_
125