1 // 2 // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 7 // Shader.h: Defines the abstract gl::Shader class and its concrete derived 8 // classes VertexShader and FragmentShader. Implements GL shader objects and 9 // related functionality. [OpenGL ES 2.0.24] section 2.10 page 24 and section 10 // 3.8 page 84. 11 12 #ifndef LIBGLESV2_SHADER_H_ 13 #define LIBGLESV2_SHADER_H_ 14 15 #define GL_APICALL 16 #include <GLES2/gl2.h> 17 #include <string> 18 #include <list> 19 #include <vector> 20 21 #include "compiler/Uniform.h" 22 #include "common/angleutils.h" 23 24 namespace rx 25 { 26 class Renderer; 27 } 28 29 namespace gl 30 { 31 class ResourceManager; 32 33 struct Varying 34 { 35 Varying(GLenum type, const std::string &name, int size, bool array) 36 : type(type), name(name), size(size), array(array), reg(-1), col(-1) 37 { 38 } 39 40 GLenum type; 41 std::string name; 42 int size; // Number of 'type' elements 43 bool array; 44 45 int reg; // First varying register, assigned during link 46 int col; // First register element, assigned during link 47 }; 48 49 typedef std::list<Varying> VaryingList; 50 51 class Shader 52 { 53 friend class ProgramBinary; 54 55 public: 56 Shader(ResourceManager *manager, const rx::Renderer *renderer, GLuint handle); 57 58 virtual ~Shader(); 59 60 virtual GLenum getType() = 0; 61 GLuint getHandle() const; 62 63 void deleteSource(); 64 void setSource(GLsizei count, const char **string, const GLint *length); 65 int getInfoLogLength() const; 66 void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog); 67 int getSourceLength() const; 68 void getSource(GLsizei bufSize, GLsizei *length, char *buffer); 69 int getTranslatedSourceLength() const; 70 void getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer); 71 const sh::ActiveUniforms &getUniforms(); 72 73 virtual void compile() = 0; 74 virtual void uncompile(); 75 bool isCompiled(); 76 const char *getHLSL(); 77 78 void addRef(); 79 void release(); 80 unsigned int getRefCount() const; 81 bool isFlaggedForDeletion() const; 82 void flagForDeletion(); 83 84 static void releaseCompiler(); 85 86 protected: 87 void parseVaryings(); 88 void resetVaryingsRegisterAssignment(); 89 90 void compileToHLSL(void *compiler); 91 92 void getSourceImpl(char *source, GLsizei bufSize, GLsizei *length, char *buffer); 93 94 static GLenum parseType(const std::string &type); 95 static bool compareVarying(const Varying &x, const Varying &y); 96 97 const rx::Renderer *const mRenderer; 98 99 VaryingList mVaryings; 100 101 bool mUsesMultipleRenderTargets; 102 bool mUsesFragColor; 103 bool mUsesFragData; 104 bool mUsesFragCoord; 105 bool mUsesFrontFacing; 106 bool mUsesPointSize; 107 bool mUsesPointCoord; 108 bool mUsesDepthRange; 109 bool mUsesFragDepth; 110 bool mUsesDiscardRewriting; 111 112 static void *mFragmentCompiler; 113 static void *mVertexCompiler; 114 115 private: 116 DISALLOW_COPY_AND_ASSIGN(Shader); 117 118 void initializeCompiler(); 119 120 const GLuint mHandle; 121 unsigned int mRefCount; // Number of program objects this shader is attached to 122 bool mDeleteStatus; // Flag to indicate that the shader can be deleted when no longer in use 123 124 char *mSource; 125 char *mHlsl; 126 char *mInfoLog; 127 sh::ActiveUniforms mActiveUniforms; 128 129 ResourceManager *mResourceManager; 130 }; 131 132 struct Attribute 133 { 134 Attribute() : type(GL_NONE), name("") 135 { 136 } 137 138 Attribute(GLenum type, const std::string &name) : type(type), name(name) 139 { 140 } 141 142 GLenum type; 143 std::string name; 144 }; 145 146 typedef std::vector<Attribute> AttributeArray; 147 148 class VertexShader : public Shader 149 { 150 friend class ProgramBinary; 151 152 public: 153 VertexShader(ResourceManager *manager, const rx::Renderer *renderer, GLuint handle); 154 155 ~VertexShader(); 156 157 virtual GLenum getType(); 158 virtual void compile(); 159 virtual void uncompile(); 160 int getSemanticIndex(const std::string &attributeName); 161 162 private: 163 DISALLOW_COPY_AND_ASSIGN(VertexShader); 164 165 void parseAttributes(); 166 167 AttributeArray mAttributes; 168 }; 169 170 class FragmentShader : public Shader 171 { 172 public: 173 FragmentShader(ResourceManager *manager,const rx::Renderer *renderer, GLuint handle); 174 175 ~FragmentShader(); 176 177 virtual GLenum getType(); 178 virtual void compile(); 179 180 private: 181 DISALLOW_COPY_AND_ASSIGN(FragmentShader); 182 }; 183 } 184 185 #endif // LIBGLESV2_SHADER_H_ 186