1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/angle/src/libGLESv2/Context.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,505 @@ 1.4 +// 1.5 +// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. 1.6 +// Use of this source code is governed by a BSD-style license that can be 1.7 +// found in the LICENSE file. 1.8 +// 1.9 + 1.10 +// Context.h: Defines the gl::Context class, managing all GL state and performing 1.11 +// rendering operations. It is the GLES2 specific implementation of EGLContext. 1.12 + 1.13 +#ifndef LIBGLESV2_CONTEXT_H_ 1.14 +#define LIBGLESV2_CONTEXT_H_ 1.15 + 1.16 +#define GL_APICALL 1.17 +#include <GLES2/gl2.h> 1.18 +#include <GLES2/gl2ext.h> 1.19 +#define EGLAPI 1.20 +#include <EGL/egl.h> 1.21 + 1.22 +#include <string> 1.23 +#include <map> 1.24 +#ifdef _MSC_VER 1.25 +#include <hash_map> 1.26 +#else 1.27 +#include <unordered_map> 1.28 +#endif 1.29 + 1.30 +#include "common/angleutils.h" 1.31 +#include "common/RefCountObject.h" 1.32 +#include "libGLESv2/HandleAllocator.h" 1.33 +#include "libGLESv2/angletypes.h" 1.34 +#include "libGLESv2/Constants.h" 1.35 + 1.36 +namespace rx 1.37 +{ 1.38 +class Renderer; 1.39 +} 1.40 + 1.41 +namespace egl 1.42 +{ 1.43 +class Display; 1.44 +class Surface; 1.45 +} 1.46 + 1.47 +namespace gl 1.48 +{ 1.49 +class Shader; 1.50 +class Program; 1.51 +class ProgramBinary; 1.52 +class Texture; 1.53 +class Texture2D; 1.54 +class TextureCubeMap; 1.55 +class Framebuffer; 1.56 +class Renderbuffer; 1.57 +class RenderbufferStorage; 1.58 +class Colorbuffer; 1.59 +class Depthbuffer; 1.60 +class Stencilbuffer; 1.61 +class DepthStencilbuffer; 1.62 +class Fence; 1.63 +class Query; 1.64 +class ResourceManager; 1.65 +class Buffer; 1.66 + 1.67 +enum QueryType 1.68 +{ 1.69 + QUERY_ANY_SAMPLES_PASSED, 1.70 + QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE, 1.71 + 1.72 + QUERY_TYPE_COUNT 1.73 +}; 1.74 + 1.75 +// Helper structure describing a single vertex attribute 1.76 +class VertexAttribute 1.77 +{ 1.78 + public: 1.79 + VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mPointer(NULL), mArrayEnabled(false), mDivisor(0) 1.80 + { 1.81 + mCurrentValue[0] = 0.0f; 1.82 + mCurrentValue[1] = 0.0f; 1.83 + mCurrentValue[2] = 0.0f; 1.84 + mCurrentValue[3] = 1.0f; 1.85 + } 1.86 + 1.87 + int typeSize() const 1.88 + { 1.89 + switch (mType) 1.90 + { 1.91 + case GL_BYTE: return mSize * sizeof(GLbyte); 1.92 + case GL_UNSIGNED_BYTE: return mSize * sizeof(GLubyte); 1.93 + case GL_SHORT: return mSize * sizeof(GLshort); 1.94 + case GL_UNSIGNED_SHORT: return mSize * sizeof(GLushort); 1.95 + case GL_FIXED: return mSize * sizeof(GLfixed); 1.96 + case GL_FLOAT: return mSize * sizeof(GLfloat); 1.97 + default: UNREACHABLE(); return mSize * sizeof(GLfloat); 1.98 + } 1.99 + } 1.100 + 1.101 + GLsizei stride() const 1.102 + { 1.103 + return mStride ? mStride : typeSize(); 1.104 + } 1.105 + 1.106 + // From glVertexAttribPointer 1.107 + GLenum mType; 1.108 + GLint mSize; 1.109 + bool mNormalized; 1.110 + GLsizei mStride; // 0 means natural stride 1.111 + 1.112 + union 1.113 + { 1.114 + const void *mPointer; 1.115 + intptr_t mOffset; 1.116 + }; 1.117 + 1.118 + BindingPointer<Buffer> mBoundBuffer; // Captured when glVertexAttribPointer is called. 1.119 + 1.120 + bool mArrayEnabled; // From glEnable/DisableVertexAttribArray 1.121 + float mCurrentValue[4]; // From glVertexAttrib 1.122 + unsigned int mDivisor; 1.123 +}; 1.124 + 1.125 +// Helper structure to store all raw state 1.126 +struct State 1.127 +{ 1.128 + Color colorClearValue; 1.129 + GLclampf depthClearValue; 1.130 + int stencilClearValue; 1.131 + 1.132 + RasterizerState rasterizer; 1.133 + bool scissorTest; 1.134 + Rectangle scissor; 1.135 + 1.136 + BlendState blend; 1.137 + Color blendColor; 1.138 + bool sampleCoverage; 1.139 + GLclampf sampleCoverageValue; 1.140 + bool sampleCoverageInvert; 1.141 + 1.142 + DepthStencilState depthStencil; 1.143 + GLint stencilRef; 1.144 + GLint stencilBackRef; 1.145 + 1.146 + GLfloat lineWidth; 1.147 + 1.148 + GLenum generateMipmapHint; 1.149 + GLenum fragmentShaderDerivativeHint; 1.150 + 1.151 + Rectangle viewport; 1.152 + float zNear; 1.153 + float zFar; 1.154 + 1.155 + unsigned int activeSampler; // Active texture unit selector - GL_TEXTURE0 1.156 + BindingPointer<Buffer> arrayBuffer; 1.157 + BindingPointer<Buffer> elementArrayBuffer; 1.158 + GLuint readFramebuffer; 1.159 + GLuint drawFramebuffer; 1.160 + BindingPointer<Renderbuffer> renderbuffer; 1.161 + GLuint currentProgram; 1.162 + 1.163 + VertexAttribute vertexAttribute[MAX_VERTEX_ATTRIBS]; 1.164 + BindingPointer<Texture> samplerTexture[TEXTURE_TYPE_COUNT][IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS]; 1.165 + BindingPointer<Query> activeQuery[QUERY_TYPE_COUNT]; 1.166 + 1.167 + GLint unpackAlignment; 1.168 + GLint packAlignment; 1.169 + bool packReverseRowOrder; 1.170 +}; 1.171 + 1.172 +class Context 1.173 +{ 1.174 + public: 1.175 + Context(const gl::Context *shareContext, rx::Renderer *renderer, bool notifyResets, bool robustAccess); 1.176 + 1.177 + ~Context(); 1.178 + 1.179 + void makeCurrent(egl::Surface *surface); 1.180 + 1.181 + virtual void markContextLost(); 1.182 + bool isContextLost(); 1.183 + 1.184 + // State manipulation 1.185 + void setClearColor(float red, float green, float blue, float alpha); 1.186 + 1.187 + void setClearDepth(float depth); 1.188 + 1.189 + void setClearStencil(int stencil); 1.190 + 1.191 + void setCullFace(bool enabled); 1.192 + bool isCullFaceEnabled() const; 1.193 + 1.194 + void setCullMode(GLenum mode); 1.195 + 1.196 + void setFrontFace(GLenum front); 1.197 + 1.198 + void setDepthTest(bool enabled); 1.199 + bool isDepthTestEnabled() const; 1.200 + 1.201 + void setDepthFunc(GLenum depthFunc); 1.202 + 1.203 + void setDepthRange(float zNear, float zFar); 1.204 + 1.205 + void setBlend(bool enabled); 1.206 + bool isBlendEnabled() const; 1.207 + 1.208 + void setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha); 1.209 + void setBlendColor(float red, float green, float blue, float alpha); 1.210 + void setBlendEquation(GLenum rgbEquation, GLenum alphaEquation); 1.211 + 1.212 + void setStencilTest(bool enabled); 1.213 + bool isStencilTestEnabled() const; 1.214 + 1.215 + void setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask); 1.216 + void setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask); 1.217 + void setStencilWritemask(GLuint stencilWritemask); 1.218 + void setStencilBackWritemask(GLuint stencilBackWritemask); 1.219 + void setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass); 1.220 + void setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass); 1.221 + 1.222 + void setPolygonOffsetFill(bool enabled); 1.223 + bool isPolygonOffsetFillEnabled() const; 1.224 + 1.225 + void setPolygonOffsetParams(GLfloat factor, GLfloat units); 1.226 + 1.227 + void setSampleAlphaToCoverage(bool enabled); 1.228 + bool isSampleAlphaToCoverageEnabled() const; 1.229 + 1.230 + void setSampleCoverage(bool enabled); 1.231 + bool isSampleCoverageEnabled() const; 1.232 + 1.233 + void setSampleCoverageParams(GLclampf value, bool invert); 1.234 + 1.235 + void setScissorTest(bool enabled); 1.236 + bool isScissorTestEnabled() const; 1.237 + 1.238 + void setDither(bool enabled); 1.239 + bool isDitherEnabled() const; 1.240 + 1.241 + void setLineWidth(GLfloat width); 1.242 + 1.243 + void setGenerateMipmapHint(GLenum hint); 1.244 + void setFragmentShaderDerivativeHint(GLenum hint); 1.245 + 1.246 + void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height); 1.247 + 1.248 + void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height); 1.249 + 1.250 + void setColorMask(bool red, bool green, bool blue, bool alpha); 1.251 + void setDepthMask(bool mask); 1.252 + 1.253 + void setActiveSampler(unsigned int active); 1.254 + 1.255 + GLuint getReadFramebufferHandle() const; 1.256 + GLuint getDrawFramebufferHandle() const; 1.257 + GLuint getRenderbufferHandle() const; 1.258 + 1.259 + GLuint getArrayBufferHandle() const; 1.260 + 1.261 + GLuint getActiveQuery(GLenum target) const; 1.262 + 1.263 + void setEnableVertexAttribArray(unsigned int attribNum, bool enabled); 1.264 + const VertexAttribute &getVertexAttribState(unsigned int attribNum); 1.265 + void setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, 1.266 + bool normalized, GLsizei stride, const void *pointer); 1.267 + const void *getVertexAttribPointer(unsigned int attribNum) const; 1.268 + 1.269 + void setUnpackAlignment(GLint alignment); 1.270 + GLint getUnpackAlignment() const; 1.271 + 1.272 + void setPackAlignment(GLint alignment); 1.273 + GLint getPackAlignment() const; 1.274 + 1.275 + void setPackReverseRowOrder(bool reverseRowOrder); 1.276 + bool getPackReverseRowOrder() const; 1.277 + 1.278 + // These create and destroy methods are merely pass-throughs to 1.279 + // ResourceManager, which owns these object types 1.280 + GLuint createBuffer(); 1.281 + GLuint createShader(GLenum type); 1.282 + GLuint createProgram(); 1.283 + GLuint createTexture(); 1.284 + GLuint createRenderbuffer(); 1.285 + 1.286 + void deleteBuffer(GLuint buffer); 1.287 + void deleteShader(GLuint shader); 1.288 + void deleteProgram(GLuint program); 1.289 + void deleteTexture(GLuint texture); 1.290 + void deleteRenderbuffer(GLuint renderbuffer); 1.291 + 1.292 + // Framebuffers are owned by the Context, so these methods do not pass through 1.293 + GLuint createFramebuffer(); 1.294 + void deleteFramebuffer(GLuint framebuffer); 1.295 + 1.296 + // Fences are owned by the Context. 1.297 + GLuint createFence(); 1.298 + void deleteFence(GLuint fence); 1.299 + 1.300 + // Queries are owned by the Context; 1.301 + GLuint createQuery(); 1.302 + void deleteQuery(GLuint query); 1.303 + 1.304 + void bindArrayBuffer(GLuint buffer); 1.305 + void bindElementArrayBuffer(GLuint buffer); 1.306 + void bindTexture2D(GLuint texture); 1.307 + void bindTextureCubeMap(GLuint texture); 1.308 + void bindReadFramebuffer(GLuint framebuffer); 1.309 + void bindDrawFramebuffer(GLuint framebuffer); 1.310 + void bindRenderbuffer(GLuint renderbuffer); 1.311 + void useProgram(GLuint program); 1.312 + void linkProgram(GLuint program); 1.313 + void setProgramBinary(GLuint program, const void *binary, GLint length); 1.314 + 1.315 + void beginQuery(GLenum target, GLuint query); 1.316 + void endQuery(GLenum target); 1.317 + 1.318 + void setFramebufferZero(Framebuffer *framebuffer); 1.319 + 1.320 + void setRenderbufferStorage(GLsizei width, GLsizei height, GLenum internalformat, GLsizei samples); 1.321 + 1.322 + void setVertexAttrib(GLuint index, const GLfloat *values); 1.323 + void setVertexAttribDivisor(GLuint index, GLuint divisor); 1.324 + 1.325 + Buffer *getBuffer(GLuint handle); 1.326 + Fence *getFence(GLuint handle); 1.327 + Shader *getShader(GLuint handle); 1.328 + Program *getProgram(GLuint handle); 1.329 + Texture *getTexture(GLuint handle); 1.330 + Framebuffer *getFramebuffer(GLuint handle); 1.331 + Renderbuffer *getRenderbuffer(GLuint handle); 1.332 + Query *getQuery(GLuint handle, bool create, GLenum type); 1.333 + 1.334 + Buffer *getArrayBuffer(); 1.335 + Buffer *getElementArrayBuffer(); 1.336 + ProgramBinary *getCurrentProgramBinary(); 1.337 + Texture2D *getTexture2D(); 1.338 + TextureCubeMap *getTextureCubeMap(); 1.339 + Texture *getSamplerTexture(unsigned int sampler, TextureType type); 1.340 + Framebuffer *getReadFramebuffer(); 1.341 + Framebuffer *getDrawFramebuffer(); 1.342 + 1.343 + bool getFloatv(GLenum pname, GLfloat *params); 1.344 + bool getIntegerv(GLenum pname, GLint *params); 1.345 + bool getBooleanv(GLenum pname, GLboolean *params); 1.346 + 1.347 + bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams); 1.348 + 1.349 + void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels); 1.350 + void clear(GLbitfield mask); 1.351 + void drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances); 1.352 + void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances); 1.353 + void sync(bool block); // flush/finish 1.354 + 1.355 + void recordInvalidEnum(); 1.356 + void recordInvalidValue(); 1.357 + void recordInvalidOperation(); 1.358 + void recordOutOfMemory(); 1.359 + void recordInvalidFramebufferOperation(); 1.360 + 1.361 + GLenum getError(); 1.362 + GLenum getResetStatus(); 1.363 + virtual bool isResetNotificationEnabled(); 1.364 + 1.365 + int getMajorShaderModel() const; 1.366 + float getMaximumPointSize() const; 1.367 + unsigned int getMaximumCombinedTextureImageUnits() const; 1.368 + int getMaximumRenderbufferDimension() const; 1.369 + int getMaximumTextureDimension() const; 1.370 + int getMaximumCubeTextureDimension() const; 1.371 + int getMaximumTextureLevel() const; 1.372 + unsigned int getMaximumRenderTargets() const; 1.373 + GLsizei getMaxSupportedSamples() const; 1.374 + const char *getExtensionString() const; 1.375 + const char *getRendererString() const; 1.376 + bool supportsEventQueries() const; 1.377 + bool supportsOcclusionQueries() const; 1.378 + bool supportsBGRATextures() const; 1.379 + bool supportsDXT1Textures() const; 1.380 + bool supportsDXT3Textures() const; 1.381 + bool supportsDXT5Textures() const; 1.382 + bool supportsFloat32Textures() const; 1.383 + bool supportsFloat32LinearFilter() const; 1.384 + bool supportsFloat32RenderableTextures() const; 1.385 + bool supportsFloat16Textures() const; 1.386 + bool supportsFloat16LinearFilter() const; 1.387 + bool supportsFloat16RenderableTextures() const; 1.388 + bool supportsLuminanceTextures() const; 1.389 + bool supportsLuminanceAlphaTextures() const; 1.390 + bool supportsDepthTextures() const; 1.391 + bool supports32bitIndices() const; 1.392 + bool supportsNonPower2Texture() const; 1.393 + bool supportsInstancing() const; 1.394 + bool supportsTextureFilterAnisotropy() const; 1.395 + 1.396 + bool getCurrentReadFormatType(GLenum *format, GLenum *type); 1.397 + 1.398 + float getTextureMaxAnisotropy() const; 1.399 + 1.400 + void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, 1.401 + GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, 1.402 + GLbitfield mask); 1.403 + 1.404 + private: 1.405 + DISALLOW_COPY_AND_ASSIGN(Context); 1.406 + 1.407 + bool applyRenderTarget(GLenum drawMode, bool ignoreViewport); 1.408 + void applyState(GLenum drawMode); 1.409 + void applyShaders(); 1.410 + void applyTextures(); 1.411 + void applyTextures(SamplerType type); 1.412 + 1.413 + void detachBuffer(GLuint buffer); 1.414 + void detachTexture(GLuint texture); 1.415 + void detachFramebuffer(GLuint framebuffer); 1.416 + void detachRenderbuffer(GLuint renderbuffer); 1.417 + 1.418 + Texture *getIncompleteTexture(TextureType type); 1.419 + 1.420 + bool skipDraw(GLenum drawMode); 1.421 + 1.422 + void initExtensionString(); 1.423 + void initRendererString(); 1.424 + 1.425 + rx::Renderer *const mRenderer; 1.426 + 1.427 + State mState; 1.428 + 1.429 + BindingPointer<Texture2D> mTexture2DZero; 1.430 + BindingPointer<TextureCubeMap> mTextureCubeMapZero; 1.431 + 1.432 +#ifndef HASH_MAP 1.433 +# ifdef _MSC_VER 1.434 +# define HASH_MAP stdext::hash_map 1.435 +# else 1.436 +# define HASH_MAP std::unordered_map 1.437 +# endif 1.438 +#endif 1.439 + 1.440 + typedef HASH_MAP<GLuint, Framebuffer*> FramebufferMap; 1.441 + FramebufferMap mFramebufferMap; 1.442 + HandleAllocator mFramebufferHandleAllocator; 1.443 + 1.444 + typedef HASH_MAP<GLuint, Fence*> FenceMap; 1.445 + FenceMap mFenceMap; 1.446 + HandleAllocator mFenceHandleAllocator; 1.447 + 1.448 + typedef HASH_MAP<GLuint, Query*> QueryMap; 1.449 + QueryMap mQueryMap; 1.450 + HandleAllocator mQueryHandleAllocator; 1.451 + 1.452 + const char *mExtensionString; 1.453 + const char *mRendererString; 1.454 + 1.455 + BindingPointer<Texture> mIncompleteTextures[TEXTURE_TYPE_COUNT]; 1.456 + 1.457 + // Recorded errors 1.458 + bool mInvalidEnum; 1.459 + bool mInvalidValue; 1.460 + bool mInvalidOperation; 1.461 + bool mOutOfMemory; 1.462 + bool mInvalidFramebufferOperation; 1.463 + 1.464 + // Current/lost context flags 1.465 + bool mHasBeenCurrent; 1.466 + bool mContextLost; 1.467 + GLenum mResetStatus; 1.468 + GLenum mResetStrategy; 1.469 + bool mRobustAccess; 1.470 + 1.471 + BindingPointer<ProgramBinary> mCurrentProgramBinary; 1.472 + Framebuffer *mBoundDrawFramebuffer; 1.473 + 1.474 + int mMajorShaderModel; 1.475 + float mMaximumPointSize; 1.476 + bool mSupportsVertexTexture; 1.477 + bool mSupportsNonPower2Texture; 1.478 + bool mSupportsInstancing; 1.479 + int mMaxViewportDimension; 1.480 + int mMaxRenderbufferDimension; 1.481 + int mMaxTextureDimension; 1.482 + int mMaxCubeTextureDimension; 1.483 + int mMaxTextureLevel; 1.484 + float mMaxTextureAnisotropy; 1.485 + bool mSupportsEventQueries; 1.486 + bool mSupportsOcclusionQueries; 1.487 + bool mSupportsBGRATextures; 1.488 + bool mSupportsDXT1Textures; 1.489 + bool mSupportsDXT3Textures; 1.490 + bool mSupportsDXT5Textures; 1.491 + bool mSupportsFloat32Textures; 1.492 + bool mSupportsFloat32LinearFilter; 1.493 + bool mSupportsFloat32RenderableTextures; 1.494 + bool mSupportsFloat16Textures; 1.495 + bool mSupportsFloat16LinearFilter; 1.496 + bool mSupportsFloat16RenderableTextures; 1.497 + bool mSupportsLuminanceTextures; 1.498 + bool mSupportsLuminanceAlphaTextures; 1.499 + bool mSupportsDepthTextures; 1.500 + bool mSupports32bitIndices; 1.501 + bool mSupportsTextureFilterAnisotropy; 1.502 + int mNumCompressedTextureFormats; 1.503 + 1.504 + ResourceManager *mResourceManager; 1.505 +}; 1.506 +} 1.507 + 1.508 +#endif // INCLUDE_CONTEXT_H_