• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2002-2010 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 <d3dx9.h>
18 #include <list>
19 #include <vector>
20 
21 #include "libGLESv2/ResourceManager.h"
22 
23 namespace gl
24 {
25 struct Varying
26 {
VaryingVarying27     Varying(GLenum type, const std::string &name, int size, bool array)
28         : type(type), name(name), size(size), array(array), reg(-1), col(-1)
29     {
30     }
31 
32     GLenum type;
33     std::string name;
34     int size;   // Number of 'type' elements
35     bool array;
36 
37     int reg;    // First varying register, assigned during link
38     int col;    // First register element, assigned during link
39 };
40 
41 typedef std::list<Varying> VaryingList;
42 
43 class Shader
44 {
45     friend Program;
46 
47   public:
48     Shader(ResourceManager *manager, GLuint handle);
49 
50     virtual ~Shader();
51 
52     virtual GLenum getType() = 0;
53     GLuint getHandle() const;
54 
55     void deleteSource();
56     void setSource(GLsizei count, const char **string, const GLint *length);
57     int getInfoLogLength() const;
58     void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog);
59     int getSourceLength() const;
60     void getSource(GLsizei bufSize, GLsizei *length, char *source);
61 
62     virtual void compile() = 0;
63     bool isCompiled();
64     const char *getHLSL();
65 
66     void addRef();
67     void release();
68     unsigned int getRefCount() const;
69     bool isFlaggedForDeletion() const;
70     void flagForDeletion();
71 
72     static void releaseCompiler();
73 
74   protected:
75     DISALLOW_COPY_AND_ASSIGN(Shader);
76 
77     void parseVaryings();
78 
79     void compileToHLSL(void *compiler);
80 
81     static GLenum parseType(const std::string &type);
82     static bool compareVarying(const Varying &x, const Varying &y);
83 
84     const GLuint mHandle;
85     unsigned int mRefCount;     // Number of program objects this shader is attached to
86     bool mDeleteStatus;         // Flag to indicate that the shader can be deleted when no longer in use
87 
88     char *mSource;
89     char *mHlsl;
90     char *mInfoLog;
91 
92     VaryingList varyings;
93 
94     bool mUsesFragCoord;
95     bool mUsesFrontFacing;
96     bool mUsesPointSize;
97     bool mUsesPointCoord;
98 
99     ResourceManager *mResourceManager;
100 
101     static void *mFragmentCompiler;
102     static void *mVertexCompiler;
103 };
104 
105 struct Attribute
106 {
AttributeAttribute107     Attribute() : type(GL_NONE), name("")
108     {
109     }
110 
AttributeAttribute111     Attribute(GLenum type, const std::string &name) : type(type), name(name)
112     {
113     }
114 
115     GLenum type;
116     std::string name;
117 };
118 
119 typedef std::vector<Attribute> AttributeArray;
120 
121 class VertexShader : public Shader
122 {
123     friend Program;
124 
125   public:
126     VertexShader(ResourceManager *manager, GLuint handle);
127 
128     ~VertexShader();
129 
130     GLenum getType();
131     void compile();
132     int getSemanticIndex(const std::string &attributeName);
133 
134   private:
135     DISALLOW_COPY_AND_ASSIGN(VertexShader);
136 
137     void parseAttributes();
138 
139     AttributeArray mAttributes;
140 };
141 
142 class FragmentShader : public Shader
143 {
144   public:
145     FragmentShader(ResourceManager *manager, GLuint handle);
146 
147     ~FragmentShader();
148 
149     GLenum getType();
150     void compile();
151 
152   private:
153     DISALLOW_COPY_AND_ASSIGN(FragmentShader);
154 };
155 }
156 
157 #endif   // LIBGLESV2_SHADER_H_
158