OSDN Git Service

9d4621ee520691e9d4868c1e05f26be56e6114d5
[android-x86/external-swiftshader.git] / src / OpenGL / libGLESv2 / Program.h
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 // Program.h: Defines the Program class. Implements GL program objects
16 // and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
17
18 #ifndef LIBGLESV2_PROGRAM_H_
19 #define LIBGLESV2_PROGRAM_H_
20
21 #include "Shader.h"
22 #include "Context.h"
23 #include "Shader/PixelShader.hpp"
24 #include "Shader/VertexShader.hpp"
25
26 #include <string>
27 #include <vector>
28 #include <set>
29
30 namespace es2
31 {
32         class Device;
33         class ResourceManager;
34         class FragmentShader;
35         class VertexShader;
36
37         // Helper struct representing a single shader uniform
38         struct Uniform
39         {
40                 struct BlockInfo
41                 {
42                         BlockInfo(const glsl::Uniform& uniform, int blockIndex);
43
44                         int index;
45                         int offset;
46                         int arrayStride;
47                         int matrixStride;
48                         bool isRowMajorMatrix;
49                 };
50
51                 Uniform(GLenum type, GLenum precision, const std::string &name, unsigned int arraySize,
52                         const BlockInfo &blockInfo);
53
54                 ~Uniform();
55
56                 bool isArray() const;
57                 int size() const;
58                 int registerCount() const;
59
60                 const GLenum type;
61                 const GLenum precision;
62                 const std::string name;
63                 const unsigned int arraySize;
64                 const BlockInfo blockInfo;
65
66                 unsigned char *data;
67                 bool dirty;
68
69                 short psRegisterIndex;
70                 short vsRegisterIndex;
71         };
72
73         // Helper struct representing a single shader uniform block
74         struct UniformBlock
75         {
76                 // use GL_INVALID_INDEX for non-array elements
77                 UniformBlock(const std::string &name, unsigned int elementIndex, unsigned int dataSize, std::vector<unsigned int> memberUniformIndexes);
78
79                 void setRegisterIndex(GLenum shader, unsigned int registerIndex);
80
81                 bool isArrayElement() const;
82                 bool isReferencedByVertexShader() const;
83                 bool isReferencedByFragmentShader() const;
84
85                 const std::string name;
86                 const unsigned int elementIndex;
87                 const unsigned int dataSize;
88
89                 std::vector<unsigned int> memberUniformIndexes;
90
91                 unsigned int psRegisterIndex;
92                 unsigned int vsRegisterIndex;
93         };
94
95         // Struct used for correlating uniforms/elements of uniform arrays to handles
96         struct UniformLocation
97         {
98                 UniformLocation(const std::string &name, unsigned int element, unsigned int index);
99
100                 std::string name;
101                 unsigned int element;
102                 unsigned int index;
103         };
104
105         struct LinkedVarying
106         {
107                 LinkedVarying();
108                 LinkedVarying(const std::string &name, GLenum type, GLsizei size, int reg, int col);
109
110                 // Original GL name
111                 std::string name;
112
113                 GLenum type;
114                 GLsizei size;
115
116                 int reg;    // First varying register, assigned during link
117                 int col;    // First register element, assigned during link
118         };
119
120         class Program
121         {
122         public:
123                 Program(ResourceManager *manager, GLuint handle);
124
125                 ~Program();
126
127                 bool attachShader(Shader *shader);
128                 bool detachShader(Shader *shader);
129                 int getAttachedShadersCount() const;
130
131                 sw::PixelShader *getPixelShader();
132                 sw::VertexShader *getVertexShader();
133
134                 void bindAttributeLocation(GLuint index, const char *name);
135                 GLint getAttributeLocation(const char *name);
136                 int getAttributeStream(int attributeIndex);
137
138                 GLint getSamplerMapping(sw::SamplerType type, unsigned int samplerIndex);
139                 TextureType getSamplerTextureType(sw::SamplerType type, unsigned int samplerIndex);
140
141                 GLuint getUniformIndex(const std::string &name) const;
142                 GLuint getUniformBlockIndex(const std::string &name) const;
143                 void bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding);
144                 GLuint getUniformBlockBinding(GLuint uniformBlockIndex) const;
145                 void getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const;
146
147                 GLint getUniformLocation(const std::string &name) const;
148                 bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
149                 bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
150                 bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
151                 bool setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
152                 bool setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
153                 bool setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
154                 bool setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
155                 bool setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
156                 bool setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
157                 bool setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
158                 bool setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
159                 bool setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
160                 bool setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
161                 bool setUniform1iv(GLint location, GLsizei count, const GLint *v);
162                 bool setUniform2iv(GLint location, GLsizei count, const GLint *v);
163                 bool setUniform3iv(GLint location, GLsizei count, const GLint *v);
164                 bool setUniform4iv(GLint location, GLsizei count, const GLint *v);
165                 bool setUniform1uiv(GLint location, GLsizei count, const GLuint *v);
166                 bool setUniform2uiv(GLint location, GLsizei count, const GLuint *v);
167                 bool setUniform3uiv(GLint location, GLsizei count, const GLuint *v);
168                 bool setUniform4uiv(GLint location, GLsizei count, const GLuint *v);
169
170                 bool getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params);
171                 bool getUniformiv(GLint location, GLsizei *bufSize, GLint *params);
172                 bool getUniformuiv(GLint location, GLsizei *bufSize, GLuint *params);
173
174                 void dirtyAllUniforms();
175                 void applyUniforms(Device *device);
176                 void applyUniformBuffers(Device *device, BufferBinding* uniformBuffers);
177                 void applyTransformFeedback(Device *device, TransformFeedback* transformFeedback);
178
179                 void link();
180                 bool isLinked() const;
181                 size_t getInfoLogLength() const;
182                 void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog);
183                 void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders);
184
185                 GLint getFragDataLocation(const GLchar *name);
186
187                 void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const;
188                 size_t getActiveAttributeCount() const;
189                 GLint getActiveAttributeMaxLength() const;
190
191                 void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const;
192                 size_t getActiveUniformCount() const;
193                 GLint getActiveUniformMaxLength() const;
194                 GLint getActiveUniformi(GLuint index, GLenum pname) const;
195
196                 void getActiveUniformBlockName(GLuint index, GLsizei bufSize, GLsizei *length, GLchar *name) const;
197                 size_t getActiveUniformBlockCount() const;
198                 GLint getActiveUniformBlockMaxLength() const;
199
200                 void setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode);
201                 void getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const;
202                 GLsizei getTransformFeedbackVaryingCount() const;
203                 GLsizei getTransformFeedbackVaryingMaxLength() const;
204                 GLenum getTransformFeedbackBufferMode() const;
205
206                 void addRef();
207                 void release();
208                 unsigned int getRefCount() const;
209                 void flagForDeletion();
210                 bool isFlaggedForDeletion() const;
211
212                 void validate(Device* device);
213                 bool validateSamplers(bool logErrors);
214                 bool isValidated() const;
215
216                 unsigned int getSerial() const;
217
218                 bool getBinaryRetrievableHint() const { return retrievableBinary; }
219                 void setBinaryRetrievable(bool retrievable) { retrievableBinary = retrievable; }
220                 GLint getBinaryLength() const;
221
222         private:
223                 void unlink();
224                 void resetUniformBlockBindings();
225
226                 bool linkVaryings();
227                 bool linkTransformFeedback();
228
229                 bool linkAttributes();
230                 int getAttributeBinding(const glsl::Attribute &attribute);
231
232                 bool linkUniforms(const Shader *shader);
233                 bool linkUniformBlocks(const Shader *vertexShader, const Shader *fragmentShader);
234                 bool areMatchingUniformBlocks(const glsl::UniformBlock &block1, const glsl::UniformBlock &block2, const Shader *shader1, const Shader *shader2);
235                 bool defineUniform(GLenum shader, GLenum type, GLenum precision, const std::string &_name, unsigned int arraySize, int registerIndex, const Uniform::BlockInfo& blockInfo);
236                 bool defineUniformBlock(const Shader *shader, const glsl::UniformBlock &block);
237                 bool applyUniform(Device *device, GLint location, float* data);
238                 bool applyUniform1bv(Device *device, GLint location, GLsizei count, const GLboolean *v);
239                 bool applyUniform2bv(Device *device, GLint location, GLsizei count, const GLboolean *v);
240                 bool applyUniform3bv(Device *device, GLint location, GLsizei count, const GLboolean *v);
241                 bool applyUniform4bv(Device *device, GLint location, GLsizei count, const GLboolean *v);
242                 bool applyUniform1fv(Device *device, GLint location, GLsizei count, const GLfloat *v);
243                 bool applyUniform2fv(Device *device, GLint location, GLsizei count, const GLfloat *v);
244                 bool applyUniform3fv(Device *device, GLint location, GLsizei count, const GLfloat *v);
245                 bool applyUniform4fv(Device *device, GLint location, GLsizei count, const GLfloat *v);
246                 bool applyUniformMatrix2fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
247                 bool applyUniformMatrix2x3fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
248                 bool applyUniformMatrix2x4fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
249                 bool applyUniformMatrix3fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
250                 bool applyUniformMatrix3x2fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
251                 bool applyUniformMatrix3x4fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
252                 bool applyUniformMatrix4fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
253                 bool applyUniformMatrix4x2fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
254                 bool applyUniformMatrix4x3fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
255                 bool applyUniform1iv(Device *device, GLint location, GLsizei count, const GLint *v);
256                 bool applyUniform2iv(Device *device, GLint location, GLsizei count, const GLint *v);
257                 bool applyUniform3iv(Device *device, GLint location, GLsizei count, const GLint *v);
258                 bool applyUniform4iv(Device *device, GLint location, GLsizei count, const GLint *v);
259                 bool applyUniform1uiv(Device *device, GLint location, GLsizei count, const GLuint *v);
260                 bool applyUniform2uiv(Device *device, GLint location, GLsizei count, const GLuint *v);
261                 bool applyUniform3uiv(Device *device, GLint location, GLsizei count, const GLuint *v);
262                 bool applyUniform4uiv(Device *device, GLint location, GLsizei count, const GLuint *v);
263
264                 bool setUniformfv(GLint location, GLsizei count, const GLfloat *v, int numElements);
265                 bool setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum type);
266                 bool setUniformiv(GLint location, GLsizei count, const GLint *v, int numElements);
267                 bool setUniformuiv(GLint location, GLsizei count, const GLuint *v, int numElements);
268
269                 void appendToInfoLog(const char *info, ...);
270                 void resetInfoLog();
271
272                 static unsigned int issueSerial();
273
274         private:
275                 FragmentShader *fragmentShader;
276                 VertexShader *vertexShader;
277
278                 sw::PixelShader *pixelBinary;
279                 sw::VertexShader *vertexBinary;
280
281                 std::set<std::string> attributeBinding[MAX_VERTEX_ATTRIBS];
282                 glsl::Attribute linkedAttribute[MAX_VERTEX_ATTRIBS];
283                 int attributeStream[MAX_VERTEX_ATTRIBS];
284
285                 GLuint uniformBlockBindings[MAX_UNIFORM_BUFFER_BINDINGS];
286
287                 std::vector<std::string> transformFeedbackVaryings;
288                 GLenum transformFeedbackBufferMode;
289                 size_t totalLinkedVaryingsComponents;
290
291                 struct Sampler
292                 {
293                         bool active;
294                         GLint logicalTextureUnit;
295                         TextureType textureType;
296                 };
297
298                 Sampler samplersPS[MAX_TEXTURE_IMAGE_UNITS];
299                 Sampler samplersVS[MAX_VERTEX_TEXTURE_IMAGE_UNITS];
300
301                 typedef std::vector<Uniform*> UniformArray;
302                 UniformArray uniforms;
303                 typedef std::vector<UniformLocation> UniformIndex;
304                 UniformIndex uniformIndex;
305                 typedef std::vector<UniformBlock*> UniformBlockArray;
306                 UniformBlockArray uniformBlocks;
307                 typedef std::vector<LinkedVarying> LinkedVaryingArray;
308                 LinkedVaryingArray transformFeedbackLinkedVaryings;
309
310                 bool linked;
311                 bool orphaned;   // Flag to indicate that the program can be deleted when no longer in use
312                 char *infoLog;
313                 bool validated;
314                 bool retrievableBinary;
315
316                 unsigned int referenceCount;
317                 const unsigned int serial;
318
319                 static unsigned int currentSerial;
320
321                 ResourceManager *resourceManager;
322                 const GLuint handle;
323         };
324 }
325
326 #endif   // LIBGLESV2_PROGRAM_H_