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 #ifdef _MSC_VER 22 #include <hash_map> 23 #else 24 #include <unordered_map> 25 #endif 26 27 #include "common/angleutils.h" 28 #include "common/RefCountObject.h" 29 #include "libGLESv2/HandleAllocator.h" 30 #include "libGLESv2/angletypes.h" 31 #include "libGLESv2/Constants.h" 32 33 namespace rx 34 { 35 class Renderer; 36 } 37 38 namespace egl 39 { 40 class Display; 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 private: 402 DISALLOW_COPY_AND_ASSIGN(Context); 403 404 bool applyRenderTarget(GLenum drawMode, bool ignoreViewport); 405 void applyState(GLenum drawMode); 406 void applyShaders(); 407 void applyTextures(); 408 void applyTextures(SamplerType type); 409 410 void detachBuffer(GLuint buffer); 411 void detachTexture(GLuint texture); 412 void detachFramebuffer(GLuint framebuffer); 413 void detachRenderbuffer(GLuint renderbuffer); 414 415 Texture *getIncompleteTexture(TextureType type); 416 417 bool skipDraw(GLenum drawMode); 418 419 void initExtensionString(); 420 void initRendererString(); 421 422 rx::Renderer *const mRenderer; 423 424 State mState; 425 426 BindingPointer<Texture2D> mTexture2DZero; 427 BindingPointer<TextureCubeMap> mTextureCubeMapZero; 428 429 #ifndef HASH_MAP 430 # ifdef _MSC_VER 431 # define HASH_MAP stdext::hash_map 432 # else 433 # define HASH_MAP std::unordered_map 434 # endif 435 #endif 436 437 typedef HASH_MAP<GLuint, Framebuffer*> FramebufferMap; 438 FramebufferMap mFramebufferMap; 439 HandleAllocator mFramebufferHandleAllocator; 440 441 typedef HASH_MAP<GLuint, Fence*> FenceMap; 442 FenceMap mFenceMap; 443 HandleAllocator mFenceHandleAllocator; 444 445 typedef HASH_MAP<GLuint, Query*> QueryMap; 446 QueryMap mQueryMap; 447 HandleAllocator mQueryHandleAllocator; 448 449 const char *mExtensionString; 450 const char *mRendererString; 451 452 BindingPointer<Texture> mIncompleteTextures[TEXTURE_TYPE_COUNT]; 453 454 // Recorded errors 455 bool mInvalidEnum; 456 bool mInvalidValue; 457 bool mInvalidOperation; 458 bool mOutOfMemory; 459 bool mInvalidFramebufferOperation; 460 461 // Current/lost context flags 462 bool mHasBeenCurrent; 463 bool mContextLost; 464 GLenum mResetStatus; 465 GLenum mResetStrategy; 466 bool mRobustAccess; 467 468 BindingPointer<ProgramBinary> mCurrentProgramBinary; 469 Framebuffer *mBoundDrawFramebuffer; 470 471 int mMajorShaderModel; 472 float mMaximumPointSize; 473 bool mSupportsVertexTexture; 474 bool mSupportsNonPower2Texture; 475 bool mSupportsInstancing; 476 int mMaxViewportDimension; 477 int mMaxRenderbufferDimension; 478 int mMaxTextureDimension; 479 int mMaxCubeTextureDimension; 480 int mMaxTextureLevel; 481 float mMaxTextureAnisotropy; 482 bool mSupportsEventQueries; 483 bool mSupportsOcclusionQueries; 484 bool mSupportsBGRATextures; 485 bool mSupportsDXT1Textures; 486 bool mSupportsDXT3Textures; 487 bool mSupportsDXT5Textures; 488 bool mSupportsFloat32Textures; 489 bool mSupportsFloat32LinearFilter; 490 bool mSupportsFloat32RenderableTextures; 491 bool mSupportsFloat16Textures; 492 bool mSupportsFloat16LinearFilter; 493 bool mSupportsFloat16RenderableTextures; 494 bool mSupportsLuminanceTextures; 495 bool mSupportsLuminanceAlphaTextures; 496 bool mSupportsDepthTextures; 497 bool mSupports32bitIndices; 498 bool mSupportsTextureFilterAnisotropy; 499 int mNumCompressedTextureFormats; 500 501 ResourceManager *mResourceManager; 502 }; 503 } 504 505 #endif // INCLUDE_CONTEXT_H_ 506