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