michael@0: // michael@0: // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // Context.h: Defines the gl::Context class, managing all GL state and performing michael@0: // rendering operations. It is the GLES2 specific implementation of EGLContext. michael@0: michael@0: #ifndef LIBGLESV2_CONTEXT_H_ michael@0: #define LIBGLESV2_CONTEXT_H_ michael@0: michael@0: #define GL_APICALL michael@0: #include michael@0: #include michael@0: #define EGLAPI michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #ifdef _MSC_VER michael@0: #include michael@0: #else michael@0: #include michael@0: #endif michael@0: michael@0: #include "common/angleutils.h" michael@0: #include "common/RefCountObject.h" michael@0: #include "libGLESv2/HandleAllocator.h" michael@0: #include "libGLESv2/angletypes.h" michael@0: #include "libGLESv2/Constants.h" michael@0: michael@0: namespace rx michael@0: { michael@0: class Renderer; michael@0: } michael@0: michael@0: namespace egl michael@0: { michael@0: class Display; michael@0: class Surface; michael@0: } michael@0: michael@0: namespace gl michael@0: { michael@0: class Shader; michael@0: class Program; michael@0: class ProgramBinary; michael@0: class Texture; michael@0: class Texture2D; michael@0: class TextureCubeMap; michael@0: class Framebuffer; michael@0: class Renderbuffer; michael@0: class RenderbufferStorage; michael@0: class Colorbuffer; michael@0: class Depthbuffer; michael@0: class Stencilbuffer; michael@0: class DepthStencilbuffer; michael@0: class Fence; michael@0: class Query; michael@0: class ResourceManager; michael@0: class Buffer; michael@0: michael@0: enum QueryType michael@0: { michael@0: QUERY_ANY_SAMPLES_PASSED, michael@0: QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE, michael@0: michael@0: QUERY_TYPE_COUNT michael@0: }; michael@0: michael@0: // Helper structure describing a single vertex attribute michael@0: class VertexAttribute michael@0: { michael@0: public: michael@0: VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mPointer(NULL), mArrayEnabled(false), mDivisor(0) michael@0: { michael@0: mCurrentValue[0] = 0.0f; michael@0: mCurrentValue[1] = 0.0f; michael@0: mCurrentValue[2] = 0.0f; michael@0: mCurrentValue[3] = 1.0f; michael@0: } michael@0: michael@0: int typeSize() const michael@0: { michael@0: switch (mType) michael@0: { michael@0: case GL_BYTE: return mSize * sizeof(GLbyte); michael@0: case GL_UNSIGNED_BYTE: return mSize * sizeof(GLubyte); michael@0: case GL_SHORT: return mSize * sizeof(GLshort); michael@0: case GL_UNSIGNED_SHORT: return mSize * sizeof(GLushort); michael@0: case GL_FIXED: return mSize * sizeof(GLfixed); michael@0: case GL_FLOAT: return mSize * sizeof(GLfloat); michael@0: default: UNREACHABLE(); return mSize * sizeof(GLfloat); michael@0: } michael@0: } michael@0: michael@0: GLsizei stride() const michael@0: { michael@0: return mStride ? mStride : typeSize(); michael@0: } michael@0: michael@0: // From glVertexAttribPointer michael@0: GLenum mType; michael@0: GLint mSize; michael@0: bool mNormalized; michael@0: GLsizei mStride; // 0 means natural stride michael@0: michael@0: union michael@0: { michael@0: const void *mPointer; michael@0: intptr_t mOffset; michael@0: }; michael@0: michael@0: BindingPointer mBoundBuffer; // Captured when glVertexAttribPointer is called. michael@0: michael@0: bool mArrayEnabled; // From glEnable/DisableVertexAttribArray michael@0: float mCurrentValue[4]; // From glVertexAttrib michael@0: unsigned int mDivisor; michael@0: }; michael@0: michael@0: // Helper structure to store all raw state michael@0: struct State michael@0: { michael@0: Color colorClearValue; michael@0: GLclampf depthClearValue; michael@0: int stencilClearValue; michael@0: michael@0: RasterizerState rasterizer; michael@0: bool scissorTest; michael@0: Rectangle scissor; michael@0: michael@0: BlendState blend; michael@0: Color blendColor; michael@0: bool sampleCoverage; michael@0: GLclampf sampleCoverageValue; michael@0: bool sampleCoverageInvert; michael@0: michael@0: DepthStencilState depthStencil; michael@0: GLint stencilRef; michael@0: GLint stencilBackRef; michael@0: michael@0: GLfloat lineWidth; michael@0: michael@0: GLenum generateMipmapHint; michael@0: GLenum fragmentShaderDerivativeHint; michael@0: michael@0: Rectangle viewport; michael@0: float zNear; michael@0: float zFar; michael@0: michael@0: unsigned int activeSampler; // Active texture unit selector - GL_TEXTURE0 michael@0: BindingPointer arrayBuffer; michael@0: BindingPointer elementArrayBuffer; michael@0: GLuint readFramebuffer; michael@0: GLuint drawFramebuffer; michael@0: BindingPointer renderbuffer; michael@0: GLuint currentProgram; michael@0: michael@0: VertexAttribute vertexAttribute[MAX_VERTEX_ATTRIBS]; michael@0: BindingPointer samplerTexture[TEXTURE_TYPE_COUNT][IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS]; michael@0: BindingPointer activeQuery[QUERY_TYPE_COUNT]; michael@0: michael@0: GLint unpackAlignment; michael@0: GLint packAlignment; michael@0: bool packReverseRowOrder; michael@0: }; michael@0: michael@0: class Context michael@0: { michael@0: public: michael@0: Context(const gl::Context *shareContext, rx::Renderer *renderer, bool notifyResets, bool robustAccess); michael@0: michael@0: ~Context(); michael@0: michael@0: void makeCurrent(egl::Surface *surface); michael@0: michael@0: virtual void markContextLost(); michael@0: bool isContextLost(); michael@0: michael@0: // State manipulation michael@0: void setClearColor(float red, float green, float blue, float alpha); michael@0: michael@0: void setClearDepth(float depth); michael@0: michael@0: void setClearStencil(int stencil); michael@0: michael@0: void setCullFace(bool enabled); michael@0: bool isCullFaceEnabled() const; michael@0: michael@0: void setCullMode(GLenum mode); michael@0: michael@0: void setFrontFace(GLenum front); michael@0: michael@0: void setDepthTest(bool enabled); michael@0: bool isDepthTestEnabled() const; michael@0: michael@0: void setDepthFunc(GLenum depthFunc); michael@0: michael@0: void setDepthRange(float zNear, float zFar); michael@0: michael@0: void setBlend(bool enabled); michael@0: bool isBlendEnabled() const; michael@0: michael@0: void setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha); michael@0: void setBlendColor(float red, float green, float blue, float alpha); michael@0: void setBlendEquation(GLenum rgbEquation, GLenum alphaEquation); michael@0: michael@0: void setStencilTest(bool enabled); michael@0: bool isStencilTestEnabled() const; michael@0: michael@0: void setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask); michael@0: void setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask); michael@0: void setStencilWritemask(GLuint stencilWritemask); michael@0: void setStencilBackWritemask(GLuint stencilBackWritemask); michael@0: void setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass); michael@0: void setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass); michael@0: michael@0: void setPolygonOffsetFill(bool enabled); michael@0: bool isPolygonOffsetFillEnabled() const; michael@0: michael@0: void setPolygonOffsetParams(GLfloat factor, GLfloat units); michael@0: michael@0: void setSampleAlphaToCoverage(bool enabled); michael@0: bool isSampleAlphaToCoverageEnabled() const; michael@0: michael@0: void setSampleCoverage(bool enabled); michael@0: bool isSampleCoverageEnabled() const; michael@0: michael@0: void setSampleCoverageParams(GLclampf value, bool invert); michael@0: michael@0: void setScissorTest(bool enabled); michael@0: bool isScissorTestEnabled() const; michael@0: michael@0: void setDither(bool enabled); michael@0: bool isDitherEnabled() const; michael@0: michael@0: void setLineWidth(GLfloat width); michael@0: michael@0: void setGenerateMipmapHint(GLenum hint); michael@0: void setFragmentShaderDerivativeHint(GLenum hint); michael@0: michael@0: void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height); michael@0: michael@0: void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height); michael@0: michael@0: void setColorMask(bool red, bool green, bool blue, bool alpha); michael@0: void setDepthMask(bool mask); michael@0: michael@0: void setActiveSampler(unsigned int active); michael@0: michael@0: GLuint getReadFramebufferHandle() const; michael@0: GLuint getDrawFramebufferHandle() const; michael@0: GLuint getRenderbufferHandle() const; michael@0: michael@0: GLuint getArrayBufferHandle() const; michael@0: michael@0: GLuint getActiveQuery(GLenum target) const; michael@0: michael@0: void setEnableVertexAttribArray(unsigned int attribNum, bool enabled); michael@0: const VertexAttribute &getVertexAttribState(unsigned int attribNum); michael@0: void setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, michael@0: bool normalized, GLsizei stride, const void *pointer); michael@0: const void *getVertexAttribPointer(unsigned int attribNum) const; michael@0: michael@0: void setUnpackAlignment(GLint alignment); michael@0: GLint getUnpackAlignment() const; michael@0: michael@0: void setPackAlignment(GLint alignment); michael@0: GLint getPackAlignment() const; michael@0: michael@0: void setPackReverseRowOrder(bool reverseRowOrder); michael@0: bool getPackReverseRowOrder() const; michael@0: michael@0: // These create and destroy methods are merely pass-throughs to michael@0: // ResourceManager, which owns these object types michael@0: GLuint createBuffer(); michael@0: GLuint createShader(GLenum type); michael@0: GLuint createProgram(); michael@0: GLuint createTexture(); michael@0: GLuint createRenderbuffer(); michael@0: michael@0: void deleteBuffer(GLuint buffer); michael@0: void deleteShader(GLuint shader); michael@0: void deleteProgram(GLuint program); michael@0: void deleteTexture(GLuint texture); michael@0: void deleteRenderbuffer(GLuint renderbuffer); michael@0: michael@0: // Framebuffers are owned by the Context, so these methods do not pass through michael@0: GLuint createFramebuffer(); michael@0: void deleteFramebuffer(GLuint framebuffer); michael@0: michael@0: // Fences are owned by the Context. michael@0: GLuint createFence(); michael@0: void deleteFence(GLuint fence); michael@0: michael@0: // Queries are owned by the Context; michael@0: GLuint createQuery(); michael@0: void deleteQuery(GLuint query); michael@0: michael@0: void bindArrayBuffer(GLuint buffer); michael@0: void bindElementArrayBuffer(GLuint buffer); michael@0: void bindTexture2D(GLuint texture); michael@0: void bindTextureCubeMap(GLuint texture); michael@0: void bindReadFramebuffer(GLuint framebuffer); michael@0: void bindDrawFramebuffer(GLuint framebuffer); michael@0: void bindRenderbuffer(GLuint renderbuffer); michael@0: void useProgram(GLuint program); michael@0: void linkProgram(GLuint program); michael@0: void setProgramBinary(GLuint program, const void *binary, GLint length); michael@0: michael@0: void beginQuery(GLenum target, GLuint query); michael@0: void endQuery(GLenum target); michael@0: michael@0: void setFramebufferZero(Framebuffer *framebuffer); michael@0: michael@0: void setRenderbufferStorage(GLsizei width, GLsizei height, GLenum internalformat, GLsizei samples); michael@0: michael@0: void setVertexAttrib(GLuint index, const GLfloat *values); michael@0: void setVertexAttribDivisor(GLuint index, GLuint divisor); michael@0: michael@0: Buffer *getBuffer(GLuint handle); michael@0: Fence *getFence(GLuint handle); michael@0: Shader *getShader(GLuint handle); michael@0: Program *getProgram(GLuint handle); michael@0: Texture *getTexture(GLuint handle); michael@0: Framebuffer *getFramebuffer(GLuint handle); michael@0: Renderbuffer *getRenderbuffer(GLuint handle); michael@0: Query *getQuery(GLuint handle, bool create, GLenum type); michael@0: michael@0: Buffer *getArrayBuffer(); michael@0: Buffer *getElementArrayBuffer(); michael@0: ProgramBinary *getCurrentProgramBinary(); michael@0: Texture2D *getTexture2D(); michael@0: TextureCubeMap *getTextureCubeMap(); michael@0: Texture *getSamplerTexture(unsigned int sampler, TextureType type); michael@0: Framebuffer *getReadFramebuffer(); michael@0: Framebuffer *getDrawFramebuffer(); michael@0: michael@0: bool getFloatv(GLenum pname, GLfloat *params); michael@0: bool getIntegerv(GLenum pname, GLint *params); michael@0: bool getBooleanv(GLenum pname, GLboolean *params); michael@0: michael@0: bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams); michael@0: michael@0: void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels); michael@0: void clear(GLbitfield mask); michael@0: void drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances); michael@0: void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances); michael@0: void sync(bool block); // flush/finish michael@0: michael@0: void recordInvalidEnum(); michael@0: void recordInvalidValue(); michael@0: void recordInvalidOperation(); michael@0: void recordOutOfMemory(); michael@0: void recordInvalidFramebufferOperation(); michael@0: michael@0: GLenum getError(); michael@0: GLenum getResetStatus(); michael@0: virtual bool isResetNotificationEnabled(); michael@0: michael@0: int getMajorShaderModel() const; michael@0: float getMaximumPointSize() const; michael@0: unsigned int getMaximumCombinedTextureImageUnits() const; michael@0: int getMaximumRenderbufferDimension() const; michael@0: int getMaximumTextureDimension() const; michael@0: int getMaximumCubeTextureDimension() const; michael@0: int getMaximumTextureLevel() const; michael@0: unsigned int getMaximumRenderTargets() const; michael@0: GLsizei getMaxSupportedSamples() const; michael@0: const char *getExtensionString() const; michael@0: const char *getRendererString() const; michael@0: bool supportsEventQueries() const; michael@0: bool supportsOcclusionQueries() const; michael@0: bool supportsBGRATextures() const; michael@0: bool supportsDXT1Textures() const; michael@0: bool supportsDXT3Textures() const; michael@0: bool supportsDXT5Textures() const; michael@0: bool supportsFloat32Textures() const; michael@0: bool supportsFloat32LinearFilter() const; michael@0: bool supportsFloat32RenderableTextures() const; michael@0: bool supportsFloat16Textures() const; michael@0: bool supportsFloat16LinearFilter() const; michael@0: bool supportsFloat16RenderableTextures() const; michael@0: bool supportsLuminanceTextures() const; michael@0: bool supportsLuminanceAlphaTextures() const; michael@0: bool supportsDepthTextures() const; michael@0: bool supports32bitIndices() const; michael@0: bool supportsNonPower2Texture() const; michael@0: bool supportsInstancing() const; michael@0: bool supportsTextureFilterAnisotropy() const; michael@0: michael@0: bool getCurrentReadFormatType(GLenum *format, GLenum *type); michael@0: michael@0: float getTextureMaxAnisotropy() const; michael@0: michael@0: void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, michael@0: GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, michael@0: GLbitfield mask); michael@0: michael@0: private: michael@0: DISALLOW_COPY_AND_ASSIGN(Context); michael@0: michael@0: bool applyRenderTarget(GLenum drawMode, bool ignoreViewport); michael@0: void applyState(GLenum drawMode); michael@0: void applyShaders(); michael@0: void applyTextures(); michael@0: void applyTextures(SamplerType type); michael@0: michael@0: void detachBuffer(GLuint buffer); michael@0: void detachTexture(GLuint texture); michael@0: void detachFramebuffer(GLuint framebuffer); michael@0: void detachRenderbuffer(GLuint renderbuffer); michael@0: michael@0: Texture *getIncompleteTexture(TextureType type); michael@0: michael@0: bool skipDraw(GLenum drawMode); michael@0: michael@0: void initExtensionString(); michael@0: void initRendererString(); michael@0: michael@0: rx::Renderer *const mRenderer; michael@0: michael@0: State mState; michael@0: michael@0: BindingPointer mTexture2DZero; michael@0: BindingPointer mTextureCubeMapZero; michael@0: michael@0: #ifndef HASH_MAP michael@0: # ifdef _MSC_VER michael@0: # define HASH_MAP stdext::hash_map michael@0: # else michael@0: # define HASH_MAP std::unordered_map michael@0: # endif michael@0: #endif michael@0: michael@0: typedef HASH_MAP FramebufferMap; michael@0: FramebufferMap mFramebufferMap; michael@0: HandleAllocator mFramebufferHandleAllocator; michael@0: michael@0: typedef HASH_MAP FenceMap; michael@0: FenceMap mFenceMap; michael@0: HandleAllocator mFenceHandleAllocator; michael@0: michael@0: typedef HASH_MAP QueryMap; michael@0: QueryMap mQueryMap; michael@0: HandleAllocator mQueryHandleAllocator; michael@0: michael@0: const char *mExtensionString; michael@0: const char *mRendererString; michael@0: michael@0: BindingPointer mIncompleteTextures[TEXTURE_TYPE_COUNT]; michael@0: michael@0: // Recorded errors michael@0: bool mInvalidEnum; michael@0: bool mInvalidValue; michael@0: bool mInvalidOperation; michael@0: bool mOutOfMemory; michael@0: bool mInvalidFramebufferOperation; michael@0: michael@0: // Current/lost context flags michael@0: bool mHasBeenCurrent; michael@0: bool mContextLost; michael@0: GLenum mResetStatus; michael@0: GLenum mResetStrategy; michael@0: bool mRobustAccess; michael@0: michael@0: BindingPointer mCurrentProgramBinary; michael@0: Framebuffer *mBoundDrawFramebuffer; michael@0: michael@0: int mMajorShaderModel; michael@0: float mMaximumPointSize; michael@0: bool mSupportsVertexTexture; michael@0: bool mSupportsNonPower2Texture; michael@0: bool mSupportsInstancing; michael@0: int mMaxViewportDimension; michael@0: int mMaxRenderbufferDimension; michael@0: int mMaxTextureDimension; michael@0: int mMaxCubeTextureDimension; michael@0: int mMaxTextureLevel; michael@0: float mMaxTextureAnisotropy; michael@0: bool mSupportsEventQueries; michael@0: bool mSupportsOcclusionQueries; michael@0: bool mSupportsBGRATextures; michael@0: bool mSupportsDXT1Textures; michael@0: bool mSupportsDXT3Textures; michael@0: bool mSupportsDXT5Textures; michael@0: bool mSupportsFloat32Textures; michael@0: bool mSupportsFloat32LinearFilter; michael@0: bool mSupportsFloat32RenderableTextures; michael@0: bool mSupportsFloat16Textures; michael@0: bool mSupportsFloat16LinearFilter; michael@0: bool mSupportsFloat16RenderableTextures; michael@0: bool mSupportsLuminanceTextures; michael@0: bool mSupportsLuminanceAlphaTextures; michael@0: bool mSupportsDepthTextures; michael@0: bool mSupports32bitIndices; michael@0: bool mSupportsTextureFilterAnisotropy; michael@0: int mNumCompressedTextureFormats; michael@0: michael@0: ResourceManager *mResourceManager; michael@0: }; michael@0: } michael@0: michael@0: #endif // INCLUDE_CONTEXT_H_