1 // 2 // Copyright (c) 2002-2010 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 #include <d3d9.h> 19 20 #include <map> 21 22 #include "common/angleutils.h" 23 #include "libGLESv2/ResourceManager.h" 24 #include "libGLESv2/RefCountObject.h" 25 26 namespace egl 27 { 28 class Display; 29 class Surface; 30 class Config; 31 } 32 33 namespace gl 34 { 35 struct TranslatedAttribute; 36 struct TranslatedIndexData; 37 38 class Buffer; 39 class Shader; 40 class Program; 41 class Texture; 42 class Texture2D; 43 class TextureCubeMap; 44 class Framebuffer; 45 class Renderbuffer; 46 class RenderbufferStorage; 47 class Colorbuffer; 48 class Depthbuffer; 49 class Stencilbuffer; 50 class DepthStencilbuffer; 51 class VertexDataManager; 52 class IndexDataManager; 53 class Blit; 54 class Fence; 55 56 enum 57 { 58 MAX_VERTEX_ATTRIBS = 16, 59 MAX_VERTEX_UNIFORM_VECTORS = 256 - 2, // 256 is the minimum for SM2, and in practice the maximum for DX9. Reserve space for dx_HalfPixelSize and dx_DepthRange. 60 MAX_VARYING_VECTORS_SM2 = 8, 61 MAX_VARYING_VECTORS_SM3 = 10, 62 MAX_COMBINED_TEXTURE_IMAGE_UNITS = 16, 63 MAX_VERTEX_TEXTURE_IMAGE_UNITS = 0, 64 MAX_TEXTURE_IMAGE_UNITS = 16, 65 MAX_FRAGMENT_UNIFORM_VECTORS_SM2 = 32 - 3, // Reserve space for dx_Viewport, dx_Depth, and dx_DepthRange. dx_PointOrLines and dx_FrontCCW use separate bool registers. 66 MAX_FRAGMENT_UNIFORM_VECTORS_SM3 = 224 - 3, 67 MAX_DRAW_BUFFERS = 1, 68 69 IMPLEMENTATION_COLOR_READ_FORMAT = GL_RGB, 70 IMPLEMENTATION_COLOR_READ_TYPE = GL_UNSIGNED_SHORT_5_6_5 71 }; 72 73 const float ALIASED_LINE_WIDTH_RANGE_MIN = 1.0f; 74 const float ALIASED_LINE_WIDTH_RANGE_MAX = 1.0f; 75 const float ALIASED_POINT_SIZE_RANGE_MIN = 1.0f; 76 const float ALIASED_POINT_SIZE_RANGE_MAX_SM2 = 1.0f; 77 const float ALIASED_POINT_SIZE_RANGE_MAX_SM3 = 64.0f; 78 79 struct Color 80 { 81 float red; 82 float green; 83 float blue; 84 float alpha; 85 }; 86 87 // Helper structure describing a single vertex attribute 88 class VertexAttribute 89 { 90 public: 91 VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mPointer(NULL), mArrayEnabled(false) 92 { 93 mCurrentValue[0] = 0.0f; 94 mCurrentValue[1] = 0.0f; 95 mCurrentValue[2] = 0.0f; 96 mCurrentValue[3] = 1.0f; 97 } 98 99 int typeSize() const 100 { 101 switch (mType) 102 { 103 case GL_BYTE: return mSize * sizeof(GLbyte); 104 case GL_UNSIGNED_BYTE: return mSize * sizeof(GLubyte); 105 case GL_SHORT: return mSize * sizeof(GLshort); 106 case GL_UNSIGNED_SHORT: return mSize * sizeof(GLushort); 107 case GL_FIXED: return mSize * sizeof(GLfixed); 108 case GL_FLOAT: return mSize * sizeof(GLfloat); 109 default: UNREACHABLE(); return mSize * sizeof(GLfloat); 110 } 111 } 112 113 GLsizei stride() const 114 { 115 return mStride ? mStride : typeSize(); 116 } 117 118 // From glVertexAttribPointer 119 GLenum mType; 120 GLint mSize; 121 bool mNormalized; 122 GLsizei mStride; // 0 means natural stride 123 124 union 125 { 126 const void *mPointer; 127 intptr_t mOffset; 128 }; 129 130 BindingPointer<Buffer> mBoundBuffer; // Captured when glVertexAttribPointer is called. 131 132 bool mArrayEnabled; // From glEnable/DisableVertexAttribArray 133 float mCurrentValue[4]; // From glVertexAttrib 134 }; 135 136 typedef VertexAttribute VertexAttributeArray[MAX_VERTEX_ATTRIBS]; 137 138 // Helper structure to store all raw state 139 struct State 140 { 141 Color colorClearValue; 142 GLclampf depthClearValue; 143 int stencilClearValue; 144 145 bool cullFace; 146 GLenum cullMode; 147 GLenum frontFace; 148 bool depthTest; 149 GLenum depthFunc; 150 bool blend; 151 GLenum sourceBlendRGB; 152 GLenum destBlendRGB; 153 GLenum sourceBlendAlpha; 154 GLenum destBlendAlpha; 155 GLenum blendEquationRGB; 156 GLenum blendEquationAlpha; 157 Color blendColor; 158 bool stencilTest; 159 GLenum stencilFunc; 160 GLint stencilRef; 161 GLuint stencilMask; 162 GLenum stencilFail; 163 GLenum stencilPassDepthFail; 164 GLenum stencilPassDepthPass; 165 GLuint stencilWritemask; 166 GLenum stencilBackFunc; 167 GLint stencilBackRef; 168 GLuint stencilBackMask; 169 GLenum stencilBackFail; 170 GLenum stencilBackPassDepthFail; 171 GLenum stencilBackPassDepthPass; 172 GLuint stencilBackWritemask; 173 bool polygonOffsetFill; 174 GLfloat polygonOffsetFactor; 175 GLfloat polygonOffsetUnits; 176 bool sampleAlphaToCoverage; 177 bool sampleCoverage; 178 GLclampf sampleCoverageValue; 179 bool sampleCoverageInvert; 180 bool scissorTest; 181 bool dither; 182 183 GLfloat lineWidth; 184 185 GLenum generateMipmapHint; 186 GLenum fragmentShaderDerivativeHint; 187 188 GLint viewportX; 189 GLint viewportY; 190 GLsizei viewportWidth; 191 GLsizei viewportHeight; 192 float zNear; 193 float zFar; 194 195 GLint scissorX; 196 GLint scissorY; 197 GLsizei scissorWidth; 198 GLsizei scissorHeight; 199 200 bool colorMaskRed; 201 bool colorMaskGreen; 202 bool colorMaskBlue; 203 bool colorMaskAlpha; 204 bool depthMask; 205 206 int activeSampler; // Active texture unit selector - GL_TEXTURE0 207 BindingPointer<Buffer> arrayBuffer; 208 BindingPointer<Buffer> elementArrayBuffer; 209 GLuint readFramebuffer; 210 GLuint drawFramebuffer; 211 BindingPointer<Renderbuffer> renderbuffer; 212 GLuint currentProgram; 213 214 VertexAttribute vertexAttribute[MAX_VERTEX_ATTRIBS]; 215 BindingPointer<Texture> samplerTexture[SAMPLER_TYPE_COUNT][MAX_TEXTURE_IMAGE_UNITS]; 216 217 GLint unpackAlignment; 218 GLint packAlignment; 219 }; 220 221 class Context 222 { 223 public: 224 Context(const egl::Config *config, const gl::Context *shareContext); 225 226 ~Context(); 227 228 void makeCurrent(egl::Display *display, egl::Surface *surface); 229 230 void markAllStateDirty(); 231 232 // State manipulation 233 void setClearColor(float red, float green, float blue, float alpha); 234 235 void setClearDepth(float depth); 236 237 void setClearStencil(int stencil); 238 239 void setCullFace(bool enabled); 240 bool isCullFaceEnabled() const; 241 242 void setCullMode(GLenum mode); 243 244 void setFrontFace(GLenum front); 245 246 void setDepthTest(bool enabled); 247 bool isDepthTestEnabled() const; 248 249 void setDepthFunc(GLenum depthFunc); 250 251 void setDepthRange(float zNear, float zFar); 252 253 void setBlend(bool enabled); 254 bool isBlendEnabled() const; 255 256 void setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha); 257 void setBlendColor(float red, float green, float blue, float alpha); 258 void setBlendEquation(GLenum rgbEquation, GLenum alphaEquation); 259 260 void setStencilTest(bool enabled); 261 bool isStencilTestEnabled() const; 262 263 void setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask); 264 void setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask); 265 void setStencilWritemask(GLuint stencilWritemask); 266 void setStencilBackWritemask(GLuint stencilBackWritemask); 267 void setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass); 268 void setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass); 269 270 void setPolygonOffsetFill(bool enabled); 271 bool isPolygonOffsetFillEnabled() const; 272 273 void setPolygonOffsetParams(GLfloat factor, GLfloat units); 274 275 void setSampleAlphaToCoverage(bool enabled); 276 bool isSampleAlphaToCoverageEnabled() const; 277 278 void setSampleCoverage(bool enabled); 279 bool isSampleCoverageEnabled() const; 280 281 void setSampleCoverageParams(GLclampf value, bool invert); 282 283 void setScissorTest(bool enabled); 284 bool isScissorTestEnabled() const; 285 286 void setDither(bool enabled); 287 bool isDitherEnabled() const; 288 289 void setLineWidth(GLfloat width); 290 291 void setGenerateMipmapHint(GLenum hint); 292 void setFragmentShaderDerivativeHint(GLenum hint); 293 294 void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height); 295 296 void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height); 297 298 void setColorMask(bool red, bool green, bool blue, bool alpha); 299 void setDepthMask(bool mask); 300 301 void setActiveSampler(int active); 302 303 GLuint getReadFramebufferHandle() const; 304 GLuint getDrawFramebufferHandle() const; 305 GLuint getRenderbufferHandle() const; 306 307 GLuint getArrayBufferHandle() const; 308 309 void setEnableVertexAttribArray(unsigned int attribNum, bool enabled); 310 const VertexAttribute &getVertexAttribState(unsigned int attribNum); 311 void setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, 312 bool normalized, GLsizei stride, const void *pointer); 313 const void *getVertexAttribPointer(unsigned int attribNum) const; 314 315 const VertexAttributeArray &getVertexAttributes(); 316 317 void setUnpackAlignment(GLint alignment); 318 GLint getUnpackAlignment() const; 319 320 void setPackAlignment(GLint alignment); 321 GLint getPackAlignment() const; 322 323 // These create and destroy methods are merely pass-throughs to 324 // ResourceManager, which owns these object types 325 GLuint createBuffer(); 326 GLuint createShader(GLenum type); 327 GLuint createProgram(); 328 GLuint createTexture(); 329 GLuint createRenderbuffer(); 330 331 void deleteBuffer(GLuint buffer); 332 void deleteShader(GLuint shader); 333 void deleteProgram(GLuint program); 334 void deleteTexture(GLuint texture); 335 void deleteRenderbuffer(GLuint renderbuffer); 336 337 // Framebuffers are owned by the Context, so these methods do not pass through 338 GLuint createFramebuffer(); 339 void deleteFramebuffer(GLuint framebuffer); 340 341 // Fences are owned by the Context. 342 GLuint createFence(); 343 void deleteFence(GLuint fence); 344 345 void bindArrayBuffer(GLuint buffer); 346 void bindElementArrayBuffer(GLuint buffer); 347 void bindTexture2D(GLuint texture); 348 void bindTextureCubeMap(GLuint texture); 349 void bindReadFramebuffer(GLuint framebuffer); 350 void bindDrawFramebuffer(GLuint framebuffer); 351 void bindRenderbuffer(GLuint renderbuffer); 352 void useProgram(GLuint program); 353 354 void setFramebufferZero(Framebuffer *framebuffer); 355 356 void setRenderbufferStorage(RenderbufferStorage *renderbuffer); 357 358 void setVertexAttrib(GLuint index, const GLfloat *values); 359 360 Buffer *getBuffer(GLuint handle); 361 Fence *getFence(GLuint handle); 362 Shader *getShader(GLuint handle); 363 Program *getProgram(GLuint handle); 364 Texture *getTexture(GLuint handle); 365 Framebuffer *getFramebuffer(GLuint handle); 366 Renderbuffer *getRenderbuffer(GLuint handle); 367 368 Buffer *getArrayBuffer(); 369 Buffer *getElementArrayBuffer(); 370 Program *getCurrentProgram(); 371 Texture2D *getTexture2D(); 372 TextureCubeMap *getTextureCubeMap(); 373 Texture *getSamplerTexture(unsigned int sampler, SamplerType type); 374 Framebuffer *getReadFramebuffer(); 375 Framebuffer *getDrawFramebuffer(); 376 377 bool getFloatv(GLenum pname, GLfloat *params); 378 bool getIntegerv(GLenum pname, GLint *params); 379 bool getBooleanv(GLenum pname, GLboolean *params); 380 381 bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams); 382 383 bool applyRenderTarget(bool ignoreViewport); 384 void applyState(GLenum drawMode); 385 GLenum applyVertexBuffer(GLint first, GLsizei count); 386 GLenum applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo); 387 void applyShaders(); 388 void applyTextures(); 389 390 void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels); 391 void clear(GLbitfield mask); 392 void drawArrays(GLenum mode, GLint first, GLsizei count); 393 void drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices); 394 void finish(); 395 void flush(); 396 397 // Draw the last segment of a line loop 398 void drawClosingLine(unsigned int first, unsigned int last); 399 void drawClosingLine(GLsizei count, GLenum type, const void *indices); 400 401 void recordInvalidEnum(); 402 void recordInvalidValue(); 403 void recordInvalidOperation(); 404 void recordOutOfMemory(); 405 void recordInvalidFramebufferOperation(); 406 407 GLenum getError(); 408 409 bool supportsShaderModel3() const; 410 int getMaximumVaryingVectors() const; 411 int getMaximumFragmentUniformVectors() const; 412 int getMaximumRenderbufferDimension() const; 413 int getMaximumTextureDimension() const; 414 int getMaximumCubeTextureDimension() const; 415 int getMaximumTextureLevel() const; 416 GLsizei getMaxSupportedSamples() const; 417 int getNearestSupportedSamples(D3DFORMAT format, int requested) const; 418 const char *getExtensionString() const; 419 bool supportsEventQueries() const; 420 bool supportsCompressedTextures() const; 421 bool supportsFloatTextures() const; 422 bool supportsFloatLinearFilter() const; 423 bool supportsFloatRenderableTextures() const; 424 bool supportsHalfFloatTextures() const; 425 bool supportsHalfFloatLinearFilter() const; 426 bool supportsHalfFloatRenderableTextures() const; 427 bool supportsLuminanceTextures() const; 428 bool supportsLuminanceAlphaTextures() const; 429 bool supports32bitIndices() const; 430 431 void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, 432 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, 433 GLbitfield mask); 434 435 Blit *getBlitter() { return mBlit; } 436 437 const D3DCAPS9 &getDeviceCaps() { return mDeviceCaps; } 438 439 private: 440 DISALLOW_COPY_AND_ASSIGN(Context); 441 442 void lookupAttributeMapping(TranslatedAttribute *attributes); 443 444 void detachBuffer(GLuint buffer); 445 void detachTexture(GLuint texture); 446 void detachFramebuffer(GLuint framebuffer); 447 void detachRenderbuffer(GLuint renderbuffer); 448 449 Texture *getIncompleteTexture(SamplerType type); 450 451 bool cullSkipsDraw(GLenum drawMode); 452 bool isTriangleMode(GLenum drawMode); 453 454 const egl::Config *const mConfig; 455 456 State mState; 457 458 BindingPointer<Texture2D> mTexture2DZero; 459 BindingPointer<TextureCubeMap> mTextureCubeMapZero; 460 461 typedef std::map<GLuint, Framebuffer*> FramebufferMap; 462 FramebufferMap mFramebufferMap; 463 464 typedef std::map<GLuint, Fence*> FenceMap; 465 FenceMap mFenceMap; 466 467 void initExtensionString(); 468 std::string mExtensionString; 469 470 VertexDataManager *mVertexDataManager; 471 IndexDataManager *mIndexDataManager; 472 473 Blit *mBlit; 474 475 BindingPointer<Texture> mIncompleteTextures[SAMPLER_TYPE_COUNT]; 476 477 // Recorded errors 478 bool mInvalidEnum; 479 bool mInvalidValue; 480 bool mInvalidOperation; 481 bool mOutOfMemory; 482 bool mInvalidFramebufferOperation; 483 484 bool mHasBeenCurrent; 485 486 unsigned int mAppliedProgram; 487 unsigned int mAppliedRenderTargetSerial; 488 unsigned int mAppliedDepthbufferSerial; 489 unsigned int mAppliedStencilbufferSerial; 490 bool mDepthStencilInitialized; 491 492 bool mSupportsShaderModel3; 493 int mMaxRenderbufferDimension; 494 int mMaxTextureDimension; 495 int mMaxCubeTextureDimension; 496 int mMaxTextureLevel; 497 std::map<D3DFORMAT, bool *> mMultiSampleSupport; 498 GLsizei mMaxSupportedSamples; 499 bool mSupportsEventQueries; 500 bool mSupportsCompressedTextures; 501 bool mSupportsFloatTextures; 502 bool mSupportsFloatLinearFilter; 503 bool mSupportsFloatRenderableTextures; 504 bool mSupportsHalfFloatTextures; 505 bool mSupportsHalfFloatLinearFilter; 506 bool mSupportsHalfFloatRenderableTextures; 507 bool mSupportsLuminanceTextures; 508 bool mSupportsLuminanceAlphaTextures; 509 bool mSupports32bitIndices; 510 511 // state caching flags 512 bool mClearStateDirty; 513 bool mCullStateDirty; 514 bool mDepthStateDirty; 515 bool mMaskStateDirty; 516 bool mPixelPackingStateDirty; 517 bool mBlendStateDirty; 518 bool mStencilStateDirty; 519 bool mPolygonOffsetStateDirty; 520 bool mScissorStateDirty; 521 bool mSampleStateDirty; 522 bool mFrontFaceDirty; 523 bool mDitherStateDirty; 524 525 IDirect3DStateBlock9 *mMaskedClearSavedState; 526 527 D3DCAPS9 mDeviceCaps; 528 529 ResourceManager *mResourceManager; 530 }; 531 } 532 533 extern "C" 534 { 535 // Exported functions for use by EGL 536 gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext); 537 void glDestroyContext(gl::Context *context); 538 void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface); 539 gl::Context *glGetCurrentContext(); 540 __eglMustCastToProperFunctionPointerType __stdcall glGetProcAddress(const char *procname); 541 } 542 543 #endif // INCLUDE_CONTEXT_H_ 544