Shader.h (11484B)
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/MemoryBuffer.h" 24 #include "common/Optional.h" 25 #include "common/angleutils.h" 26 #include "libANGLE/BinaryStream.h" 27 #include "libANGLE/Caps.h" 28 #include "libANGLE/Compiler.h" 29 #include "libANGLE/Debug.h" 30 #include "libANGLE/angletypes.h" 31 32 namespace rx 33 { 34 class GLImplFactory; 35 class ShaderImpl; 36 class ShaderSh; 37 class WaitableCompileEvent; 38 } // namespace rx 39 40 namespace angle 41 { 42 class WaitableEvent; 43 class WorkerThreadPool; 44 } // namespace angle 45 46 namespace gl 47 { 48 class CompileTask; 49 class Context; 50 class ShaderProgramManager; 51 class State; 52 class BinaryInputStream; 53 class BinaryOutputStream; 54 55 // We defer the compile until link time, or until properties are queried. 56 enum class CompileStatus 57 { 58 NOT_COMPILED, 59 COMPILE_REQUESTED, 60 COMPILED, 61 }; 62 63 class ShaderState final : angle::NonCopyable 64 { 65 public: 66 ShaderState(ShaderType shaderType); 67 ~ShaderState(); 68 69 const std::string &getLabel() const { return mLabel; } 70 71 const std::string &getSource() const { return mSource; } 72 bool isCompiledToBinary() const { return !mCompiledBinary.empty(); } 73 const std::string &getTranslatedSource() const { return mTranslatedSource; } 74 const sh::BinaryBlob &getCompiledBinary() const { return mCompiledBinary; } 75 76 ShaderType getShaderType() const { return mShaderType; } 77 int getShaderVersion() const { return mShaderVersion; } 78 79 const std::vector<sh::ShaderVariable> &getInputVaryings() const { return mInputVaryings; } 80 const std::vector<sh::ShaderVariable> &getOutputVaryings() const { return mOutputVaryings; } 81 const std::vector<sh::ShaderVariable> &getUniforms() const { return mUniforms; } 82 const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return mUniformBlocks; } 83 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const 84 { 85 return mShaderStorageBlocks; 86 } 87 const std::vector<sh::ShaderVariable> &getActiveAttributes() const { return mActiveAttributes; } 88 const std::vector<sh::ShaderVariable> &getAllAttributes() const { return mAllAttributes; } 89 const std::vector<sh::ShaderVariable> &getActiveOutputVariables() const 90 { 91 return mActiveOutputVariables; 92 } 93 94 bool compilePending() const { return mCompileStatus == CompileStatus::COMPILE_REQUESTED; } 95 96 const sh::WorkGroupSize &getLocalSize() const { return mLocalSize; } 97 98 bool hasDiscard() const { return mHasDiscard; } 99 bool enablesPerSampleShading() const { return mEnablesPerSampleShading; } 100 rx::SpecConstUsageBits getSpecConstUsageBits() const { return mSpecConstUsageBits; } 101 102 int getNumViews() const { return mNumViews; } 103 104 Optional<PrimitiveMode> getGeometryShaderInputPrimitiveType() const 105 { 106 return mGeometryShaderInputPrimitiveType; 107 } 108 109 Optional<PrimitiveMode> getGeometryShaderOutputPrimitiveType() const 110 { 111 return mGeometryShaderOutputPrimitiveType; 112 } 113 114 Optional<GLint> geoGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; } 115 116 Optional<GLint> getGeometryShaderInvocations() const { return mGeometryShaderInvocations; } 117 118 CompileStatus getCompileStatus() const { return mCompileStatus; } 119 120 private: 121 friend class Shader; 122 123 std::string mLabel; 124 125 ShaderType mShaderType; 126 int mShaderVersion; 127 std::string mTranslatedSource; 128 sh::BinaryBlob mCompiledBinary; 129 std::string mSource; 130 131 sh::WorkGroupSize mLocalSize; 132 133 std::vector<sh::ShaderVariable> mInputVaryings; 134 std::vector<sh::ShaderVariable> mOutputVaryings; 135 std::vector<sh::ShaderVariable> mUniforms; 136 std::vector<sh::InterfaceBlock> mUniformBlocks; 137 std::vector<sh::InterfaceBlock> mShaderStorageBlocks; 138 std::vector<sh::ShaderVariable> mAllAttributes; 139 std::vector<sh::ShaderVariable> mActiveAttributes; 140 std::vector<sh::ShaderVariable> mActiveOutputVariables; 141 142 bool mHasDiscard; 143 bool mEnablesPerSampleShading; 144 BlendEquationBitSet mAdvancedBlendEquations; 145 rx::SpecConstUsageBits mSpecConstUsageBits; 146 147 // ANGLE_multiview. 148 int mNumViews; 149 150 // Geometry Shader. 151 Optional<PrimitiveMode> mGeometryShaderInputPrimitiveType; 152 Optional<PrimitiveMode> mGeometryShaderOutputPrimitiveType; 153 Optional<GLint> mGeometryShaderMaxVertices; 154 int mGeometryShaderInvocations; 155 156 // Tessellation Shader 157 int mTessControlShaderVertices; 158 GLenum mTessGenMode; 159 GLenum mTessGenSpacing; 160 GLenum mTessGenVertexOrder; 161 GLenum mTessGenPointMode; 162 163 // Indicates if this shader has been successfully compiled 164 CompileStatus mCompileStatus; 165 }; 166 167 class Shader final : angle::NonCopyable, public LabeledObject 168 { 169 public: 170 Shader(ShaderProgramManager *manager, 171 rx::GLImplFactory *implFactory, 172 const gl::Limitations &rendererLimitations, 173 ShaderType type, 174 ShaderProgramID handle); 175 176 void onDestroy(const Context *context); 177 178 angle::Result setLabel(const Context *context, const std::string &label) override; 179 const std::string &getLabel() const override; 180 181 ShaderType getType() const { return mType; } 182 ShaderProgramID getHandle() const; 183 184 rx::ShaderImpl *getImplementation() const { return mImplementation.get(); } 185 186 void setSource(GLsizei count, const char *const *string, const GLint *length); 187 int getInfoLogLength(const Context *context); 188 void getInfoLog(const Context *context, GLsizei bufSize, GLsizei *length, char *infoLog); 189 std::string getInfoLogString() const { return mInfoLog; } 190 int getSourceLength() const; 191 const std::string &getSourceString() const { return mState.getSource(); } 192 void getSource(GLsizei bufSize, GLsizei *length, char *buffer) const; 193 int getTranslatedSourceLength(const Context *context); 194 int getTranslatedSourceWithDebugInfoLength(const Context *context); 195 const std::string &getTranslatedSource(const Context *context); 196 void getTranslatedSource(const Context *context, 197 GLsizei bufSize, 198 GLsizei *length, 199 char *buffer); 200 void getTranslatedSourceWithDebugInfo(const Context *context, 201 GLsizei bufSize, 202 GLsizei *length, 203 char *buffer); 204 const sh::BinaryBlob &getCompiledBinary(const Context *context); 205 206 void compile(const Context *context); 207 bool isCompiled(const Context *context); 208 bool isCompleted(); 209 210 void addRef(); 211 void release(const Context *context); 212 unsigned int getRefCount() const; 213 bool isFlaggedForDeletion() const; 214 void flagForDeletion(); 215 bool hasDiscard() const { return mState.mHasDiscard; } 216 bool enablesPerSampleShading() const { return mState.mEnablesPerSampleShading; } 217 BlendEquationBitSet getAdvancedBlendEquations() const { return mState.mAdvancedBlendEquations; } 218 rx::SpecConstUsageBits getSpecConstUsageBits() const { return mState.mSpecConstUsageBits; } 219 220 int getShaderVersion(const Context *context); 221 222 const std::vector<sh::ShaderVariable> &getInputVaryings(const Context *context); 223 const std::vector<sh::ShaderVariable> &getOutputVaryings(const Context *context); 224 const std::vector<sh::ShaderVariable> &getUniforms(const Context *context); 225 const std::vector<sh::InterfaceBlock> &getUniformBlocks(const Context *context); 226 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks(const Context *context); 227 const std::vector<sh::ShaderVariable> &getActiveAttributes(const Context *context); 228 const std::vector<sh::ShaderVariable> &getAllAttributes(const Context *context); 229 const std::vector<sh::ShaderVariable> &getActiveOutputVariables(const Context *context); 230 231 // Returns mapped name of a transform feedback varying. The original name may contain array 232 // brackets with an index inside, which will get copied to the mapped name. The varying must be 233 // known to be declared in the shader. 234 std::string getTransformFeedbackVaryingMappedName(const Context *context, 235 const std::string &tfVaryingName); 236 237 const sh::WorkGroupSize &getWorkGroupSize(const Context *context); 238 239 int getNumViews(const Context *context); 240 241 Optional<PrimitiveMode> getGeometryShaderInputPrimitiveType(const Context *context); 242 Optional<PrimitiveMode> getGeometryShaderOutputPrimitiveType(const Context *context); 243 int getGeometryShaderInvocations(const Context *context); 244 Optional<GLint> getGeometryShaderMaxVertices(const Context *context); 245 int getTessControlShaderVertices(const Context *context); 246 GLenum getTessGenMode(const Context *context); 247 GLenum getTessGenSpacing(const Context *context); 248 GLenum getTessGenVertexOrder(const Context *context); 249 GLenum getTessGenPointMode(const Context *context); 250 251 const std::string &getCompilerResourcesString() const; 252 253 const ShaderState &getState() const { return mState; } 254 255 GLuint getCurrentMaxComputeWorkGroupInvocations() const 256 { 257 return mCurrentMaxComputeWorkGroupInvocations; 258 } 259 260 unsigned int getMaxComputeSharedMemory() const { return mMaxComputeSharedMemory; } 261 bool hasBeenDeleted() const { return mDeleteStatus; } 262 263 // Block until compiling is finished and resolve it. 264 void resolveCompile(const Context *context); 265 266 // Writes a shader's binary to the output memory buffer. 267 angle::Result serialize(const Context *context, angle::MemoryBuffer *binaryOut) const; 268 angle::Result deserialize(const Context *context, BinaryInputStream &stream); 269 angle::Result loadBinary(const Context *context, const void *binary, GLsizei length); 270 271 private: 272 struct CompilingState; 273 274 ~Shader() override; 275 static void GetSourceImpl(const std::string &source, 276 GLsizei bufSize, 277 GLsizei *length, 278 char *buffer); 279 280 ShaderState mState; 281 std::unique_ptr<rx::ShaderImpl> mImplementation; 282 const gl::Limitations mRendererLimitations; 283 const ShaderProgramID mHandle; 284 const ShaderType mType; 285 unsigned int mRefCount; // Number of program objects this shader is attached to 286 bool mDeleteStatus; // Flag to indicate that the shader can be deleted when no longer in use 287 std::string mInfoLog; 288 289 // We keep a reference to the translator in order to defer compiles while preserving settings. 290 BindingPointer<Compiler> mBoundCompiler; 291 std::unique_ptr<CompilingState> mCompilingState; 292 std::string mCompilerResourcesString; 293 294 ShaderProgramManager *mResourceManager; 295 296 GLuint mCurrentMaxComputeWorkGroupInvocations; 297 unsigned int mMaxComputeSharedMemory; 298 }; 299 300 bool CompareShaderVar(const sh::ShaderVariable &x, const sh::ShaderVariable &y); 301 302 const char *GetShaderTypeString(ShaderType type); 303 } // namespace gl 304 305 #endif // LIBANGLE_SHADER_H_