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"
25 // Mapping between variable name and info.
26 typedef base::hash_map
<std::string
, sh::Attribute
> AttributeMap
;
27 typedef base::hash_map
<std::string
, sh::Uniform
> UniformMap
;
28 typedef base::hash_map
<std::string
, sh::Varying
> VaryingMap
;
29 // Mapping between hashed name and original name.
30 typedef base::hash_map
<std::string
, std::string
> NameMap
;
32 // Translates a GLSL ES 2.0 shader to desktop GLSL shader, or just
33 // validates GLSL ES 2.0 shaders on a true GLSL ES implementation.
34 class ShaderTranslatorInterface
35 : public base::RefCounted
<ShaderTranslatorInterface
> {
37 ShaderTranslatorInterface() {}
39 // Initializes the translator.
40 // Must be called once before using the translator object.
41 virtual bool Init(sh::GLenum shader_type
,
42 ShShaderSpec shader_spec
,
43 const ShBuiltInResources
* resources
,
44 ShShaderOutput shader_output_language
,
45 ShCompileOptions driver_bug_workarounds
) = 0;
47 // Translates the given shader source.
48 // Returns true if translation is successful, false otherwise.
49 // Always fill |info_log| if it's non-null.
50 // Upon success, fill |translated_shader|, |attrib_map|, |uniform_map|,
51 // |varying_map|, and |name_map| if they are non-null.
52 virtual bool Translate(const std::string
& shader_source
,
53 std::string
* info_log
,
54 std::string
* translated_shader
,
56 AttributeMap
* attrib_map
,
57 UniformMap
* uniform_map
,
58 VaryingMap
* varying_map
,
59 NameMap
* name_map
) const = 0;
61 // Return a string that is unique for a specfic set of options that would
62 // possibly affect compilation.
63 virtual std::string
GetStringForOptionsThatWouldAffectCompilation() const = 0;
66 virtual ~ShaderTranslatorInterface() {}
69 friend class base::RefCounted
<ShaderTranslatorInterface
>;
70 DISALLOW_COPY_AND_ASSIGN(ShaderTranslatorInterface
);
73 // Implementation of ShaderTranslatorInterface
74 class GPU_EXPORT ShaderTranslator
75 : NON_EXPORTED_BASE(public ShaderTranslatorInterface
) {
77 class DestructionObserver
{
79 DestructionObserver();
80 virtual ~DestructionObserver();
82 virtual void OnDestruct(ShaderTranslator
* translator
) = 0;
85 DISALLOW_COPY_AND_ASSIGN(DestructionObserver
);
90 // Return shader output lanaguage type based on the context version.
91 static ShShaderOutput
GetShaderOutputLanguageForContext(
92 const gfx::GLVersionInfo
& context_version
);
94 // Overridden from ShaderTranslatorInterface.
95 bool Init(sh::GLenum shader_type
,
96 ShShaderSpec shader_spec
,
97 const ShBuiltInResources
* resources
,
98 ShShaderOutput shader_output_language
,
99 ShCompileOptions driver_bug_workarounds
) override
;
101 // Overridden from ShaderTranslatorInterface.
102 bool Translate(const std::string
& shader_source
,
103 std::string
* info_log
,
104 std::string
* translated_source
,
106 AttributeMap
* attrib_map
,
107 UniformMap
* uniform_map
,
108 VaryingMap
* varying_map
,
109 NameMap
* name_map
) const override
;
111 std::string
GetStringForOptionsThatWouldAffectCompilation() const override
;
113 void AddDestructionObserver(DestructionObserver
* observer
);
114 void RemoveDestructionObserver(DestructionObserver
* observer
);
117 ~ShaderTranslator() override
;
119 int GetCompileOptions() const;
122 ShCompileOptions driver_bug_workarounds_
;
123 base::ObserverList
<DestructionObserver
> destruction_observers_
;
129 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_