1 // 2 // Copyright (c) 2002-2014 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 #include <GLES3/gl3.h> 14 #include <GLES3/gl3ext.h> 15 #include <GLES2/gl2.h> 16 #include <GLES2/gl2ext.h> 17 18 #include <string> 19 #include <vector> 20 21 #include "common/RefCountObject.h" 22 #include "angletypes.h" 23 #include "common/mathutil.h" 24 #include "libGLESv2/Uniform.h" 25 #include "libGLESv2/Shader.h" 26 #include "libGLESv2/Constants.h" 27 #include "libGLESv2/renderer/VertexDataManager.h" 28 29 namespace rx 30 { 31 class ShaderExecutable; 32 class Renderer; 33 struct TranslatedAttribute; 34 class UniformStorage; 35 class DynamicHLSL; 36 } 37 38 namespace gl 39 { 40 class FragmentShader; 41 class VertexShader; 42 class InfoLog; 43 class AttributeBindings; 44 class Buffer; 45 46 // Struct used for correlating uniforms/elements of uniform arrays to handles 47 struct VariableLocation 48 { VariableLocationVariableLocation49 VariableLocation() 50 { 51 } 52 53 VariableLocation(const std::string &name, unsigned int element, unsigned int index); 54 55 std::string name; 56 unsigned int element; 57 unsigned int index; 58 }; 59 60 struct LinkedVarying 61 { 62 LinkedVarying(); 63 LinkedVarying(const std::string &name, GLenum type, GLsizei size, const std::string &semanticName, 64 unsigned int semanticIndex, unsigned int semanticIndexCount); 65 66 // Original GL name 67 std::string name; 68 69 GLenum type; 70 GLsizei size; 71 72 // DirectX semantic information 73 std::string semanticName; 74 unsigned int semanticIndex; 75 unsigned int semanticIndexCount; 76 }; 77 78 // This is the result of linking a program. It is the state that would be passed to ProgramBinary. 79 class ProgramBinary : public RefCountObject 80 { 81 public: 82 explicit ProgramBinary(rx::Renderer *renderer); 83 ~ProgramBinary(); 84 85 rx::ShaderExecutable *getPixelExecutable() const; 86 rx::ShaderExecutable *getVertexExecutableForInputLayout(const VertexFormat inputLayout[MAX_VERTEX_ATTRIBS]); 87 rx::ShaderExecutable *getGeometryExecutable() const; 88 89 GLuint getAttributeLocation(const char *name); 90 int getSemanticIndex(int attributeIndex); 91 92 GLint getSamplerMapping(SamplerType type, unsigned int samplerIndex); 93 TextureType getSamplerTextureType(SamplerType type, unsigned int samplerIndex); 94 GLint getUsedSamplerRange(SamplerType type); 95 bool usesPointSize() const; 96 bool usesPointSpriteEmulation() const; 97 bool usesGeometryShader() const; 98 99 GLint getUniformLocation(std::string name); 100 GLuint getUniformIndex(std::string name); 101 GLuint getUniformBlockIndex(std::string name); 102 void setUniform1fv(GLint location, GLsizei count, const GLfloat *v); 103 void setUniform2fv(GLint location, GLsizei count, const GLfloat *v); 104 void setUniform3fv(GLint location, GLsizei count, const GLfloat *v); 105 void setUniform4fv(GLint location, GLsizei count, const GLfloat *v); 106 void setUniform1iv(GLint location, GLsizei count, const GLint *v); 107 void setUniform2iv(GLint location, GLsizei count, const GLint *v); 108 void setUniform3iv(GLint location, GLsizei count, const GLint *v); 109 void setUniform4iv(GLint location, GLsizei count, const GLint *v); 110 void setUniform1uiv(GLint location, GLsizei count, const GLuint *v); 111 void setUniform2uiv(GLint location, GLsizei count, const GLuint *v); 112 void setUniform3uiv(GLint location, GLsizei count, const GLuint *v); 113 void setUniform4uiv(GLint location, GLsizei count, const GLuint *v); 114 void setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); 115 void setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); 116 void setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); 117 void setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); 118 void setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); 119 void setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); 120 void setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); 121 void setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); 122 void setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); 123 124 bool getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params); 125 bool getUniformiv(GLint location, GLsizei *bufSize, GLint *params); 126 bool getUniformuiv(GLint location, GLsizei *bufSize, GLuint *params); 127 128 void dirtyAllUniforms(); 129 void applyUniforms(); 130 bool applyUniformBuffers(const std::vector<Buffer*> boundBuffers); 131 132 bool load(InfoLog &infoLog, const void *binary, GLsizei length); 133 bool save(void* binary, GLsizei bufSize, GLsizei *length); 134 GLint getLength(); 135 136 bool link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader, 137 const std::vector<std::string>& transformFeedbackVaryings, GLenum transformFeedbackBufferMode); 138 void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders); 139 140 void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const; 141 GLint getActiveAttributeCount() const; 142 GLint getActiveAttributeMaxLength() const; 143 144 void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const; 145 GLint getActiveUniformCount() const; 146 GLint getActiveUniformMaxLength() const; 147 GLint getActiveUniformi(GLuint index, GLenum pname) const; 148 bool isValidUniformLocation(GLint location) const; 149 LinkedUniform *getUniformByLocation(GLint location) const; 150 151 void getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const; 152 void getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const; 153 GLuint getActiveUniformBlockCount() const; 154 GLuint getActiveUniformBlockMaxLength() const; 155 UniformBlock *getUniformBlockByIndex(GLuint blockIndex); 156 157 GLint getFragDataLocation(const char *name) const; 158 159 size_t getTransformFeedbackVaryingCount() const; 160 const LinkedVarying &getTransformFeedbackVarying(size_t idx) const; 161 GLenum getTransformFeedbackBufferMode() const; 162 163 void validate(InfoLog &infoLog); 164 bool validateSamplers(InfoLog *infoLog); 165 bool isValidated() const; 166 167 unsigned int getSerial() const; 168 int getShaderVersion() const; 169 170 void initAttributesByLayout(); 171 void sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const; 172 getUniforms()173 const std::vector<LinkedUniform*> &getUniforms() const { return mUniforms; } getVertexUniformStorage()174 const rx::UniformStorage &getVertexUniformStorage() const { return *mVertexUniformStorage; } getFragmentUniformStorage()175 const rx::UniformStorage &getFragmentUniformStorage() const { return *mFragmentUniformStorage; } 176 177 private: 178 DISALLOW_COPY_AND_ASSIGN(ProgramBinary); 179 180 bool linkVaryings(InfoLog &infoLog, FragmentShader *fragmentShader, VertexShader *vertexShader); 181 bool linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader); 182 183 typedef std::vector<BlockMemberInfo>::const_iterator BlockInfoItr; 184 185 template <class ShaderVarType> 186 bool linkValidateFields(InfoLog &infoLog, const std::string &varName, const ShaderVarType &vertexVar, const ShaderVarType &fragmentVar); 187 bool linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const ShaderVariable &vertexVariable, const ShaderVariable &fragmentVariable, bool validatePrecision); 188 189 bool linkValidateVariables(InfoLog &infoLog, const std::string &uniformName, const Uniform &vertexUniform, const Uniform &fragmentUniform); 190 bool linkValidateVariables(InfoLog &infoLog, const std::string &varyingName, const Varying &vertexVarying, const Varying &fragmentVarying); 191 bool linkValidateVariables(InfoLog &infoLog, const std::string &uniformName, const InterfaceBlockField &vertexUniform, const InterfaceBlockField &fragmentUniform); 192 bool linkUniforms(InfoLog &infoLog, const std::vector<Uniform> &vertexUniforms, const std::vector<Uniform> &fragmentUniforms); 193 bool defineUniform(GLenum shader, const Uniform &constant, InfoLog &infoLog); 194 bool areMatchingInterfaceBlocks(InfoLog &infoLog, const InterfaceBlock &vertexInterfaceBlock, const InterfaceBlock &fragmentInterfaceBlock); 195 bool linkUniformBlocks(InfoLog &infoLog, const std::vector<InterfaceBlock> &vertexUniformBlocks, const std::vector<InterfaceBlock> &fragmentUniformBlocks); 196 bool gatherTransformFeedbackLinkedVaryings(InfoLog &infoLog, const std::vector<LinkedVarying> &linkedVaryings, 197 const std::vector<std::string> &transformFeedbackVaryingNames, 198 GLenum transformFeedbackBufferMode, 199 std::vector<LinkedVarying> *outTransformFeedbackLinkedVaryings) const; 200 void defineUniformBlockMembers(const std::vector<InterfaceBlockField> &fields, const std::string &prefix, int blockIndex, BlockInfoItr *blockInfoItr, std::vector<unsigned int> *blockUniformIndexes); 201 bool defineUniformBlock(InfoLog &infoLog, GLenum shader, const InterfaceBlock &interfaceBlock); 202 bool assignUniformBlockRegister(InfoLog &infoLog, UniformBlock *uniformBlock, GLenum shader, unsigned int registerIndex); 203 void defineOutputVariables(FragmentShader *fragmentShader); 204 void initializeUniformStorage(); 205 206 template <typename T> 207 void setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType); 208 209 template <int cols, int rows> 210 void setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType); 211 212 template <typename T> 213 bool getUniformv(GLint location, GLsizei *bufSize, T *params, GLenum uniformType); 214 215 static TextureType getTextureType(GLenum samplerType, InfoLog &infoLog); 216 217 class VertexExecutable 218 { 219 public: 220 VertexExecutable(rx::Renderer *const renderer, 221 const VertexFormat inputLayout[MAX_VERTEX_ATTRIBS], 222 const GLenum signature[MAX_VERTEX_ATTRIBS], 223 rx::ShaderExecutable *shaderExecutable); 224 ~VertexExecutable(); 225 226 bool matchesSignature(const GLenum convertedLayout[MAX_VERTEX_ATTRIBS]) const; 227 inputs()228 const VertexFormat *inputs() const { return mInputs; } signature()229 const GLenum *signature() const { return mSignature; } shaderExecutable()230 rx::ShaderExecutable *shaderExecutable() const { return mShaderExecutable; } 231 232 private: 233 VertexFormat mInputs[MAX_VERTEX_ATTRIBS]; 234 GLenum mSignature[MAX_VERTEX_ATTRIBS]; 235 rx::ShaderExecutable *mShaderExecutable; 236 }; 237 238 rx::Renderer *const mRenderer; 239 DynamicHLSL *mDynamicHLSL; 240 241 std::string mVertexHLSL; 242 rx::D3DWorkaroundType mVertexWorkarounds; 243 std::vector<VertexExecutable *> mVertexExecutables; 244 rx::ShaderExecutable *mGeometryExecutable; 245 rx::ShaderExecutable *mPixelExecutable; 246 247 Attribute mLinkedAttribute[MAX_VERTEX_ATTRIBS]; 248 Attribute mShaderAttributes[MAX_VERTEX_ATTRIBS]; 249 int mSemanticIndex[MAX_VERTEX_ATTRIBS]; 250 int mAttributesByLayout[MAX_VERTEX_ATTRIBS]; 251 252 GLenum mTransformFeedbackBufferMode; 253 std::vector<LinkedVarying> mTransformFeedbackLinkedVaryings; 254 255 struct Sampler 256 { 257 Sampler(); 258 259 bool active; 260 GLint logicalTextureUnit; 261 TextureType textureType; 262 }; 263 264 Sampler mSamplersPS[MAX_TEXTURE_IMAGE_UNITS]; 265 Sampler mSamplersVS[IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS]; 266 GLuint mUsedVertexSamplerRange; 267 GLuint mUsedPixelSamplerRange; 268 bool mUsesPointSize; 269 int mShaderVersion; 270 271 std::vector<LinkedUniform*> mUniforms; 272 std::vector<UniformBlock*> mUniformBlocks; 273 std::vector<VariableLocation> mUniformIndex; 274 std::map<int, VariableLocation> mOutputVariables; 275 rx::UniformStorage *mVertexUniformStorage; 276 rx::UniformStorage *mFragmentUniformStorage; 277 278 bool mValidated; 279 280 const unsigned int mSerial; 281 282 static unsigned int issueSerial(); 283 static unsigned int mCurrentSerial; 284 }; 285 286 } 287 288 #endif // LIBGLESV2_PROGRAM_BINARY_H_ 289