1 //
2 // Copyright 2015 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 // StateManagerGL.h: Defines a class for caching applied OpenGL state
8 
9 #ifndef LIBANGLE_RENDERER_GL_STATEMANAGERGL_H_
10 #define LIBANGLE_RENDERER_GL_STATEMANAGERGL_H_
11 
12 #include "common/debug.h"
13 #include "libANGLE/Error.h"
14 #include "libANGLE/State.h"
15 #include "libANGLE/angletypes.h"
16 #include "libANGLE/renderer/gl/functionsgl_typedefs.h"
17 #include "platform/FeaturesGL.h"
18 
19 #include <map>
20 
21 namespace gl
22 {
23 struct Caps;
24 class FramebufferState;
25 class State;
26 }  // namespace gl
27 
28 namespace rx
29 {
30 
31 class FramebufferGL;
32 class FunctionsGL;
33 class TransformFeedbackGL;
34 class VertexArrayGL;
35 class QueryGL;
36 
37 class StateManagerGL final : angle::NonCopyable
38 {
39   public:
40     StateManagerGL(const FunctionsGL *functions,
41                    const gl::Caps &rendererCaps,
42                    const gl::Extensions &extensions,
43                    const angle::FeaturesGL &features);
44     ~StateManagerGL();
45 
46     void deleteProgram(GLuint program);
47     void deleteVertexArray(GLuint vao);
48     void deleteTexture(GLuint texture);
49     void deleteSampler(GLuint sampler);
50     void deleteBuffer(GLuint buffer);
51     void deleteFramebuffer(GLuint fbo);
52     void deleteRenderbuffer(GLuint rbo);
53     void deleteTransformFeedback(GLuint transformFeedback);
54 
55     void useProgram(GLuint program);
56     void forceUseProgram(GLuint program);
57     void bindVertexArray(GLuint vao, GLuint elementArrayBuffer);
58     void bindBuffer(gl::BufferBinding target, GLuint buffer);
59     void bindBufferBase(gl::BufferBinding target, size_t index, GLuint buffer);
60     void bindBufferRange(gl::BufferBinding target,
61                          size_t index,
62                          GLuint buffer,
63                          size_t offset,
64                          size_t size);
65     void activeTexture(size_t unit);
66     void bindTexture(gl::TextureType type, GLuint texture);
67     void invalidateTexture(gl::TextureType type);
68     void bindSampler(size_t unit, GLuint sampler);
69     void bindImageTexture(size_t unit,
70                           GLuint texture,
71                           GLint level,
72                           GLboolean layered,
73                           GLint layer,
74                           GLenum access,
75                           GLenum format);
76     void bindFramebuffer(GLenum type, GLuint framebuffer);
77     void bindRenderbuffer(GLenum type, GLuint renderbuffer);
78     void bindTransformFeedback(GLenum type, GLuint transformFeedback);
79     void onTransformFeedbackStateChange();
80     void beginQuery(gl::QueryType type, QueryGL *queryObject, GLuint queryId);
81     void endQuery(gl::QueryType type, QueryGL *queryObject, GLuint queryId);
82 
83     void setAttributeCurrentData(size_t index, const gl::VertexAttribCurrentValueData &data);
84 
85     void setScissorTestEnabled(bool enabled);
86     void setScissor(const gl::Rectangle &scissor);
87 
88     void setViewport(const gl::Rectangle &viewport);
89     void setDepthRange(float near, float far);
90 
91     void setBlendEnabled(bool enabled);
92     void setBlendColor(const gl::ColorF &blendColor);
93     void setBlendFuncs(GLenum sourceBlendRGB,
94                        GLenum destBlendRGB,
95                        GLenum sourceBlendAlpha,
96                        GLenum destBlendAlpha);
97     void setBlendEquations(GLenum blendEquationRGB, GLenum blendEquationAlpha);
98     void setColorMask(bool red, bool green, bool blue, bool alpha);
99     void setSampleAlphaToCoverageEnabled(bool enabled);
100     void setSampleCoverageEnabled(bool enabled);
101     void setSampleCoverage(float value, bool invert);
102     void setSampleMaskEnabled(bool enabled);
103     void setSampleMaski(GLuint maskNumber, GLbitfield mask);
104 
105     void setDepthTestEnabled(bool enabled);
106     void setDepthFunc(GLenum depthFunc);
107     void setDepthMask(bool mask);
108     void setStencilTestEnabled(bool enabled);
109     void setStencilFrontWritemask(GLuint mask);
110     void setStencilBackWritemask(GLuint mask);
111     void setStencilFrontFuncs(GLenum func, GLint ref, GLuint mask);
112     void setStencilBackFuncs(GLenum func, GLint ref, GLuint mask);
113     void setStencilFrontOps(GLenum sfail, GLenum dpfail, GLenum dppass);
114     void setStencilBackOps(GLenum sfail, GLenum dpfail, GLenum dppass);
115 
116     void setCullFaceEnabled(bool enabled);
117     void setCullFace(gl::CullFaceMode cullFace);
118     void setFrontFace(GLenum frontFace);
119     void setPolygonOffsetFillEnabled(bool enabled);
120     void setPolygonOffset(float factor, float units);
121     void setRasterizerDiscardEnabled(bool enabled);
122     void setLineWidth(float width);
123 
124     void setPrimitiveRestartEnabled(bool enabled);
125     void setPrimitiveRestartIndex(GLuint index);
126 
127     void setClearColor(const gl::ColorF &clearColor);
128     void setClearDepth(float clearDepth);
129     void setClearStencil(GLint clearStencil);
130 
131     void setPixelUnpackState(const gl::PixelUnpackState &unpack);
132     void setPixelUnpackBuffer(const gl::Buffer *pixelBuffer);
133     void setPixelPackState(const gl::PixelPackState &pack);
134     void setPixelPackBuffer(const gl::Buffer *pixelBuffer);
135 
136     void setFramebufferSRGBEnabled(const gl::Context *context, bool enabled);
137     void setFramebufferSRGBEnabledForFramebuffer(const gl::Context *context,
138                                                  bool enabled,
139                                                  const FramebufferGL *framebuffer);
140     void setColorMaskForFramebuffer(bool red,
141                                     bool green,
142                                     bool blue,
143                                     bool alpha,
144                                     const FramebufferGL *framebuffer);
145 
146     void setDitherEnabled(bool enabled);
147 
148     void setMultisamplingStateEnabled(bool enabled);
149     void setSampleAlphaToOneStateEnabled(bool enabled);
150 
151     void setCoverageModulation(GLenum components);
152 
153     void setProvokingVertex(GLenum mode);
154 
155     void pauseTransformFeedback();
156     angle::Result pauseAllQueries(const gl::Context *context);
157     angle::Result pauseQuery(const gl::Context *context, gl::QueryType type);
158     angle::Result resumeAllQueries(const gl::Context *context);
159     angle::Result resumeQuery(const gl::Context *context, gl::QueryType type);
160     angle::Result onMakeCurrent(const gl::Context *context);
161 
162     void syncState(const gl::Context *context,
163                    const gl::State::DirtyBits &glDirtyBits,
164                    const gl::State::DirtyBits &bitMask);
165 
updateMultiviewBaseViewLayerIndexUniform(const gl::Program * program,const gl::FramebufferState & drawFramebufferState)166     ANGLE_INLINE void updateMultiviewBaseViewLayerIndexUniform(
167         const gl::Program *program,
168         const gl::FramebufferState &drawFramebufferState) const
169     {
170         if (mIsMultiviewEnabled && program && program->usesMultiview())
171         {
172             updateMultiviewBaseViewLayerIndexUniformImpl(program, drawFramebufferState);
173         }
174     }
175 
getProgramID()176     GLuint getProgramID() const { return mProgram; }
getVertexArrayID()177     GLuint getVertexArrayID() const { return mVAO; }
getFramebufferID(angle::FramebufferBinding binding)178     GLuint getFramebufferID(angle::FramebufferBinding binding) const
179     {
180         return mFramebuffers[binding];
181     }
getBufferID(gl::BufferBinding binding)182     GLuint getBufferID(gl::BufferBinding binding) const { return mBuffers[binding]; }
183 
184     void validateState() const;
185 
186   private:
187     void setTextureCubemapSeamlessEnabled(bool enabled);
188 
189     void propagateProgramToVAO(const gl::Program *program, VertexArrayGL *vao);
190 
191     void updateProgramTextureBindings(const gl::Context *context);
192     void updateProgramStorageBufferBindings(const gl::Context *context);
193     void updateProgramUniformBufferBindings(const gl::Context *context);
194     void updateProgramAtomicCounterBufferBindings(const gl::Context *context);
195     void updateProgramImageBindings(const gl::Context *context);
196 
197     void updateDispatchIndirectBufferBinding(const gl::Context *context);
198     void updateDrawIndirectBufferBinding(const gl::Context *context);
199 
200     void syncSamplersState(const gl::Context *context);
201     void syncTransformFeedbackState(const gl::Context *context);
202 
203     void updateMultiviewBaseViewLayerIndexUniformImpl(
204         const gl::Program *program,
205         const gl::FramebufferState &drawFramebufferState) const;
206 
207     const FunctionsGL *mFunctions;
208     const angle::FeaturesGL &mFeatures;
209 
210     GLuint mProgram;
211 
212     GLuint mVAO;
213     std::vector<gl::VertexAttribCurrentValueData> mVertexAttribCurrentValues;
214 
215     angle::PackedEnumMap<gl::BufferBinding, GLuint> mBuffers;
216 
217     struct IndexedBufferBinding
218     {
219         IndexedBufferBinding();
220 
221         size_t offset;
222         size_t size;
223         GLuint buffer;
224     };
225     angle::PackedEnumMap<gl::BufferBinding, std::vector<IndexedBufferBinding>> mIndexedBuffers;
226 
227     size_t mTextureUnitIndex;
228     angle::PackedEnumMap<gl::TextureType, gl::ActiveTextureArray<GLuint>> mTextures;
229     gl::ActiveTextureArray<GLuint> mSamplers;
230 
231     struct ImageUnitBinding
232     {
ImageUnitBindingImageUnitBinding233         ImageUnitBinding()
234             : texture(0), level(0), layered(false), layer(0), access(GL_READ_ONLY), format(GL_R32UI)
235         {}
236 
237         GLuint texture;
238         GLint level;
239         GLboolean layered;
240         GLint layer;
241         GLenum access;
242         GLenum format;
243     };
244     std::vector<ImageUnitBinding> mImages;
245 
246     GLuint mTransformFeedback;
247     TransformFeedbackGL *mCurrentTransformFeedback;
248 
249     // Queries that are currently running on the driver
250     angle::PackedEnumMap<gl::QueryType, QueryGL *> mQueries;
251 
252     // Queries that are temporarily in the paused state so that their results will not be affected
253     // by other operations
254     angle::PackedEnumMap<gl::QueryType, QueryGL *> mTemporaryPausedQueries;
255 
256     gl::ContextID mPrevDrawContext;
257 
258     GLint mUnpackAlignment;
259     GLint mUnpackRowLength;
260     GLint mUnpackSkipRows;
261     GLint mUnpackSkipPixels;
262     GLint mUnpackImageHeight;
263     GLint mUnpackSkipImages;
264 
265     GLint mPackAlignment;
266     GLint mPackRowLength;
267     GLint mPackSkipRows;
268     GLint mPackSkipPixels;
269 
270     // TODO(jmadill): Convert to std::array when available
271     std::vector<GLenum> mFramebuffers;
272     GLuint mRenderbuffer;
273 
274     bool mScissorTestEnabled;
275     gl::Rectangle mScissor;
276     gl::Rectangle mViewport;
277     float mNear;
278     float mFar;
279 
280     bool mBlendEnabled;
281     gl::ColorF mBlendColor;
282     GLenum mSourceBlendRGB;
283     GLenum mDestBlendRGB;
284     GLenum mSourceBlendAlpha;
285     GLenum mDestBlendAlpha;
286     GLenum mBlendEquationRGB;
287     GLenum mBlendEquationAlpha;
288     bool mColorMaskRed;
289     bool mColorMaskGreen;
290     bool mColorMaskBlue;
291     bool mColorMaskAlpha;
292     bool mSampleAlphaToCoverageEnabled;
293     bool mSampleCoverageEnabled;
294     float mSampleCoverageValue;
295     bool mSampleCoverageInvert;
296     bool mSampleMaskEnabled;
297     std::array<GLbitfield, gl::MAX_SAMPLE_MASK_WORDS> mSampleMaskValues;
298 
299     bool mDepthTestEnabled;
300     GLenum mDepthFunc;
301     bool mDepthMask;
302     bool mStencilTestEnabled;
303     GLenum mStencilFrontFunc;
304     GLint mStencilFrontRef;
305     GLuint mStencilFrontValueMask;
306     GLenum mStencilFrontStencilFailOp;
307     GLenum mStencilFrontStencilPassDepthFailOp;
308     GLenum mStencilFrontStencilPassDepthPassOp;
309     GLuint mStencilFrontWritemask;
310     GLenum mStencilBackFunc;
311     GLint mStencilBackRef;
312     GLuint mStencilBackValueMask;
313     GLenum mStencilBackStencilFailOp;
314     GLenum mStencilBackStencilPassDepthFailOp;
315     GLenum mStencilBackStencilPassDepthPassOp;
316     GLuint mStencilBackWritemask;
317 
318     bool mCullFaceEnabled;
319     gl::CullFaceMode mCullFace;
320     GLenum mFrontFace;
321     bool mPolygonOffsetFillEnabled;
322     GLfloat mPolygonOffsetFactor;
323     GLfloat mPolygonOffsetUnits;
324     bool mRasterizerDiscardEnabled;
325     float mLineWidth;
326 
327     bool mPrimitiveRestartEnabled;
328     GLuint mPrimitiveRestartIndex;
329 
330     gl::ColorF mClearColor;
331     float mClearDepth;
332     GLint mClearStencil;
333 
334     bool mFramebufferSRGBEnabled;
335     bool mDitherEnabled;
336     bool mTextureCubemapSeamlessEnabled;
337 
338     bool mMultisamplingEnabled;
339     bool mSampleAlphaToOneEnabled;
340 
341     GLenum mCoverageModulation;
342 
343     const bool mIsMultiviewEnabled;
344 
345     GLenum mProvokingVertex;
346 
347     gl::State::DirtyBits mLocalDirtyBits;
348     gl::AttributesMask mLocalDirtyCurrentValues;
349 };
350 }  // namespace rx
351 
352 #endif  // LIBANGLE_RENDERER_GL_STATEMANAGERGL_H_
353