• 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     rx::SpecConstUsageBits mSpecConstUsageBits;
139 
140     // ANGLE_multiview.
141     int mNumViews;
142 
143     // Geometry Shader.
144     Optional<PrimitiveMode> mGeometryShaderInputPrimitiveType;
145     Optional<PrimitiveMode> mGeometryShaderOutputPrimitiveType;
146     Optional<GLint> mGeometryShaderMaxVertices;
147     int mGeometryShaderInvocations;
148 
149     // Tessellation Shader
150     int mTessControlShaderVertices;
151     GLenum mTessGenMode;
152     GLenum mTessGenSpacing;
153     GLenum mTessGenVertexOrder;
154     GLenum mTessGenPointMode;
155 
156     // Indicates if this shader has been successfully compiled
157     CompileStatus mCompileStatus;
158 };
159 
160 class Shader final : angle::NonCopyable, public LabeledObject
161 {
162   public:
163     Shader(ShaderProgramManager *manager,
164            rx::GLImplFactory *implFactory,
165            const gl::Limitations &rendererLimitations,
166            ShaderType type,
167            ShaderProgramID handle);
168 
169     void onDestroy(const Context *context);
170 
171     void setLabel(const Context *context, const std::string &label) override;
172     const std::string &getLabel() const override;
173 
getType()174     ShaderType getType() const { return mType; }
175     ShaderProgramID getHandle() const;
176 
getImplementation()177     rx::ShaderImpl *getImplementation() const { return mImplementation.get(); }
178 
179     void setSource(GLsizei count, const char *const *string, const GLint *length);
180     int getInfoLogLength();
181     void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog);
getInfoLogString()182     std::string getInfoLogString() const { return mInfoLog; }
183     int getSourceLength() const;
getSourceString()184     const std::string &getSourceString() const { return mState.getSource(); }
185     void getSource(GLsizei bufSize, GLsizei *length, char *buffer) const;
186     int getTranslatedSourceLength();
187     int getTranslatedSourceWithDebugInfoLength();
188     const std::string &getTranslatedSource();
189     void getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer);
190     void getTranslatedSourceWithDebugInfo(GLsizei bufSize, GLsizei *length, char *buffer);
191     const sh::BinaryBlob &getCompiledBinary();
192 
193     void compile(const Context *context);
194     bool isCompiled();
195     bool isCompleted();
196 
197     void addRef();
198     void release(const Context *context);
199     unsigned int getRefCount() const;
200     bool isFlaggedForDeletion() const;
201     void flagForDeletion();
hasEarlyFragmentTestsOptimization()202     bool hasEarlyFragmentTestsOptimization() const
203     {
204         return mState.mEarlyFragmentTestsOptimization;
205     }
getSpecConstUsageBits()206     rx::SpecConstUsageBits getSpecConstUsageBits() const { return mState.mSpecConstUsageBits; }
207 
208     int getShaderVersion();
209 
210     const std::vector<sh::ShaderVariable> &getInputVaryings();
211     const std::vector<sh::ShaderVariable> &getOutputVaryings();
212     const std::vector<sh::ShaderVariable> &getUniforms();
213     const std::vector<sh::InterfaceBlock> &getUniformBlocks();
214     const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks();
215     const std::vector<sh::ShaderVariable> &getActiveAttributes();
216     const std::vector<sh::ShaderVariable> &getAllAttributes();
217     const std::vector<sh::ShaderVariable> &getActiveOutputVariables();
218 
219     // Returns mapped name of a transform feedback varying. The original name may contain array
220     // brackets with an index inside, which will get copied to the mapped name. The varying must be
221     // known to be declared in the shader.
222     std::string getTransformFeedbackVaryingMappedName(const std::string &tfVaryingName);
223 
224     const sh::WorkGroupSize &getWorkGroupSize();
225 
226     int getNumViews();
227 
228     Optional<PrimitiveMode> getGeometryShaderInputPrimitiveType();
229     Optional<PrimitiveMode> getGeometryShaderOutputPrimitiveType();
230     int getGeometryShaderInvocations();
231     Optional<GLint> getGeometryShaderMaxVertices();
232     int getTessControlShaderVertices();
233     GLenum getTessGenMode();
234     GLenum getTessGenSpacing();
235     GLenum getTessGenVertexOrder();
236     GLenum getTessGenPointMode();
237 
238     const std::string &getCompilerResourcesString() const;
239 
getState()240     const ShaderState &getState() const { return mState; }
241 
getCurrentMaxComputeWorkGroupInvocations()242     GLuint getCurrentMaxComputeWorkGroupInvocations() const
243     {
244         return mCurrentMaxComputeWorkGroupInvocations;
245     }
246 
getMaxComputeSharedMemory()247     unsigned int getMaxComputeSharedMemory() const { return mMaxComputeSharedMemory; }
hasBeenDeleted()248     bool hasBeenDeleted() const { return mDeleteStatus; }
249 
250   private:
251     struct CompilingState;
252 
253     ~Shader() override;
254     static void GetSourceImpl(const std::string &source,
255                               GLsizei bufSize,
256                               GLsizei *length,
257                               char *buffer);
258 
259     void resolveCompile();
260 
261     ShaderState mState;
262     std::unique_ptr<rx::ShaderImpl> mImplementation;
263     const gl::Limitations mRendererLimitations;
264     const ShaderProgramID mHandle;
265     const ShaderType mType;
266     unsigned int mRefCount;  // Number of program objects this shader is attached to
267     bool mDeleteStatus;  // Flag to indicate that the shader can be deleted when no longer in use
268     std::string mInfoLog;
269 
270     // We keep a reference to the translator in order to defer compiles while preserving settings.
271     BindingPointer<Compiler> mBoundCompiler;
272     std::unique_ptr<CompilingState> mCompilingState;
273     std::string mCompilerResourcesString;
274 
275     ShaderProgramManager *mResourceManager;
276 
277     GLuint mCurrentMaxComputeWorkGroupInvocations;
278     unsigned int mMaxComputeSharedMemory;
279 };
280 
281 bool CompareShaderVar(const sh::ShaderVariable &x, const sh::ShaderVariable &y);
282 
283 const char *GetShaderTypeString(ShaderType type);
284 }  // namespace gl
285 
286 #endif  // LIBANGLE_SHADER_H_
287