1 // 2 // Copyright (c) 2002-2013 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 // Context.h: Defines the gl::Context class, managing all GL state and performing 8 // rendering operations. It is the GLES2 specific implementation of EGLContext. 9 10 #ifndef LIBGLESV2_CONTEXT_H_ 11 #define LIBGLESV2_CONTEXT_H_ 12 13 #define GL_APICALL 14 #include <GLES2/gl2.h> 15 #include <GLES2/gl2ext.h> 16 #define EGLAPI 17 #include <EGL/egl.h> 18 19 #include <string> 20 #include <map> 21 #include <set> 22 #ifdef _MSC_VER 23 #include <hash_map> 24 #else 25 #include <unordered_map> 26 #endif 27 28 #include "common/angleutils.h" 29 #include "common/RefCountObject.h" 30 #include "libGLESv2/HandleAllocator.h" 31 #include "libGLESv2/angletypes.h" 32 #include "libGLESv2/Constants.h" 33 34 namespace rx 35 { 36 class Renderer; 37 } 38 39 namespace egl 40 { 41 class Surface; 42 } 43 44 namespace gl 45 { 46 class Shader; 47 class Program; 48 class ProgramBinary; 49 class Texture; 50 class Texture2D; 51 class TextureCubeMap; 52 class Framebuffer; 53 class Renderbuffer; 54 class RenderbufferStorage; 55 class Colorbuffer; 56 class Depthbuffer; 57 class Stencilbuffer; 58 class DepthStencilbuffer; 59 class Fence; 60 class Query; 61 class ResourceManager; 62 class Buffer; 63 64 enum QueryType 65 { 66 QUERY_ANY_SAMPLES_PASSED, 67 QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE, 68 69 QUERY_TYPE_COUNT 70 }; 71 72 // Helper structure describing a single vertex attribute 73 class VertexAttribute 74 { 75 public: 76 VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mPointer(NULL), mArrayEnabled(false), mDivisor(0) 77 { 78 mCurrentValue[0] = 0.0f; 79 mCurrentValue[1] = 0.0f; 80 mCurrentValue[2] = 0.0f; 81 mCurrentValue[3] = 1.0f; 82 } 83 84 int typeSize() const 85 { 86 switch (mType) 87 { 88 case GL_BYTE: return mSize * sizeof(GLbyte); 89 case GL_UNSIGNED_BYTE: return mSize * sizeof(GLubyte); 90 case GL_SHORT: return mSize * sizeof(GLshort); 91 case GL_UNSIGNED_SHORT: return mSize * sizeof(GLushort); 92 case GL_FIXED: return mSize * sizeof(GLfixed); 93 case GL_FLOAT: return mSize * sizeof(GLfloat); 94 default: UNREACHABLE(); return mSize * sizeof(GLfloat); 95 } 96 } 97 98 GLsizei stride() const 99 { 100 return mStride ? mStride : typeSize(); 101 } 102 103 // From glVertexAttribPointer 104 GLenum mType; 105 GLint mSize; 106 bool mNormalized; 107 GLsizei mStride; // 0 means natural stride 108 109 union 110 { 111 const void *mPointer; 112 intptr_t mOffset; 113 }; 114 115 BindingPointer<Buffer> mBoundBuffer; // Captured when glVertexAttribPointer is called. 116 117 bool mArrayEnabled; // From glEnable/DisableVertexAttribArray 118 float mCurrentValue[4]; // From glVertexAttrib 119 unsigned int mDivisor; 120 }; 121 122 // Helper structure to store all raw state 123 struct State 124 { 125 Color colorClearValue; 126 GLclampf depthClearValue; 127 int stencilClearValue; 128 129 RasterizerState rasterizer; 130 bool scissorTest; 131 Rectangle scissor; 132 133 BlendState blend; 134 Color blendColor; 135 bool sampleCoverage; 136 GLclampf sampleCoverageValue; 137 bool sampleCoverageInvert; 138 139 DepthStencilState depthStencil; 140 GLint stencilRef; 141 GLint stencilBackRef; 142 143 GLfloat lineWidth; 144 145 GLenum generateMipmapHint; 146 GLenum fragmentShaderDerivativeHint; 147 148 Rectangle viewport; 149 float zNear; 150 float zFar; 151 152 unsigned int activeSampler; // Active texture unit selector - GL_TEXTURE0 153 BindingPointer<Buffer> arrayBuffer; 154 BindingPointer<Buffer> elementArrayBuffer; 155 GLuint readFramebuffer; 156 GLuint drawFramebuffer; 157 BindingPointer<Renderbuffer> renderbuffer; 158 GLuint currentProgram; 159 160 VertexAttribute vertexAttribute[MAX_VERTEX_ATTRIBS]; 161 BindingPointer<Texture> samplerTexture[TEXTURE_TYPE_COUNT][IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS]; 162 BindingPointer<Query> activeQuery[QUERY_TYPE_COUNT]; 163 164 GLint unpackAlignment; 165 GLint packAlignment; 166 bool packReverseRowOrder; 167 }; 168 169 class Context 170 { 171 public: 172 Context(const gl::Context *shareContext, rx::Renderer *renderer, bool notifyResets, bool robustAccess); 173 174 ~Context(); 175 176 void makeCurrent(egl::Surface *surface); 177 178 virtual void markContextLost(); 179 bool isContextLost(); 180 181 // State manipulation 182 void setClearColor(float red, float green, float blue, float alpha); 183 184 void setClearDepth(float depth); 185 186 void setClearStencil(int stencil); 187 188 void setCullFace(bool enabled); 189 bool isCullFaceEnabled() const; 190 191 void setCullMode(GLenum mode); 192 193 void setFrontFace(GLenum front); 194 195 void setDepthTest(bool enabled); 196 bool isDepthTestEnabled() const; 197 198 void setDepthFunc(GLenum depthFunc); 199 200 void setDepthRange(float zNear, float zFar); 201 202 void setBlend(bool enabled); 203 bool isBlendEnabled() const; 204 205 void setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha); 206 void setBlendColor(float red, float green, float blue, float alpha); 207 void setBlendEquation(GLenum rgbEquation, GLenum alphaEquation); 208 209 void setStencilTest(bool enabled); 210 bool isStencilTestEnabled() const; 211 212 void setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask); 213 void setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask); 214 void setStencilWritemask(GLuint stencilWritemask); 215 void setStencilBackWritemask(GLuint stencilBackWritemask); 216 void setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass); 217 void setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass); 218 219 void setPolygonOffsetFill(bool enabled); 220 bool isPolygonOffsetFillEnabled() const; 221 222 void setPolygonOffsetParams(GLfloat factor, GLfloat units); 223 224 void setSampleAlphaToCoverage(bool enabled); 225 bool isSampleAlphaToCoverageEnabled() const; 226 227 void setSampleCoverage(bool enabled); 228 bool isSampleCoverageEnabled() const; 229 230 void setSampleCoverageParams(GLclampf value, bool invert); 231 232 void setScissorTest(bool enabled); 233 bool isScissorTestEnabled() const; 234 235 void setDither(bool enabled); 236 bool isDitherEnabled() const; 237 238 void setLineWidth(GLfloat width); 239 240 void setGenerateMipmapHint(GLenum hint); 241 void setFragmentShaderDerivativeHint(GLenum hint); 242 243 void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height); 244 245 void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height); 246 247 void setColorMask(bool red, bool green, bool blue, bool alpha); 248 void setDepthMask(bool mask); 249 250 void setActiveSampler(unsigned int active); 251 252 GLuint getReadFramebufferHandle() const; 253 GLuint getDrawFramebufferHandle() const; 254 GLuint getRenderbufferHandle() const; 255 256 GLuint getArrayBufferHandle() const; 257 258 GLuint getActiveQuery(GLenum target) const; 259 260 void setEnableVertexAttribArray(unsigned int attribNum, bool enabled); 261 const VertexAttribute &getVertexAttribState(unsigned int attribNum); 262 void setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, 263 bool normalized, GLsizei stride, const void *pointer); 264 const void *getVertexAttribPointer(unsigned int attribNum) const; 265 266 void setUnpackAlignment(GLint alignment); 267 GLint getUnpackAlignment() const; 268 269 void setPackAlignment(GLint alignment); 270 GLint getPackAlignment() const; 271 272 void setPackReverseRowOrder(bool reverseRowOrder); 273 bool getPackReverseRowOrder() const; 274 275 // These create and destroy methods are merely pass-throughs to 276 // ResourceManager, which owns these object types 277 GLuint createBuffer(); 278 GLuint createShader(GLenum type); 279 GLuint createProgram(); 280 GLuint createTexture(); 281 GLuint createRenderbuffer(); 282 283 void deleteBuffer(GLuint buffer); 284 void deleteShader(GLuint shader); 285 void deleteProgram(GLuint program); 286 void deleteTexture(GLuint texture); 287 void deleteRenderbuffer(GLuint renderbuffer); 288 289 // Framebuffers are owned by the Context, so these methods do not pass through 290 GLuint createFramebuffer(); 291 void deleteFramebuffer(GLuint framebuffer); 292 293 // Fences are owned by the Context. 294 GLuint createFence(); 295 void deleteFence(GLuint fence); 296 297 // Queries are owned by the Context; 298 GLuint createQuery(); 299 void deleteQuery(GLuint query); 300 301 void bindArrayBuffer(GLuint buffer); 302 void bindElementArrayBuffer(GLuint buffer); 303 void bindTexture2D(GLuint texture); 304 void bindTextureCubeMap(GLuint texture); 305 void bindReadFramebuffer(GLuint framebuffer); 306 void bindDrawFramebuffer(GLuint framebuffer); 307 void bindRenderbuffer(GLuint renderbuffer); 308 void useProgram(GLuint program); 309 void linkProgram(GLuint program); 310 void setProgramBinary(GLuint program, const void *binary, GLint length); 311 312 void beginQuery(GLenum target, GLuint query); 313 void endQuery(GLenum target); 314 315 void setFramebufferZero(Framebuffer *framebuffer); 316 317 void setRenderbufferStorage(GLsizei width, GLsizei height, GLenum internalformat, GLsizei samples); 318 319 void setVertexAttrib(GLuint index, const GLfloat *values); 320 void setVertexAttribDivisor(GLuint index, GLuint divisor); 321 322 Buffer *getBuffer(GLuint handle); 323 Fence *getFence(GLuint handle); 324 Shader *getShader(GLuint handle); 325 Program *getProgram(GLuint handle); 326 Texture *getTexture(GLuint handle); 327 Framebuffer *getFramebuffer(GLuint handle); 328 Renderbuffer *getRenderbuffer(GLuint handle); 329 Query *getQuery(GLuint handle, bool create, GLenum type); 330 331 Buffer *getArrayBuffer(); 332 Buffer *getElementArrayBuffer(); 333 ProgramBinary *getCurrentProgramBinary(); 334 Texture2D *getTexture2D(); 335 TextureCubeMap *getTextureCubeMap(); 336 Texture *getSamplerTexture(unsigned int sampler, TextureType type); 337 Framebuffer *getReadFramebuffer(); 338 Framebuffer *getDrawFramebuffer(); 339 340 bool getFloatv(GLenum pname, GLfloat *params); 341 bool getIntegerv(GLenum pname, GLint *params); 342 bool getBooleanv(GLenum pname, GLboolean *params); 343 344 bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams); 345 346 void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels); 347 void clear(GLbitfield mask); 348 void drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances); 349 void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances); 350 void sync(bool block); // flush/finish 351 352 void recordInvalidEnum(); 353 void recordInvalidValue(); 354 void recordInvalidOperation(); 355 void recordOutOfMemory(); 356 void recordInvalidFramebufferOperation(); 357 358 GLenum getError(); 359 GLenum getResetStatus(); 360 virtual bool isResetNotificationEnabled(); 361 362 int getMajorShaderModel() const; 363 float getMaximumPointSize() const; 364 unsigned int getMaximumCombinedTextureImageUnits() const; 365 int getMaximumRenderbufferDimension() const; 366 int getMaximumTextureDimension() const; 367 int getMaximumCubeTextureDimension() const; 368 int getMaximumTextureLevel() const; 369 unsigned int getMaximumRenderTargets() const; 370 GLsizei getMaxSupportedSamples() const; 371 const char *getExtensionString() const; 372 const char *getRendererString() const; 373 bool supportsEventQueries() const; 374 bool supportsOcclusionQueries() const; 375 bool supportsBGRATextures() const; 376 bool supportsDXT1Textures() const; 377 bool supportsDXT3Textures() const; 378 bool supportsDXT5Textures() const; 379 bool supportsFloat32Textures() const; 380 bool supportsFloat32LinearFilter() const; 381 bool supportsFloat32RenderableTextures() const; 382 bool supportsFloat16Textures() const; 383 bool supportsFloat16LinearFilter() const; 384 bool supportsFloat16RenderableTextures() const; 385 bool supportsLuminanceTextures() const; 386 bool supportsLuminanceAlphaTextures() const; 387 bool supportsDepthTextures() const; 388 bool supports32bitIndices() const; 389 bool supportsNonPower2Texture() const; 390 bool supportsInstancing() const; 391 bool supportsTextureFilterAnisotropy() const; 392 393 bool getCurrentReadFormatType(GLenum *format, GLenum *type); 394 395 float getTextureMaxAnisotropy() const; 396 397 void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, 398 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, 399 GLbitfield mask); 400 401 rx::Renderer *getRenderer() { return mRenderer; } 402 403 private: 404 DISALLOW_COPY_AND_ASSIGN(Context); 405 406 bool applyRenderTarget(GLenum drawMode, bool ignoreViewport); 407 void applyState(GLenum drawMode); 408 void applyShaders(); 409 void applyTextures(); 410 void applyTextures(SamplerType type); 411 412 void detachBuffer(GLuint buffer); 413 void detachTexture(GLuint texture); 414 void detachFramebuffer(GLuint framebuffer); 415 void detachRenderbuffer(GLuint renderbuffer); 416 417 Texture *getIncompleteTexture(TextureType type); 418 419 bool skipDraw(GLenum drawMode); 420 421 void initExtensionString(); 422 void initRendererString(); 423 424 typedef std::set<unsigned> FramebufferTextureSerialSet; 425 FramebufferTextureSerialSet getBoundFramebufferTextureSerials(); 426 427 rx::Renderer *const mRenderer; 428 429 State mState; 430 431 BindingPointer<Texture2D> mTexture2DZero; 432 BindingPointer<TextureCubeMap> mTextureCubeMapZero; 433 434 #ifndef HASH_MAP 435 # ifdef _MSC_VER 436 # define HASH_MAP stdext::hash_map 437 # else 438 # define HASH_MAP std::unordered_map 439 # endif 440 #endif 441 442 typedef HASH_MAP<GLuint, Framebuffer*> FramebufferMap; 443 FramebufferMap mFramebufferMap; 444 HandleAllocator mFramebufferHandleAllocator; 445 446 typedef HASH_MAP<GLuint, Fence*> FenceMap; 447 FenceMap mFenceMap; 448 HandleAllocator mFenceHandleAllocator; 449 450 typedef HASH_MAP<GLuint, Query*> QueryMap; 451 QueryMap mQueryMap; 452 HandleAllocator mQueryHandleAllocator; 453 454 const char *mExtensionString; 455 const char *mRendererString; 456 457 BindingPointer<Texture> mIncompleteTextures[TEXTURE_TYPE_COUNT]; 458 459 // Recorded errors 460 bool mInvalidEnum; 461 bool mInvalidValue; 462 bool mInvalidOperation; 463 bool mOutOfMemory; 464 bool mInvalidFramebufferOperation; 465 466 // Current/lost context flags 467 bool mHasBeenCurrent; 468 bool mContextLost; 469 GLenum mResetStatus; 470 GLenum mResetStrategy; 471 bool mRobustAccess; 472 473 BindingPointer<ProgramBinary> mCurrentProgramBinary; 474 Framebuffer *mBoundDrawFramebuffer; 475 476 int mMajorShaderModel; 477 float mMaximumPointSize; 478 bool mSupportsVertexTexture; 479 bool mSupportsNonPower2Texture; 480 bool mSupportsInstancing; 481 int mMaxViewportDimension; 482 int mMaxRenderbufferDimension; 483 int mMaxTextureDimension; 484 int mMaxCubeTextureDimension; 485 int mMaxTextureLevel; 486 float mMaxTextureAnisotropy; 487 bool mSupportsEventQueries; 488 bool mSupportsOcclusionQueries; 489 bool mSupportsBGRATextures; 490 bool mSupportsDXT1Textures; 491 bool mSupportsDXT3Textures; 492 bool mSupportsDXT5Textures; 493 bool mSupportsFloat32Textures; 494 bool mSupportsFloat32LinearFilter; 495 bool mSupportsFloat32RenderableTextures; 496 bool mSupportsFloat16Textures; 497 bool mSupportsFloat16LinearFilter; 498 bool mSupportsFloat16RenderableTextures; 499 bool mSupportsLuminanceTextures; 500 bool mSupportsLuminanceAlphaTextures; 501 bool mSupportsDepthTextures; 502 bool mSupports32bitIndices; 503 bool mSupportsTextureFilterAnisotropy; 504 int mNumCompressedTextureFormats; 505 506 ResourceManager *mResourceManager; 507 }; 508 } 509 510 #endif // INCLUDE_CONTEXT_H_ 511