1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef PROGRAM_DATA_H 17 #define PROGRAM_DATA_H 18 19 #include "ShaderParser.h" 20 21 #include "base/HybridComponentManager.h" 22 23 #include <memory> 24 #include <sstream> 25 #include <string> 26 #include <unordered_map> 27 28 struct GLUniformDesc { 29 GLUniformDesc() = default; 30 GLUniformDesc(const char* name, GLint location, GLsizei count, GLboolean transpose, 31 GLenum type, GLsizei size, unsigned char* val); 32 GLUniformDesc(android::base::Stream* stream); 33 GLUniformDesc(GLUniformDesc&&) = default; 34 GLUniformDesc& operator=(GLUniformDesc&&) = default; 35 36 GLsizei mCount = 0; 37 GLboolean mTranspose = GL_FALSE; 38 GLenum mType = (GLenum)0; 39 std::vector<unsigned char> mVal; 40 41 std::string mGuestName = {}; 42 43 void onSave(android::base::Stream* stream) const; 44 }; 45 46 struct AttachedShader { 47 GLuint localName = 0; 48 ShaderParser* shader = nullptr; 49 // linkedSource is only updated when glLinkProgram 50 // This is the "real" source the hardware is using for the compiled program. 51 std::string linkedSource = {}; 52 ANGLEShaderParser::ShaderLinkInfo linkInfo = {}; 53 }; 54 55 class ProgramData : public ObjectData { 56 public: 57 enum ShaderType { 58 VERTEX = 0, 59 FRAGMENT, 60 COMPUTE, 61 NUM_SHADER_TYPE 62 }; 63 ProgramData(int glesMaj, int glesMin); 64 ProgramData(android::base::Stream* stream); 65 virtual void onSave(android::base::Stream* stream, unsigned int globalName) const override; 66 virtual void postLoad(const getObjDataPtr_t& getObjDataPtr) override; 67 // restore() in ProgramData must be executed after shaders 68 virtual void restore(ObjectLocalName localName, 69 const getGlobalName_t& getGlobalName) override; 70 71 GLuint getAttachedVertexShader() const; 72 GLuint getAttachedFragmentShader() const; 73 GLuint getAttachedComputeShader() const; 74 GLuint getAttachedShader(GLenum type) const; 75 76 std::string getTranslatedName(const std::string& userVarName) const; 77 std::string getDetranslatedName(const std::string& driverName) const; 78 79 bool attachShader(GLuint shader, ShaderParser* shaderData, GLenum type); 80 bool isAttached(GLuint shader) const; 81 bool detachShader(GLuint shader); 82 void bindAttribLocation(const std::string& var, GLuint loc); 83 void linkedAttribLocation(const std::string& var, GLuint loc); 84 85 void appendValidationErrMsg(std::ostringstream& ss); 86 bool validateLink(ShaderParser* frag, ShaderParser* vert); 87 getValidateStatus()88 bool getValidateStatus() const { return ValidateStatus; } setValidateStatus(bool status)89 void setValidateStatus(bool status) { ValidateStatus = status; } 90 91 // setLinkStatus resets uniform location virtualization as well 92 void setHostLinkStatus(GLint status); 93 void setLinkStatus(GLint status); 94 bool getLinkStatus() const; 95 96 void setErrInfoLog(); 97 98 // make sure this is never called with a 0-length log returned from 99 // glGetProgramLog; it can be garbage without a null terminator. 100 void setInfoLog(const GLchar *log); 101 const GLchar* getInfoLog() const; 102 isInUse()103 bool isInUse() const { return IsInUse; } setInUse(bool inUse)104 void setInUse(bool inUse) { IsInUse = inUse; } 105 getDeleteStatus()106 bool getDeleteStatus() const { return DeleteStatus; } setDeleteStatus(bool status)107 void setDeleteStatus(bool status) { DeleteStatus = status; } 108 109 // boundAttribLocs stores the attribute locations assigned by 110 // glBindAttribLocation. 111 // It will take effect after glLinkProgram. 112 std::unordered_map<std::string, GLuint> boundAttribLocs; 113 virtual GenNameInfo getGenNameInfo() const override; addProgramName(GLuint name)114 void addProgramName(GLuint name) { ProgramName = name; } getProgramName()115 GLuint getProgramName() const { return ProgramName; } 116 117 // Virtualize uniform locations 118 // It handles location -1 as well 119 void initGuestUniformLocForKey(const std::string& key); 120 void initGuestUniformLocForKey(const std::string& key, 121 const std::string& key2); 122 int getGuestUniformLocation(const char* uniName); 123 int getHostUniformLocation(int guestLocation); 124 125 private: 126 // linkedAttribLocs stores the attribute locations the guest might 127 // know about. It includes all boundAttribLocs before the previous 128 // glLinkProgram and all attribute locations retrieved by glGetAttribLocation 129 std::unordered_map<std::string, GLuint> linkedAttribLocs; 130 std::unordered_map<GLuint, GLUniformDesc> uniforms; 131 AttachedShader attachedShaders[NUM_SHADER_TYPE] = {}; 132 std::string validationInfoLog; 133 std::string infoLog; 134 bool ValidateStatus; 135 bool LinkStatus; 136 bool HostLinkStatus; 137 bool IsInUse; 138 bool DeleteStatus; 139 GLuint ProgramName; 140 std::unordered_map<GLuint, GLuint> mUniformBlockBinding; 141 std::vector<std::string> mTransformFeedbacks; 142 GLenum mTransformFeedbackBufferMode = 0; 143 144 int mGlesMajorVersion = 2; 145 int mGlesMinorVersion = 0; 146 std::unordered_map<GLuint, GLUniformDesc> collectUniformInfo() const; 147 void getUniformValue(const GLchar *name, GLenum type, 148 std::unordered_map<GLuint, GLUniformDesc> &uniformsOnSave) const; 149 150 std::unordered_map<std::string, int> mUniNameToGuestLoc; 151 android::base::HybridComponentManager<10000, int, int> mGuestLocToHostLoc; 152 153 int mCurrUniformBaseLoc = 0; 154 bool mUseUniformLocationVirtualization = true; 155 bool mUseDirectDriverUniformInfo = false; 156 }; 157 #endif 158