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 126 void setClearColor(const gl::ColorF &clearColor); 127 void setClearDepth(float clearDepth); 128 void setClearStencil(GLint clearStencil); 129 130 void setPixelUnpackState(const gl::PixelUnpackState &unpack); 131 void setPixelUnpackBuffer(const gl::Buffer *pixelBuffer); 132 void setPixelPackState(const gl::PixelPackState &pack); 133 void setPixelPackBuffer(const gl::Buffer *pixelBuffer); 134 135 void setFramebufferSRGBEnabled(const gl::Context *context, bool enabled); 136 void setFramebufferSRGBEnabledForFramebuffer(const gl::Context *context, 137 bool enabled, 138 const FramebufferGL *framebuffer); 139 void setColorMaskForFramebuffer(bool red, 140 bool green, 141 bool blue, 142 bool alpha, 143 const FramebufferGL *framebuffer); 144 145 void setDitherEnabled(bool enabled); 146 147 void setMultisamplingStateEnabled(bool enabled); 148 void setSampleAlphaToOneStateEnabled(bool enabled); 149 150 void setCoverageModulation(GLenum components); 151 152 void setPathRenderingModelViewMatrix(const GLfloat *m); 153 void setPathRenderingProjectionMatrix(const GLfloat *m); 154 void setPathRenderingStencilState(GLenum func, GLint ref, GLuint mask); 155 156 void setProvokingVertex(GLenum mode); 157 158 void pauseTransformFeedback(); 159 angle::Result pauseAllQueries(const gl::Context *context); 160 angle::Result pauseQuery(const gl::Context *context, gl::QueryType type); 161 angle::Result resumeAllQueries(const gl::Context *context); 162 angle::Result resumeQuery(const gl::Context *context, gl::QueryType type); 163 angle::Result onMakeCurrent(const gl::Context *context); 164 165 void syncState(const gl::Context *context, 166 const gl::State::DirtyBits &glDirtyBits, 167 const gl::State::DirtyBits &bitMask); 168 updateMultiviewBaseViewLayerIndexUniform(const gl::Program * program,const gl::FramebufferState & drawFramebufferState)169 ANGLE_INLINE void updateMultiviewBaseViewLayerIndexUniform( 170 const gl::Program *program, 171 const gl::FramebufferState &drawFramebufferState) const 172 { 173 if (mIsMultiviewEnabled && program && program->usesMultiview()) 174 { 175 updateMultiviewBaseViewLayerIndexUniformImpl(program, drawFramebufferState); 176 } 177 } 178 getProgramID()179 GLuint getProgramID() const { return mProgram; } getVertexArrayID()180 GLuint getVertexArrayID() const { return mVAO; } getFramebufferID(angle::FramebufferBinding binding)181 GLuint getFramebufferID(angle::FramebufferBinding binding) const 182 { 183 return mFramebuffers[binding]; 184 } 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 329 gl::ColorF mClearColor; 330 float mClearDepth; 331 GLint mClearStencil; 332 333 bool mFramebufferSRGBEnabled; 334 bool mDitherEnabled; 335 bool mTextureCubemapSeamlessEnabled; 336 337 bool mMultisamplingEnabled; 338 bool mSampleAlphaToOneEnabled; 339 340 GLenum mCoverageModulation; 341 342 GLfloat mPathMatrixMV[16]; 343 GLfloat mPathMatrixProj[16]; 344 GLenum mPathStencilFunc; 345 GLint mPathStencilRef; 346 GLuint mPathStencilMask; 347 348 const bool mIsMultiviewEnabled; 349 350 GLenum mProvokingVertex; 351 352 gl::State::DirtyBits mLocalDirtyBits; 353 gl::AttributesMask mLocalDirtyCurrentValues; 354 }; 355 } // namespace rx 356 357 #endif // LIBANGLE_RENDERER_GL_STATEMANAGERGL_H_ 358