OSDN Git Service

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