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
31 : public base::RefCounted
<ShaderTranslatorInterface
> {
33 ShaderTranslatorInterface() {}
34 enum GlslImplementationType
{
39 // Initializes the translator.
40 // Must be called once before using the translator object.
42 sh::GLenum shader_type
,
43 ShShaderSpec shader_spec
,
44 const ShBuiltInResources
* resources
,
45 GlslImplementationType glsl_implementation_type
,
46 ShCompileOptions driver_bug_workarounds
) = 0;
48 // Translates the given shader source.
49 // Returns true if translation is successful, false otherwise.
50 // Always fill |info_log| if it's non-null.
51 // Upon success, fill |translated_shader|, |attrib_map|, |uniform_map|,
52 // |varying_map|, and |name_map| if they are non-null.
53 virtual bool Translate(const std::string
& shader_source
,
54 std::string
* info_log
,
55 std::string
* translated_shader
,
57 AttributeMap
* attrib_map
,
58 UniformMap
* uniform_map
,
59 VaryingMap
* varying_map
,
60 NameMap
* name_map
) const = 0;
62 // Return a string that is unique for a specfic set of options that would
63 // possibly affect compilation.
64 virtual std::string
GetStringForOptionsThatWouldAffectCompilation() const = 0;
67 virtual ~ShaderTranslatorInterface() {}
70 friend class base::RefCounted
<ShaderTranslatorInterface
>;
71 DISALLOW_COPY_AND_ASSIGN(ShaderTranslatorInterface
);
74 // Implementation of ShaderTranslatorInterface
75 class GPU_EXPORT ShaderTranslator
76 : NON_EXPORTED_BASE(public ShaderTranslatorInterface
) {
78 class DestructionObserver
{
80 DestructionObserver();
81 virtual ~DestructionObserver();
83 virtual void OnDestruct(ShaderTranslator
* translator
) = 0;
86 DISALLOW_COPY_AND_ASSIGN(DestructionObserver
);
91 // Overridden from ShaderTranslatorInterface.
92 bool Init(sh::GLenum shader_type
,
93 ShShaderSpec shader_spec
,
94 const ShBuiltInResources
* resources
,
95 GlslImplementationType glsl_implementation_type
,
96 ShCompileOptions driver_bug_workarounds
) override
;
98 // Overridden from ShaderTranslatorInterface.
99 bool Translate(const std::string
& shader_source
,
100 std::string
* info_log
,
101 std::string
* translated_source
,
103 AttributeMap
* attrib_map
,
104 UniformMap
* uniform_map
,
105 VaryingMap
* varying_map
,
106 NameMap
* name_map
) const override
;
108 std::string
GetStringForOptionsThatWouldAffectCompilation() const override
;
110 void AddDestructionObserver(DestructionObserver
* observer
);
111 void RemoveDestructionObserver(DestructionObserver
* observer
);
114 ~ShaderTranslator() override
;
116 int GetCompileOptions() const;
119 bool implementation_is_glsl_es_
;
120 ShCompileOptions driver_bug_workarounds_
;
121 ObserverList
<DestructionObserver
> destruction_observers_
;
127 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_