1 // 2 // Copyright 2002 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 // Shader.h: Defines the abstract gl::Shader class and its concrete derived 8 // classes VertexShader and FragmentShader. Implements GL shader objects and 9 // related functionality. [OpenGL ES 2.0.24] section 2.10 page 24 and section 10 // 3.8 page 84. 11 12 #ifndef LIBANGLE_SHADER_H_ 13 #define LIBANGLE_SHADER_H_ 14 15 #include <list> 16 #include <memory> 17 #include <string> 18 #include <vector> 19 20 #include <GLSLANG/ShaderLang.h> 21 #include "angle_gl.h" 22 23 #include "common/Optional.h" 24 #include "common/angleutils.h" 25 #include "libANGLE/Compiler.h" 26 #include "libANGLE/Debug.h" 27 #include "libANGLE/angletypes.h" 28 29 namespace rx 30 { 31 class GLImplFactory; 32 class ShaderImpl; 33 class ShaderSh; 34 class WaitableCompileEvent; 35 } // namespace rx 36 37 namespace angle 38 { 39 class WaitableEvent; 40 class WorkerThreadPool; 41 } // namespace angle 42 43 namespace gl 44 { 45 class CompileTask; 46 class Context; 47 struct Limitations; 48 class ShaderProgramManager; 49 class State; 50 51 // We defer the compile until link time, or until properties are queried. 52 enum class CompileStatus 53 { 54 NOT_COMPILED, 55 COMPILE_REQUESTED, 56 COMPILED, 57 }; 58 59 class ShaderState final : angle::NonCopyable 60 { 61 public: 62 ShaderState(ShaderType shaderType); 63 ~ShaderState(); 64 getLabel()65 const std::string &getLabel() const { return mLabel; } 66 getSource()67 const std::string &getSource() const { return mSource; } getTranslatedSource()68 const std::string &getTranslatedSource() const { return mTranslatedSource; } 69 getShaderType()70 ShaderType getShaderType() const { return mShaderType; } getShaderVersion()71 int getShaderVersion() const { return mShaderVersion; } 72 getInputVaryings()73 const std::vector<sh::Varying> &getInputVaryings() const { return mInputVaryings; } getOutputVaryings()74 const std::vector<sh::Varying> &getOutputVaryings() const { return mOutputVaryings; } getUniforms()75 const std::vector<sh::Uniform> &getUniforms() const { return mUniforms; } getUniformBlocks()76 const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return mUniformBlocks; } getShaderStorageBlocks()77 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const 78 { 79 return mShaderStorageBlocks; 80 } getActiveAttributes()81 const std::vector<sh::Attribute> &getActiveAttributes() const { return mActiveAttributes; } getAllAttributes()82 const std::vector<sh::Attribute> &getAllAttributes() const { return mAllAttributes; } getActiveOutputVariables()83 const std::vector<sh::OutputVariable> &getActiveOutputVariables() const 84 { 85 return mActiveOutputVariables; 86 } 87 compilePending()88 bool compilePending() const { return mCompileStatus == CompileStatus::COMPILE_REQUESTED; } 89 90 private: 91 friend class Shader; 92 93 std::string mLabel; 94 95 ShaderType mShaderType; 96 int mShaderVersion; 97 std::string mTranslatedSource; 98 std::string mSource; 99 100 sh::WorkGroupSize mLocalSize; 101 102 std::vector<sh::Varying> mInputVaryings; 103 std::vector<sh::Varying> mOutputVaryings; 104 std::vector<sh::Uniform> mUniforms; 105 std::vector<sh::InterfaceBlock> mUniformBlocks; 106 std::vector<sh::InterfaceBlock> mShaderStorageBlocks; 107 std::vector<sh::Attribute> mAllAttributes; 108 std::vector<sh::Attribute> mActiveAttributes; 109 std::vector<sh::OutputVariable> mActiveOutputVariables; 110 111 // ANGLE_multiview. 112 int mNumViews; 113 114 // Geometry Shader. 115 Optional<PrimitiveMode> mGeometryShaderInputPrimitiveType; 116 Optional<PrimitiveMode> mGeometryShaderOutputPrimitiveType; 117 Optional<GLint> mGeometryShaderMaxVertices; 118 int mGeometryShaderInvocations; 119 120 // Indicates if this shader has been successfully compiled 121 CompileStatus mCompileStatus; 122 }; 123 124 class Shader final : angle::NonCopyable, public LabeledObject 125 { 126 public: 127 Shader(ShaderProgramManager *manager, 128 rx::GLImplFactory *implFactory, 129 const gl::Limitations &rendererLimitations, 130 ShaderType type, 131 GLuint handle); 132 133 void onDestroy(const Context *context); 134 135 void setLabel(const Context *context, const std::string &label) override; 136 const std::string &getLabel() const override; 137 getType()138 ShaderType getType() const { return mType; } 139 GLuint getHandle() const; 140 getImplementation()141 rx::ShaderImpl *getImplementation() const { return mImplementation.get(); } 142 143 void setSource(GLsizei count, const char *const *string, const GLint *length); 144 int getInfoLogLength(); 145 void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog); 146 int getSourceLength() const; getSourceString()147 const std::string &getSourceString() const { return mState.getSource(); } 148 void getSource(GLsizei bufSize, GLsizei *length, char *buffer) const; 149 int getTranslatedSourceLength(); 150 int getTranslatedSourceWithDebugInfoLength(); 151 const std::string &getTranslatedSource(); 152 void getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer); 153 void getTranslatedSourceWithDebugInfo(GLsizei bufSize, GLsizei *length, char *buffer); 154 155 void compile(const Context *context); 156 bool isCompiled(); 157 bool isCompleted(); 158 159 void addRef(); 160 void release(const Context *context); 161 unsigned int getRefCount() const; 162 bool isFlaggedForDeletion() const; 163 void flagForDeletion(); 164 165 int getShaderVersion(); 166 167 const std::vector<sh::Varying> &getInputVaryings(); 168 const std::vector<sh::Varying> &getOutputVaryings(); 169 const std::vector<sh::Uniform> &getUniforms(); 170 const std::vector<sh::InterfaceBlock> &getUniformBlocks(); 171 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks(); 172 const std::vector<sh::Attribute> &getActiveAttributes(); 173 const std::vector<sh::Attribute> &getAllAttributes(); 174 const std::vector<sh::OutputVariable> &getActiveOutputVariables(); 175 176 // Returns mapped name of a transform feedback varying. The original name may contain array 177 // brackets with an index inside, which will get copied to the mapped name. The varying must be 178 // known to be declared in the shader. 179 std::string getTransformFeedbackVaryingMappedName(const std::string &tfVaryingName); 180 181 const sh::WorkGroupSize &getWorkGroupSize(); 182 183 int getNumViews(); 184 185 Optional<PrimitiveMode> getGeometryShaderInputPrimitiveType(); 186 Optional<PrimitiveMode> getGeometryShaderOutputPrimitiveType(); 187 int getGeometryShaderInvocations(); 188 Optional<GLint> getGeometryShaderMaxVertices(); 189 190 const std::string &getCompilerResourcesString() const; 191 192 private: 193 struct CompilingState; 194 195 ~Shader() override; 196 static void GetSourceImpl(const std::string &source, 197 GLsizei bufSize, 198 GLsizei *length, 199 char *buffer); 200 201 void resolveCompile(); 202 203 ShaderState mState; 204 std::unique_ptr<rx::ShaderImpl> mImplementation; 205 const gl::Limitations &mRendererLimitations; 206 const GLuint mHandle; 207 const ShaderType mType; 208 unsigned int mRefCount; // Number of program objects this shader is attached to 209 bool mDeleteStatus; // Flag to indicate that the shader can be deleted when no longer in use 210 std::string mInfoLog; 211 212 // We keep a reference to the translator in order to defer compiles while preserving settings. 213 BindingPointer<Compiler> mBoundCompiler; 214 std::unique_ptr<CompilingState> mCompilingState; 215 std::string mCompilerResourcesString; 216 217 ShaderProgramManager *mResourceManager; 218 219 GLuint mCurrentMaxComputeWorkGroupInvocations; 220 }; 221 222 bool CompareShaderVar(const sh::ShaderVariable &x, const sh::ShaderVariable &y); 223 224 const char *GetShaderTypeString(ShaderType type); 225 } // namespace gl 226 227 #endif // LIBANGLE_SHADER_H_ 228