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::ShaderVariable> &getInputVaryings() const { return mInputVaryings; } getOutputVaryings()74 const std::vector<sh::ShaderVariable> &getOutputVaryings() const { return mOutputVaryings; } getUniforms()75 const std::vector<sh::ShaderVariable> &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::ShaderVariable> &getActiveAttributes() const { return mActiveAttributes; } getAllAttributes()82 const std::vector<sh::ShaderVariable> &getAllAttributes() const { return mAllAttributes; } getActiveOutputVariables()83 const std::vector<sh::ShaderVariable> &getActiveOutputVariables() const 84 { 85 return mActiveOutputVariables; 86 } 87 isEarlyFragmentTeststOptimization()88 bool isEarlyFragmentTeststOptimization() const { return mEarlyFragmentTestsOptimization; } 89 compilePending()90 bool compilePending() const { return mCompileStatus == CompileStatus::COMPILE_REQUESTED; } 91 92 private: 93 friend class Shader; 94 95 std::string mLabel; 96 97 ShaderType mShaderType; 98 int mShaderVersion; 99 std::string mTranslatedSource; 100 std::string mSource; 101 102 sh::WorkGroupSize mLocalSize; 103 104 std::vector<sh::ShaderVariable> mInputVaryings; 105 std::vector<sh::ShaderVariable> mOutputVaryings; 106 std::vector<sh::ShaderVariable> mUniforms; 107 std::vector<sh::InterfaceBlock> mUniformBlocks; 108 std::vector<sh::InterfaceBlock> mShaderStorageBlocks; 109 std::vector<sh::ShaderVariable> mAllAttributes; 110 std::vector<sh::ShaderVariable> mActiveAttributes; 111 std::vector<sh::ShaderVariable> mActiveOutputVariables; 112 113 bool mEarlyFragmentTestsOptimization; 114 115 // ANGLE_multiview. 116 int mNumViews; 117 118 // Geometry Shader. 119 Optional<PrimitiveMode> mGeometryShaderInputPrimitiveType; 120 Optional<PrimitiveMode> mGeometryShaderOutputPrimitiveType; 121 Optional<GLint> mGeometryShaderMaxVertices; 122 int mGeometryShaderInvocations; 123 124 // Indicates if this shader has been successfully compiled 125 CompileStatus mCompileStatus; 126 }; 127 128 class Shader final : angle::NonCopyable, public LabeledObject 129 { 130 public: 131 Shader(ShaderProgramManager *manager, 132 rx::GLImplFactory *implFactory, 133 const gl::Limitations &rendererLimitations, 134 ShaderType type, 135 ShaderProgramID handle); 136 137 void onDestroy(const Context *context); 138 139 void setLabel(const Context *context, const std::string &label) override; 140 const std::string &getLabel() const override; 141 getType()142 ShaderType getType() const { return mType; } 143 ShaderProgramID getHandle() const; 144 getImplementation()145 rx::ShaderImpl *getImplementation() const { return mImplementation.get(); } 146 147 void setSource(GLsizei count, const char *const *string, const GLint *length); 148 int getInfoLogLength(); 149 void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog); getInfoLogString()150 std::string getInfoLogString() const { return mInfoLog; } 151 int getSourceLength() const; getSourceString()152 const std::string &getSourceString() const { return mState.getSource(); } 153 void getSource(GLsizei bufSize, GLsizei *length, char *buffer) const; 154 int getTranslatedSourceLength(); 155 int getTranslatedSourceWithDebugInfoLength(); 156 const std::string &getTranslatedSource(); 157 void getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer); 158 void getTranslatedSourceWithDebugInfo(GLsizei bufSize, GLsizei *length, char *buffer); 159 160 void compile(const Context *context); 161 bool isCompiled(); 162 bool isCompleted(); 163 164 void addRef(); 165 void release(const Context *context); 166 unsigned int getRefCount() const; 167 bool isFlaggedForDeletion() const; 168 void flagForDeletion(); hasEarlyFragmentTestsOptimization()169 bool hasEarlyFragmentTestsOptimization() const 170 { 171 return mState.mEarlyFragmentTestsOptimization; 172 } 173 174 int getShaderVersion(); 175 176 const std::vector<sh::ShaderVariable> &getInputVaryings(); 177 const std::vector<sh::ShaderVariable> &getOutputVaryings(); 178 const std::vector<sh::ShaderVariable> &getUniforms(); 179 const std::vector<sh::InterfaceBlock> &getUniformBlocks(); 180 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks(); 181 const std::vector<sh::ShaderVariable> &getActiveAttributes(); 182 const std::vector<sh::ShaderVariable> &getAllAttributes(); 183 const std::vector<sh::ShaderVariable> &getActiveOutputVariables(); 184 185 // Returns mapped name of a transform feedback varying. The original name may contain array 186 // brackets with an index inside, which will get copied to the mapped name. The varying must be 187 // known to be declared in the shader. 188 std::string getTransformFeedbackVaryingMappedName(const std::string &tfVaryingName); 189 190 const sh::WorkGroupSize &getWorkGroupSize(); 191 192 int getNumViews(); 193 194 Optional<PrimitiveMode> getGeometryShaderInputPrimitiveType(); 195 Optional<PrimitiveMode> getGeometryShaderOutputPrimitiveType(); 196 int getGeometryShaderInvocations(); 197 Optional<GLint> getGeometryShaderMaxVertices(); 198 199 const std::string &getCompilerResourcesString() const; 200 201 private: 202 struct CompilingState; 203 204 ~Shader() override; 205 static void GetSourceImpl(const std::string &source, 206 GLsizei bufSize, 207 GLsizei *length, 208 char *buffer); 209 210 void resolveCompile(); 211 212 ShaderState mState; 213 std::unique_ptr<rx::ShaderImpl> mImplementation; 214 const gl::Limitations &mRendererLimitations; 215 const ShaderProgramID mHandle; 216 const ShaderType mType; 217 unsigned int mRefCount; // Number of program objects this shader is attached to 218 bool mDeleteStatus; // Flag to indicate that the shader can be deleted when no longer in use 219 std::string mInfoLog; 220 221 // We keep a reference to the translator in order to defer compiles while preserving settings. 222 BindingPointer<Compiler> mBoundCompiler; 223 std::unique_ptr<CompilingState> mCompilingState; 224 std::string mCompilerResourcesString; 225 226 ShaderProgramManager *mResourceManager; 227 228 GLuint mCurrentMaxComputeWorkGroupInvocations; 229 unsigned int mMaxComputeSharedMemory; 230 }; 231 232 bool CompareShaderVar(const sh::ShaderVariable &x, const sh::ShaderVariable &y); 233 234 const char *GetShaderTypeString(ShaderType type); 235 } // namespace gl 236 237 #endif // LIBANGLE_SHADER_H_ 238