1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
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.
18 #ifndef LIBGLESV2_PROGRAM_H_
19 #define LIBGLESV2_PROGRAM_H_
23 #include "Shader/PixelShader.hpp"
24 #include "Shader/VertexShader.hpp"
29 #include <unordered_map>
34 class ResourceManager;
38 // Helper struct representing a single shader uniform
43 BlockInfo(const glsl::Uniform& uniform, int blockIndex);
49 bool isRowMajorMatrix;
52 Uniform(GLenum type, GLenum precision, const std::string &name, unsigned int arraySize,
53 const BlockInfo &blockInfo);
59 int registerCount() const;
62 const GLenum precision;
63 const std::string name;
64 const unsigned int arraySize;
65 const BlockInfo blockInfo;
70 short psRegisterIndex;
71 short vsRegisterIndex;
74 // Helper struct representing a single shader uniform block
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);
80 void setRegisterIndex(GLenum shader, unsigned int registerIndex);
82 bool isArrayElement() const;
83 bool isReferencedByVertexShader() const;
84 bool isReferencedByFragmentShader() const;
86 const std::string name;
87 const unsigned int elementIndex;
88 const unsigned int dataSize;
90 std::vector<unsigned int> memberUniformIndexes;
92 unsigned int psRegisterIndex;
93 unsigned int vsRegisterIndex;
96 // Struct used for correlating uniforms/elements of uniform arrays to handles
97 struct UniformLocation
99 UniformLocation(const std::string &name, unsigned int element, unsigned int index);
102 unsigned int element;
109 LinkedVarying(const std::string &name, GLenum type, GLsizei size, int reg, int col);
117 int reg; // First varying register, assigned during link
118 int col; // First register element, assigned during link
124 Program(ResourceManager *manager, GLuint handle);
128 bool attachShader(Shader *shader);
129 bool detachShader(Shader *shader);
130 int getAttachedShadersCount() const;
132 sw::PixelShader *getPixelShader();
133 sw::VertexShader *getVertexShader();
135 void bindAttributeLocation(GLuint index, const char *name);
136 GLint getAttributeLocation(const char *name);
137 int getAttributeStream(int attributeIndex);
139 GLint getSamplerMapping(sw::SamplerType type, unsigned int samplerIndex);
140 TextureType getSamplerTextureType(sw::SamplerType type, unsigned int samplerIndex);
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;
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);
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);
176 void dirtyAllUniforms();
177 void applyUniforms(Device *device);
178 void applyUniformBuffers(Device *device, BufferBinding* uniformBuffers);
179 void applyTransformFeedback(Device *device, TransformFeedback* transformFeedback);
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);
187 GLint getFragDataLocation(const GLchar *name);
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;
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;
198 void getActiveUniformBlockName(GLuint index, GLsizei bufSize, GLsizei *length, GLchar *name) const;
199 size_t getActiveUniformBlockCount() const;
200 GLint getActiveUniformBlockMaxLength() const;
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;
210 unsigned int getRefCount() const;
211 void flagForDeletion();
212 bool isFlaggedForDeletion() const;
214 void validate(Device* device);
215 bool validateSamplers(bool logErrors);
216 bool isValidated() const;
218 unsigned int getSerial() const;
220 bool getBinaryRetrievableHint() const { return retrievableBinary; }
221 void setBinaryRetrievable(bool retrievable) { retrievableBinary = retrievable; }
222 GLint getBinaryLength() const;
226 void resetUniformBlockBindings();
229 bool linkTransformFeedback();
231 bool linkAttributes();
232 int getAttributeBinding(const glsl::Attribute &attribute);
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);
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);
271 void appendToInfoLog(const char *info, ...);
274 static unsigned int issueSerial();
277 FragmentShader *fragmentShader;
278 VertexShader *vertexShader;
280 sw::PixelShader *pixelBinary;
281 sw::VertexShader *vertexBinary;
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];
288 GLuint uniformBlockBindings[MAX_UNIFORM_BUFFER_BINDINGS];
290 std::vector<std::string> transformFeedbackVaryings;
291 GLenum transformFeedbackBufferMode;
292 size_t totalLinkedVaryingsComponents;
297 GLint logicalTextureUnit;
298 TextureType textureType;
301 Sampler samplersPS[MAX_TEXTURE_IMAGE_UNITS];
302 Sampler samplersVS[MAX_VERTEX_TEXTURE_IMAGE_UNITS];
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;
314 bool orphaned; // Flag to indicate that the program can be deleted when no longer in use
317 bool retrievableBinary;
319 unsigned int referenceCount;
320 const unsigned int serial;
322 static unsigned int currentSerial;
324 ResourceManager *resourceManager;
329 #endif // LIBGLESV2_PROGRAM_H_