Handle account removal correctly on all platforms.
[chromium-blink-merge.git] / gpu / command_buffer / service / shader_translator.h
blob77e04ab01a4551f3cbafe44bff023d1f71d7ee37
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 sh::GLenum shader_type,
70 ShShaderSpec shader_spec,
71 const ShBuiltInResources* resources,
72 GlslImplementationType glsl_implementation_type,
73 ShCompileOptions driver_bug_workarounds) = 0;
75 // Translates the given shader source.
76 // Returns true if translation is successful, false otherwise.
77 // Always fill |info_log| if it's non-null.
78 // Upon success, fill |translated_shader|, |attrib_map|, |uniform_map|,
79 // |varying_map|, and |name_map| if they are non-null.
80 virtual bool Translate(const std::string& shader_source,
81 std::string* info_log,
82 std::string* translated_shader,
83 VariableMap* attrib_map,
84 VariableMap* uniform_map,
85 VariableMap* varying_map,
86 NameMap* name_map) const = 0;
88 // Return a string that is unique for a specfic set of options that would
89 // possibly affect compilation.
90 virtual std::string GetStringForOptionsThatWouldAffectCompilation() const = 0;
92 protected:
93 virtual ~ShaderTranslatorInterface() {}
96 // Implementation of ShaderTranslatorInterface
97 class GPU_EXPORT ShaderTranslator
98 : public base::RefCounted<ShaderTranslator>,
99 NON_EXPORTED_BASE(public ShaderTranslatorInterface) {
100 public:
101 class DestructionObserver {
102 public:
103 DestructionObserver();
104 virtual ~DestructionObserver();
106 virtual void OnDestruct(ShaderTranslator* translator) = 0;
108 private:
109 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
112 ShaderTranslator();
114 // Overridden from ShaderTranslatorInterface.
115 virtual bool Init(
116 sh::GLenum shader_type,
117 ShShaderSpec shader_spec,
118 const ShBuiltInResources* resources,
119 GlslImplementationType glsl_implementation_type,
120 ShCompileOptions driver_bug_workarounds) OVERRIDE;
122 // Overridden from ShaderTranslatorInterface.
123 virtual bool Translate(const std::string& shader_source,
124 std::string* info_log,
125 std::string* translated_source,
126 VariableMap* attrib_map,
127 VariableMap* uniform_map,
128 VariableMap* varying_map,
129 NameMap* name_map) const OVERRIDE;
131 virtual std::string GetStringForOptionsThatWouldAffectCompilation() const
132 OVERRIDE;
134 void AddDestructionObserver(DestructionObserver* observer);
135 void RemoveDestructionObserver(DestructionObserver* observer);
137 private:
138 friend class base::RefCounted<ShaderTranslator>;
140 virtual ~ShaderTranslator();
141 int GetCompileOptions() const;
143 ShHandle compiler_;
144 ShBuiltInResources compiler_options_;
145 bool implementation_is_glsl_es_;
146 ShCompileOptions driver_bug_workarounds_;
147 ObserverList<DestructionObserver> destruction_observers_;
149 DISALLOW_COPY_AND_ASSIGN(ShaderTranslator);
152 } // namespace gles2
153 } // namespace gpu
155 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_