1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Shader.h: Defines the abstract Shader class and its concrete derived
16 // classes VertexShader and FragmentShader. Implements GL shader objects and
17 // related functionality. [OpenGL ES 2.0.24] section 2.10 page 24 and section
18 // 3.8 page 84.
19 
20 #ifndef LIBGLESV2_SHADER_H_
21 #define LIBGLESV2_SHADER_H_
22 
23 #include "ResourceManager.h"
24 
25 #include "compiler/TranslatorASM.h"
26 
27 #include <GLES2/gl2.h>
28 
29 #include <string>
30 #include <list>
31 #include <mutex>
32 #include <vector>
33 
34 namespace glsl
35 {
36 	class OutputASM;
37 }
38 
39 namespace es2
40 {
41 
42 class Shader : public glsl::Shader
43 {
44 	friend class Program;
45 
46 public:
47 	Shader(ResourceManager *manager, GLuint handle);
48 
49 	virtual ~Shader();
50 
51 	virtual GLenum getType() const = 0;
52 	GLuint getName() const;
53 
54 	void deleteSource();
55 	void setSource(GLsizei count, const char *const *string, const GLint *length);
56 	size_t getInfoLogLength() const;
57 	void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog);
58 	size_t getSourceLength() const;
59 	void getSource(GLsizei bufSize, GLsizei *length, char *source);
60 
61 	void compile();
62 	bool isCompiled();
63 
64 	void addRef();
65 	void release();
66 	unsigned int getRefCount() const;
67 	bool isFlaggedForDeletion() const;
68 	void flagForDeletion();
69 
70 	static void releaseCompiler();
71 
72 protected:
73 	static std::mutex mutex;
74 	static bool compilerInitialized;
75 
76 	TranslatorASM *createCompiler(GLenum shaderType);
77 	void clear();
78 
79 	static bool compareVarying(const glsl::Varying &x, const glsl::Varying &y);
80 
81 	char *mSource;
82 	std::string infoLog;
83 
84 private:
85 	virtual void createShader() = 0;
86 	virtual void deleteShader() = 0;
87 
88 	const GLuint mHandle;
89 	unsigned int mRefCount;     // Number of program objects this shader is attached to
90 	bool mDeleteStatus;         // Flag to indicate that the shader can be deleted when no longer in use
91 
92 	ResourceManager *mResourceManager;
93 };
94 
95 class VertexShader : public Shader
96 {
97 	friend class Program;
98 
99 public:
100 	VertexShader(ResourceManager *manager, GLuint handle);
101 
102 	~VertexShader();
103 
104 	virtual GLenum getType() const;
105 	int getSemanticIndex(const std::string &attributeName) const;
106 
107 	virtual sw::Shader *getShader() const;
108 	virtual sw::VertexShader *getVertexShader() const;
109 
110 private:
111 	virtual void createShader();
112 	virtual void deleteShader();
113 
114 	sw::VertexShader *vertexShader;
115 };
116 
117 class FragmentShader : public Shader
118 {
119 public:
120 	FragmentShader(ResourceManager *manager, GLuint handle);
121 
122 	~FragmentShader();
123 
124 	virtual GLenum getType() const;
125 
126 	virtual sw::Shader *getShader() const;
127 	virtual sw::PixelShader *getPixelShader() const;
128 
129 private:
130 	virtual void createShader();
131 	virtual void deleteShader();
132 
133 	sw::PixelShader *pixelShader;
134 };
135 }
136 
137 #endif   // LIBGLESV2_SHADER_H_
138