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 // 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;
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 {
32 enum GlslImplementationType {
37 // Initializes the translator.
38 // Must be called once before using the translator object.
40 sh::GLenum shader_type,
41 ShShaderSpec shader_spec,
42 const ShBuiltInResources* resources,
43 GlslImplementationType glsl_implementation_type,
44 ShCompileOptions driver_bug_workarounds) = 0;
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;
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;
64 virtual ~ShaderTranslatorInterface() {}
67 // Implementation of ShaderTranslatorInterface
68 class GPU_EXPORT ShaderTranslator
69 : public base::RefCounted<ShaderTranslator>,
70 NON_EXPORTED_BASE(public ShaderTranslatorInterface) {
72 class DestructionObserver {
74 DestructionObserver();
75 virtual ~DestructionObserver();
77 virtual void OnDestruct(ShaderTranslator* translator) = 0;
80 DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
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;
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;
101 std::string GetStringForOptionsThatWouldAffectCompilation() const override;
103 void AddDestructionObserver(DestructionObserver* observer);
104 void RemoveDestructionObserver(DestructionObserver* observer);
107 friend class base::RefCounted<ShaderTranslator>;
109 ~ShaderTranslator() override;
110 int GetCompileOptions() const;
113 ShBuiltInResources compiler_options_;
114 bool implementation_is_glsl_es_;
115 ShCompileOptions driver_bug_workarounds_;
116 ObserverList<DestructionObserver> destruction_observers_;
118 DISALLOW_COPY_AND_ASSIGN(ShaderTranslator);
124 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_