1 // 2 // Copyright (c) 2002-2012 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 // Program.h: Defines the gl::Program class. Implements GL program objects 8 // and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28. 9 10 #ifndef LIBGLESV2_PROGRAM_BINARY_H_ 11 #define LIBGLESV2_PROGRAM_BINARY_H_ 12 13 #define GL_APICALL 14 #include <GLES2/gl2.h> 15 #include <GLES2/gl2ext.h> 16 17 #include <string> 18 #include <vector> 19 20 #include "common/RefCountObject.h" 21 #include "angletypes.h" 22 #include "libGLESv2/mathutil.h" 23 #include "libGLESv2/Uniform.h" 24 #include "libGLESv2/Shader.h" 25 #include "libGLESv2/Constants.h" 26 27 namespace rx 28 { 29 class ShaderExecutable; 30 class Renderer; 31 struct TranslatedAttribute; 32 } 33 34 namespace gl 35 { 36 class FragmentShader; 37 class VertexShader; 38 class InfoLog; 39 class AttributeBindings; 40 struct Varying; 41 42 // Struct used for correlating uniforms/elements of uniform arrays to handles 43 struct UniformLocation 44 { 45 UniformLocation() 46 { 47 } 48 49 UniformLocation(const std::string &name, unsigned int element, unsigned int index); 50 51 std::string name; 52 unsigned int element; 53 unsigned int index; 54 }; 55 56 // This is the result of linking a program. It is the state that would be passed to ProgramBinary. 57 class ProgramBinary : public RefCountObject 58 { 59 public: 60 explicit ProgramBinary(rx::Renderer *renderer); 61 ~ProgramBinary(); 62 63 rx::ShaderExecutable *getPixelExecutable(); 64 rx::ShaderExecutable *getVertexExecutable(); 65 rx::ShaderExecutable *getGeometryExecutable(); 66 67 GLuint getAttributeLocation(const char *name); 68 int getSemanticIndex(int attributeIndex); 69 70 GLint getSamplerMapping(SamplerType type, unsigned int samplerIndex); 71 TextureType getSamplerTextureType(SamplerType type, unsigned int samplerIndex); 72 GLint getUsedSamplerRange(SamplerType type); 73 bool usesPointSize() const; 74 bool usesPointSpriteEmulation() const; 75 bool usesGeometryShader() const; 76 77 GLint getUniformLocation(std::string name); 78 bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v); 79 bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v); 80 bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v); 81 bool setUniform4fv(GLint location, GLsizei count, const GLfloat *v); 82 bool setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value); 83 bool setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value); 84 bool setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value); 85 bool setUniform1iv(GLint location, GLsizei count, const GLint *v); 86 bool setUniform2iv(GLint location, GLsizei count, const GLint *v); 87 bool setUniform3iv(GLint location, GLsizei count, const GLint *v); 88 bool setUniform4iv(GLint location, GLsizei count, const GLint *v); 89 90 bool getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params); 91 bool getUniformiv(GLint location, GLsizei *bufSize, GLint *params); 92 93 void dirtyAllUniforms(); 94 void applyUniforms(); 95 96 bool load(InfoLog &infoLog, const void *binary, GLsizei length); 97 bool save(void* binary, GLsizei bufSize, GLsizei *length); 98 GLint getLength(); 99 100 bool link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader); 101 void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders); 102 103 void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const; 104 GLint getActiveAttributeCount() const; 105 GLint getActiveAttributeMaxLength() const; 106 107 void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const; 108 GLint getActiveUniformCount() const; 109 GLint getActiveUniformMaxLength() const; 110 111 void validate(InfoLog &infoLog); 112 bool validateSamplers(InfoLog *infoLog); 113 bool isValidated() const; 114 115 unsigned int getSerial() const; 116 117 void initAttributesByLayout(); 118 void sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const; 119 120 static std::string decorateAttribute(const std::string &name); // Prepend an underscore 121 122 private: 123 DISALLOW_COPY_AND_ASSIGN(ProgramBinary); 124 125 int packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader); 126 bool linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4], 127 std::string& pixelHLSL, std::string& vertexHLSL, 128 FragmentShader *fragmentShader, VertexShader *vertexShader); 129 130 bool linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader); 131 132 bool linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms); 133 bool defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog); 134 135 std::string generateGeometryShaderHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const; 136 std::string generatePointSpriteHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const; 137 138 rx::Renderer *const mRenderer; 139 140 rx::ShaderExecutable *mPixelExecutable; 141 rx::ShaderExecutable *mVertexExecutable; 142 rx::ShaderExecutable *mGeometryExecutable; 143 144 Attribute mLinkedAttribute[MAX_VERTEX_ATTRIBS]; 145 int mSemanticIndex[MAX_VERTEX_ATTRIBS]; 146 int mAttributesByLayout[MAX_VERTEX_ATTRIBS]; 147 148 struct Sampler 149 { 150 Sampler(); 151 152 bool active; 153 GLint logicalTextureUnit; 154 TextureType textureType; 155 }; 156 157 Sampler mSamplersPS[MAX_TEXTURE_IMAGE_UNITS]; 158 Sampler mSamplersVS[IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS]; 159 GLuint mUsedVertexSamplerRange; 160 GLuint mUsedPixelSamplerRange; 161 bool mUsesPointSize; 162 163 UniformArray mUniforms; 164 typedef std::vector<UniformLocation> UniformIndex; 165 UniformIndex mUniformIndex; 166 167 bool mValidated; 168 169 const unsigned int mSerial; 170 171 static unsigned int issueSerial(); 172 static unsigned int mCurrentSerial; 173 }; 174 } 175 176 #endif // LIBGLESV2_PROGRAM_BINARY_H_ 177