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 setBlendEnabledIndexed(const gl::DrawBufferMask blendEnabledMask); 93 void setBlendColor(const gl::ColorF &blendColor); 94 void setBlendFuncs(const gl::BlendStateExt &blendStateExt); 95 void setBlendEquations(const gl::BlendStateExt &blendStateExt); 96 void setColorMask(bool red, bool green, bool blue, bool alpha); 97 void setSampleAlphaToCoverageEnabled(bool enabled); 98 void setSampleCoverageEnabled(bool enabled); 99 void setSampleCoverage(float value, bool invert); 100 void setSampleMaskEnabled(bool enabled); 101 void setSampleMaski(GLuint maskNumber, GLbitfield mask); 102 103 void setDepthTestEnabled(bool enabled); 104 void setDepthFunc(GLenum depthFunc); 105 void setDepthMask(bool mask); 106 void setStencilTestEnabled(bool enabled); 107 void setStencilFrontWritemask(GLuint mask); 108 void setStencilBackWritemask(GLuint mask); 109 void setStencilFrontFuncs(GLenum func, GLint ref, GLuint mask); 110 void setStencilBackFuncs(GLenum func, GLint ref, GLuint mask); 111 void setStencilFrontOps(GLenum sfail, GLenum dpfail, GLenum dppass); 112 void setStencilBackOps(GLenum sfail, GLenum dpfail, GLenum dppass); 113 114 void setCullFaceEnabled(bool enabled); 115 void setCullFace(gl::CullFaceMode cullFace); 116 void setFrontFace(GLenum frontFace); 117 void setPolygonOffsetFillEnabled(bool enabled); 118 void setPolygonOffset(float factor, float units); 119 void setRasterizerDiscardEnabled(bool enabled); 120 void setLineWidth(float width); 121 122 angle::Result setPrimitiveRestartEnabled(const gl::Context *context, bool enabled); 123 angle::Result setPrimitiveRestartIndex(const gl::Context *context, GLuint index); 124 125 void setClearColor(const gl::ColorF &clearColor); 126 void setClearDepth(float clearDepth); 127 void setClearStencil(GLint clearStencil); 128 129 void setPixelUnpackState(const gl::PixelUnpackState &unpack); 130 void setPixelUnpackBuffer(const gl::Buffer *pixelBuffer); 131 void setPixelPackState(const gl::PixelPackState &pack); 132 void setPixelPackBuffer(const gl::Buffer *pixelBuffer); 133 134 void setFramebufferSRGBEnabled(const gl::Context *context, bool enabled); 135 void setFramebufferSRGBEnabledForFramebuffer(const gl::Context *context, 136 bool enabled, 137 const FramebufferGL *framebuffer); 138 void setColorMaskForFramebuffer(const gl::BlendStateExt &blendStateExt, 139 const bool disableAlpha); 140 141 void setDitherEnabled(bool enabled); 142 143 void setMultisamplingStateEnabled(bool enabled); 144 void setSampleAlphaToOneStateEnabled(bool enabled); 145 146 void setCoverageModulation(GLenum components); 147 148 void setProvokingVertex(GLenum mode); 149 150 void setClipDistancesEnable(const gl::State::ClipDistanceEnableBits &enables); 151 152 void pauseTransformFeedback(); 153 angle::Result pauseAllQueries(const gl::Context *context); 154 angle::Result pauseQuery(const gl::Context *context, gl::QueryType type); 155 angle::Result resumeAllQueries(const gl::Context *context); 156 angle::Result resumeQuery(const gl::Context *context, gl::QueryType type); 157 angle::Result onMakeCurrent(const gl::Context *context); 158 159 angle::Result syncState(const gl::Context *context, 160 const gl::State::DirtyBits &glDirtyBits, 161 const gl::State::DirtyBits &bitMask); 162 updateMultiviewBaseViewLayerIndexUniform(const gl::Program * program,const gl::FramebufferState & drawFramebufferState)163 ANGLE_INLINE void updateMultiviewBaseViewLayerIndexUniform( 164 const gl::Program *program, 165 const gl::FramebufferState &drawFramebufferState) const 166 { 167 if (mIsMultiviewEnabled && program && program->usesMultiview()) 168 { 169 updateMultiviewBaseViewLayerIndexUniformImpl(program, drawFramebufferState); 170 } 171 } 172 getProgramID()173 GLuint getProgramID() const { return mProgram; } getVertexArrayID()174 GLuint getVertexArrayID() const { return mVAO; } getFramebufferID(angle::FramebufferBinding binding)175 GLuint getFramebufferID(angle::FramebufferBinding binding) const 176 { 177 return mFramebuffers[binding]; 178 } getBufferID(gl::BufferBinding binding)179 GLuint getBufferID(gl::BufferBinding binding) const { return mBuffers[binding]; } 180 181 void validateState() const; 182 183 private: 184 void setTextureCubemapSeamlessEnabled(bool enabled); 185 186 void propagateProgramToVAO(const gl::Program *program, VertexArrayGL *vao); 187 188 void updateProgramTextureBindings(const gl::Context *context); 189 void updateProgramStorageBufferBindings(const gl::Context *context); 190 void updateProgramUniformBufferBindings(const gl::Context *context); 191 void updateProgramAtomicCounterBufferBindings(const gl::Context *context); 192 void updateProgramImageBindings(const gl::Context *context); 193 194 void updateDispatchIndirectBufferBinding(const gl::Context *context); 195 void updateDrawIndirectBufferBinding(const gl::Context *context); 196 197 void syncSamplersState(const gl::Context *context); 198 void syncTransformFeedbackState(const gl::Context *context); 199 200 void updateMultiviewBaseViewLayerIndexUniformImpl( 201 const gl::Program *program, 202 const gl::FramebufferState &drawFramebufferState) const; 203 204 const FunctionsGL *mFunctions; 205 const angle::FeaturesGL &mFeatures; 206 207 GLuint mProgram; 208 209 GLuint mVAO; 210 std::vector<gl::VertexAttribCurrentValueData> mVertexAttribCurrentValues; 211 212 angle::PackedEnumMap<gl::BufferBinding, GLuint> mBuffers; 213 214 struct IndexedBufferBinding 215 { 216 IndexedBufferBinding(); 217 218 size_t offset; 219 size_t size; 220 GLuint buffer; 221 }; 222 angle::PackedEnumMap<gl::BufferBinding, std::vector<IndexedBufferBinding>> mIndexedBuffers; 223 224 size_t mTextureUnitIndex; 225 angle::PackedEnumMap<gl::TextureType, gl::ActiveTextureArray<GLuint>> mTextures; 226 gl::ActiveTextureArray<GLuint> mSamplers; 227 228 struct ImageUnitBinding 229 { ImageUnitBindingImageUnitBinding230 ImageUnitBinding() 231 : texture(0), level(0), layered(false), layer(0), access(GL_READ_ONLY), format(GL_R32UI) 232 {} 233 234 GLuint texture; 235 GLint level; 236 GLboolean layered; 237 GLint layer; 238 GLenum access; 239 GLenum format; 240 }; 241 std::vector<ImageUnitBinding> mImages; 242 243 GLuint mTransformFeedback; 244 TransformFeedbackGL *mCurrentTransformFeedback; 245 246 // Queries that are currently running on the driver 247 angle::PackedEnumMap<gl::QueryType, QueryGL *> mQueries; 248 249 // Queries that are temporarily in the paused state so that their results will not be affected 250 // by other operations 251 angle::PackedEnumMap<gl::QueryType, QueryGL *> mTemporaryPausedQueries; 252 253 gl::ContextID mPrevDrawContext; 254 255 GLint mUnpackAlignment; 256 GLint mUnpackRowLength; 257 GLint mUnpackSkipRows; 258 GLint mUnpackSkipPixels; 259 GLint mUnpackImageHeight; 260 GLint mUnpackSkipImages; 261 262 GLint mPackAlignment; 263 GLint mPackRowLength; 264 GLint mPackSkipRows; 265 GLint mPackSkipPixels; 266 267 // TODO(jmadill): Convert to std::array when available 268 std::vector<GLenum> mFramebuffers; 269 GLuint mRenderbuffer; 270 271 bool mScissorTestEnabled; 272 gl::Rectangle mScissor; 273 gl::Rectangle mViewport; 274 float mNear; 275 float mFar; 276 277 gl::ColorF mBlendColor; 278 gl::BlendStateExt mBlendStateExt; 279 const bool mIndependentBlendStates; 280 281 bool mSampleAlphaToCoverageEnabled; 282 bool mSampleCoverageEnabled; 283 float mSampleCoverageValue; 284 bool mSampleCoverageInvert; 285 bool mSampleMaskEnabled; 286 std::array<GLbitfield, gl::MAX_SAMPLE_MASK_WORDS> mSampleMaskValues; 287 288 bool mDepthTestEnabled; 289 GLenum mDepthFunc; 290 bool mDepthMask; 291 bool mStencilTestEnabled; 292 GLenum mStencilFrontFunc; 293 GLint mStencilFrontRef; 294 GLuint mStencilFrontValueMask; 295 GLenum mStencilFrontStencilFailOp; 296 GLenum mStencilFrontStencilPassDepthFailOp; 297 GLenum mStencilFrontStencilPassDepthPassOp; 298 GLuint mStencilFrontWritemask; 299 GLenum mStencilBackFunc; 300 GLint mStencilBackRef; 301 GLuint mStencilBackValueMask; 302 GLenum mStencilBackStencilFailOp; 303 GLenum mStencilBackStencilPassDepthFailOp; 304 GLenum mStencilBackStencilPassDepthPassOp; 305 GLuint mStencilBackWritemask; 306 307 bool mCullFaceEnabled; 308 gl::CullFaceMode mCullFace; 309 GLenum mFrontFace; 310 bool mPolygonOffsetFillEnabled; 311 GLfloat mPolygonOffsetFactor; 312 GLfloat mPolygonOffsetUnits; 313 bool mRasterizerDiscardEnabled; 314 float mLineWidth; 315 316 bool mPrimitiveRestartEnabled; 317 GLuint mPrimitiveRestartIndex; 318 319 gl::ColorF mClearColor; 320 float mClearDepth; 321 GLint mClearStencil; 322 323 bool mFramebufferSRGBAvailable; 324 bool mFramebufferSRGBEnabled; 325 326 bool mDitherEnabled; 327 bool mTextureCubemapSeamlessEnabled; 328 329 bool mMultisamplingEnabled; 330 bool mSampleAlphaToOneEnabled; 331 332 GLenum mCoverageModulation; 333 334 const bool mIsMultiviewEnabled; 335 336 GLenum mProvokingVertex; 337 338 gl::State::ClipDistanceEnableBits mEnabledClipDistances; 339 const size_t mMaxClipDistances; 340 341 gl::State::DirtyBits mLocalDirtyBits; 342 gl::AttributesMask mLocalDirtyCurrentValues; 343 }; 344 } // namespace rx 345 346 #endif // LIBANGLE_RENDERER_GL_STATEMANAGERGL_H_ 347