Home | History | Annotate | Download | only in service
      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_dx11/include/GLSLANG/ShaderLang.h"
     17 
     18 namespace gpu {
     19 namespace gles2 {
     20 
     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
     28   };
     29 
     30   enum GlslBuiltInFunctionBehavior {
     31     kGlslBuiltInFunctionOriginal,
     32     kGlslBuiltInFunctionEmulated
     33   };
     34 
     35   struct VariableInfo {
     36     VariableInfo()
     37         : type(0),
     38           size(0) {
     39     }
     40 
     41     VariableInfo(int _type, int _size, std::string _name)
     42         : type(_type),
     43           size(_size),
     44           name(_name) {
     45     }
     46     bool operator==(
     47         const ShaderTranslatorInterface::VariableInfo& other) const {
     48       return type == other.type &&
     49           size == other.size &&
     50           strcmp(name.c_str(), other.name.c_str()) == 0;
     51     }
     52 
     53     int type;
     54     int size;
     55     std::string name;  // name in the original shader source.
     56   };
     57 
     58   // Mapping between variable name and info.
     59   typedef base::hash_map<std::string, VariableInfo> VariableMap;
     60   // Mapping between hashed name and original name.
     61   typedef base::hash_map<std::string, std::string> NameMap;
     62 
     63   // Initializes the translator.
     64   // Must be called once before using the translator object.
     65   virtual bool Init(
     66       ShShaderType shader_type,
     67       ShShaderSpec shader_spec,
     68       const ShBuiltInResources* resources,
     69       GlslImplementationType glsl_implementation_type,
     70       GlslBuiltInFunctionBehavior glsl_built_in_function_behavior) = 0;
     71 
     72   // Translates the given shader source.
     73   // Returns true if translation is successful, false otherwise.
     74   virtual bool Translate(const char* shader) = 0;
     75 
     76   // The following functions return results from the last translation.
     77   // The results are NULL/empty if the translation was unsuccessful.
     78   // A valid info-log is always returned irrespective of whether translation
     79   // was successful or not.
     80   virtual const char* translated_shader() const = 0;
     81   virtual const char* info_log() const = 0;
     82 
     83   virtual const VariableMap& attrib_map() const = 0;
     84   virtual const VariableMap& uniform_map() const = 0;
     85   virtual const NameMap& name_map() const = 0;
     86 
     87   // Return a string that is unique for a specfic set of options that would
     88   // possibly effect compilation.
     89   virtual std::string GetStringForOptionsThatWouldEffectCompilation() const = 0;
     90 
     91  protected:
     92   virtual ~ShaderTranslatorInterface() {}
     93 };
     94 
     95 // Implementation of ShaderTranslatorInterface
     96 class GPU_EXPORT ShaderTranslator
     97     : public base::RefCounted<ShaderTranslator>,
     98       NON_EXPORTED_BASE(public ShaderTranslatorInterface) {
     99  public:
    100   class DestructionObserver {
    101    public:
    102     DestructionObserver();
    103     virtual ~DestructionObserver();
    104 
    105     virtual void OnDestruct(ShaderTranslator* translator) = 0;
    106 
    107    private:
    108     DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
    109   };
    110 
    111   ShaderTranslator();
    112 
    113   // Overridden from ShaderTranslatorInterface.
    114   virtual bool Init(
    115       ShShaderType shader_type,
    116       ShShaderSpec shader_spec,
    117       const ShBuiltInResources* resources,
    118       GlslImplementationType glsl_implementation_type,
    119       GlslBuiltInFunctionBehavior glsl_built_in_function_behavior) OVERRIDE;
    120 
    121   // Overridden from ShaderTranslatorInterface.
    122   virtual bool Translate(const char* shader) OVERRIDE;
    123 
    124   // Overridden from ShaderTranslatorInterface.
    125   virtual const char* translated_shader() const OVERRIDE;
    126   virtual const char* info_log() const OVERRIDE;
    127 
    128   // Overridden from ShaderTranslatorInterface.
    129   virtual const VariableMap& attrib_map() const OVERRIDE;
    130   virtual const VariableMap& uniform_map() const OVERRIDE;
    131   virtual const NameMap& name_map() const OVERRIDE;
    132 
    133   virtual std::string GetStringForOptionsThatWouldEffectCompilation() const
    134       OVERRIDE;
    135 
    136   void AddDestructionObserver(DestructionObserver* observer);
    137   void RemoveDestructionObserver(DestructionObserver* observer);
    138 
    139  private:
    140   friend class base::RefCounted<ShaderTranslator>;
    141 
    142   virtual ~ShaderTranslator();
    143   void ClearResults();
    144   int GetCompileOptions() const;
    145 
    146   ShHandle compiler_;
    147   ShBuiltInResources compiler_options_;
    148   scoped_ptr<char[]> translated_shader_;
    149   scoped_ptr<char[]> info_log_;
    150   VariableMap attrib_map_;
    151   VariableMap uniform_map_;
    152   NameMap name_map_;
    153   bool implementation_is_glsl_es_;
    154   bool needs_built_in_function_emulation_;
    155   ObserverList<DestructionObserver> destruction_observers_;
    156 
    157   DISALLOW_COPY_AND_ASSIGN(ShaderTranslator);
    158 };
    159 
    160 }  // namespace gles2
    161 }  // namespace gpu
    162 
    163 #endif  // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_H_
    164 
    165