michael@0: #include "precompiled.h" 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.cpp: Implements 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: #include "libGLESv2/Context.h" michael@0: michael@0: #include "libGLESv2/main.h" michael@0: #include "libGLESv2/utilities.h" michael@0: #include "libGLESv2/Buffer.h" michael@0: #include "libGLESv2/Fence.h" michael@0: #include "libGLESv2/Framebuffer.h" michael@0: #include "libGLESv2/Renderbuffer.h" michael@0: #include "libGLESv2/Program.h" michael@0: #include "libGLESv2/ProgramBinary.h" michael@0: #include "libGLESv2/Query.h" michael@0: #include "libGLESv2/Texture.h" michael@0: #include "libGLESv2/ResourceManager.h" michael@0: #include "libGLESv2/renderer/IndexDataManager.h" michael@0: #include "libGLESv2/renderer/RenderTarget.h" michael@0: #include "libGLESv2/renderer/Renderer.h" michael@0: michael@0: #include "libEGL/Surface.h" michael@0: michael@0: #undef near michael@0: #undef far michael@0: michael@0: namespace gl michael@0: { michael@0: static const char* makeStaticString(const std::string& str) michael@0: { michael@0: static std::set strings; michael@0: std::set::iterator it = strings.find(str); michael@0: if (it != strings.end()) michael@0: return it->c_str(); michael@0: michael@0: return strings.insert(str).first->c_str(); michael@0: } michael@0: michael@0: Context::Context(const gl::Context *shareContext, rx::Renderer *renderer, bool notifyResets, bool robustAccess) : mRenderer(renderer) michael@0: { michael@0: ASSERT(robustAccess == false); // Unimplemented michael@0: michael@0: mFenceHandleAllocator.setBaseHandle(0); michael@0: michael@0: setClearColor(0.0f, 0.0f, 0.0f, 0.0f); michael@0: michael@0: mState.depthClearValue = 1.0f; michael@0: mState.stencilClearValue = 0; michael@0: michael@0: mState.rasterizer.cullFace = false; michael@0: mState.rasterizer.cullMode = GL_BACK; michael@0: mState.rasterizer.frontFace = GL_CCW; michael@0: mState.rasterizer.polygonOffsetFill = false; michael@0: mState.rasterizer.polygonOffsetFactor = 0.0f; michael@0: mState.rasterizer.polygonOffsetUnits = 0.0f; michael@0: mState.rasterizer.pointDrawMode = false; michael@0: mState.rasterizer.multiSample = false; michael@0: mState.scissorTest = false; michael@0: mState.scissor.x = 0; michael@0: mState.scissor.y = 0; michael@0: mState.scissor.width = 0; michael@0: mState.scissor.height = 0; michael@0: michael@0: mState.blend.blend = false; michael@0: mState.blend.sourceBlendRGB = GL_ONE; michael@0: mState.blend.sourceBlendAlpha = GL_ONE; michael@0: mState.blend.destBlendRGB = GL_ZERO; michael@0: mState.blend.destBlendAlpha = GL_ZERO; michael@0: mState.blend.blendEquationRGB = GL_FUNC_ADD; michael@0: mState.blend.blendEquationAlpha = GL_FUNC_ADD; michael@0: mState.blend.sampleAlphaToCoverage = false; michael@0: mState.blend.dither = true; michael@0: michael@0: mState.blendColor.red = 0; michael@0: mState.blendColor.green = 0; michael@0: mState.blendColor.blue = 0; michael@0: mState.blendColor.alpha = 0; michael@0: michael@0: mState.depthStencil.depthTest = false; michael@0: mState.depthStencil.depthFunc = GL_LESS; michael@0: mState.depthStencil.depthMask = true; michael@0: mState.depthStencil.stencilTest = false; michael@0: mState.depthStencil.stencilFunc = GL_ALWAYS; michael@0: mState.depthStencil.stencilMask = -1; michael@0: mState.depthStencil.stencilWritemask = -1; michael@0: mState.depthStencil.stencilBackFunc = GL_ALWAYS; michael@0: mState.depthStencil.stencilBackMask = - 1; michael@0: mState.depthStencil.stencilBackWritemask = -1; michael@0: mState.depthStencil.stencilFail = GL_KEEP; michael@0: mState.depthStencil.stencilPassDepthFail = GL_KEEP; michael@0: mState.depthStencil.stencilPassDepthPass = GL_KEEP; michael@0: mState.depthStencil.stencilBackFail = GL_KEEP; michael@0: mState.depthStencil.stencilBackPassDepthFail = GL_KEEP; michael@0: mState.depthStencil.stencilBackPassDepthPass = GL_KEEP; michael@0: michael@0: mState.stencilRef = 0; michael@0: mState.stencilBackRef = 0; michael@0: michael@0: mState.sampleCoverage = false; michael@0: mState.sampleCoverageValue = 1.0f; michael@0: mState.sampleCoverageInvert = false; michael@0: mState.generateMipmapHint = GL_DONT_CARE; michael@0: mState.fragmentShaderDerivativeHint = GL_DONT_CARE; michael@0: michael@0: mState.lineWidth = 1.0f; michael@0: michael@0: mState.viewport.x = 0; michael@0: mState.viewport.y = 0; michael@0: mState.viewport.width = 0; michael@0: mState.viewport.height = 0; michael@0: mState.zNear = 0.0f; michael@0: mState.zFar = 1.0f; michael@0: michael@0: mState.blend.colorMaskRed = true; michael@0: mState.blend.colorMaskGreen = true; michael@0: mState.blend.colorMaskBlue = true; michael@0: mState.blend.colorMaskAlpha = true; michael@0: michael@0: if (shareContext != NULL) michael@0: { michael@0: mResourceManager = shareContext->mResourceManager; michael@0: mResourceManager->addRef(); michael@0: } michael@0: else michael@0: { michael@0: mResourceManager = new ResourceManager(mRenderer); michael@0: } michael@0: michael@0: // [OpenGL ES 2.0.24] section 3.7 page 83: michael@0: // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional michael@0: // and cube map texture state vectors respectively associated with them. michael@0: // In order that access to these initial textures not be lost, they are treated as texture michael@0: // objects all of whose names are 0. michael@0: michael@0: mTexture2DZero.set(new Texture2D(mRenderer, 0)); michael@0: mTextureCubeMapZero.set(new TextureCubeMap(mRenderer, 0)); michael@0: michael@0: mState.activeSampler = 0; michael@0: bindArrayBuffer(0); michael@0: bindElementArrayBuffer(0); michael@0: bindTextureCubeMap(0); michael@0: bindTexture2D(0); michael@0: bindReadFramebuffer(0); michael@0: bindDrawFramebuffer(0); michael@0: bindRenderbuffer(0); michael@0: michael@0: mState.currentProgram = 0; michael@0: mCurrentProgramBinary.set(NULL); michael@0: michael@0: mState.packAlignment = 4; michael@0: mState.unpackAlignment = 4; michael@0: mState.packReverseRowOrder = false; michael@0: michael@0: mExtensionString = NULL; michael@0: mRendererString = NULL; michael@0: michael@0: mInvalidEnum = false; michael@0: mInvalidValue = false; michael@0: mInvalidOperation = false; michael@0: mOutOfMemory = false; michael@0: mInvalidFramebufferOperation = false; michael@0: michael@0: mHasBeenCurrent = false; michael@0: mContextLost = false; michael@0: mResetStatus = GL_NO_ERROR; michael@0: mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT); michael@0: mRobustAccess = robustAccess; michael@0: michael@0: mSupportsBGRATextures = false; michael@0: mSupportsDXT1Textures = false; michael@0: mSupportsDXT3Textures = false; michael@0: mSupportsDXT5Textures = false; michael@0: mSupportsEventQueries = false; michael@0: mSupportsOcclusionQueries = false; michael@0: mNumCompressedTextureFormats = 0; michael@0: } michael@0: michael@0: Context::~Context() michael@0: { michael@0: if (mState.currentProgram != 0) michael@0: { michael@0: Program *programObject = mResourceManager->getProgram(mState.currentProgram); michael@0: if (programObject) michael@0: { michael@0: programObject->release(); michael@0: } michael@0: mState.currentProgram = 0; michael@0: } michael@0: mCurrentProgramBinary.set(NULL); michael@0: michael@0: while (!mFramebufferMap.empty()) michael@0: { michael@0: deleteFramebuffer(mFramebufferMap.begin()->first); michael@0: } michael@0: michael@0: while (!mFenceMap.empty()) michael@0: { michael@0: deleteFence(mFenceMap.begin()->first); michael@0: } michael@0: michael@0: while (!mQueryMap.empty()) michael@0: { michael@0: deleteQuery(mQueryMap.begin()->first); michael@0: } michael@0: michael@0: for (int type = 0; type < TEXTURE_TYPE_COUNT; type++) michael@0: { michael@0: for (int sampler = 0; sampler < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; sampler++) michael@0: { michael@0: mState.samplerTexture[type][sampler].set(NULL); michael@0: } michael@0: } michael@0: michael@0: for (int type = 0; type < TEXTURE_TYPE_COUNT; type++) michael@0: { michael@0: mIncompleteTextures[type].set(NULL); michael@0: } michael@0: michael@0: for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: mState.vertexAttribute[i].mBoundBuffer.set(NULL); michael@0: } michael@0: michael@0: for (int i = 0; i < QUERY_TYPE_COUNT; i++) michael@0: { michael@0: mState.activeQuery[i].set(NULL); michael@0: } michael@0: michael@0: mState.arrayBuffer.set(NULL); michael@0: mState.elementArrayBuffer.set(NULL); michael@0: mState.renderbuffer.set(NULL); michael@0: michael@0: mTexture2DZero.set(NULL); michael@0: mTextureCubeMapZero.set(NULL); michael@0: michael@0: mResourceManager->release(); michael@0: } michael@0: michael@0: void Context::makeCurrent(egl::Surface *surface) michael@0: { michael@0: if (!mHasBeenCurrent) michael@0: { michael@0: mMajorShaderModel = mRenderer->getMajorShaderModel(); michael@0: mMaximumPointSize = mRenderer->getMaxPointSize(); michael@0: mSupportsVertexTexture = mRenderer->getVertexTextureSupport(); michael@0: mSupportsNonPower2Texture = mRenderer->getNonPower2TextureSupport(); michael@0: mSupportsInstancing = mRenderer->getInstancingSupport(); michael@0: michael@0: mMaxViewportDimension = mRenderer->getMaxViewportDimension(); michael@0: mMaxTextureDimension = std::min(std::min(mRenderer->getMaxTextureWidth(), mRenderer->getMaxTextureHeight()), michael@0: (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE); michael@0: mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE); michael@0: mMaxRenderbufferDimension = mMaxTextureDimension; michael@0: mMaxTextureLevel = log2(mMaxTextureDimension) + 1; michael@0: mMaxTextureAnisotropy = mRenderer->getTextureMaxAnisotropy(); michael@0: TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d, MaxTextureAnisotropy=%f", michael@0: mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel, mMaxTextureAnisotropy); michael@0: michael@0: mSupportsEventQueries = mRenderer->getEventQuerySupport(); michael@0: mSupportsOcclusionQueries = mRenderer->getOcclusionQuerySupport(); michael@0: mSupportsBGRATextures = mRenderer->getBGRATextureSupport(); michael@0: mSupportsDXT1Textures = mRenderer->getDXT1TextureSupport(); michael@0: mSupportsDXT3Textures = mRenderer->getDXT3TextureSupport(); michael@0: mSupportsDXT5Textures = mRenderer->getDXT5TextureSupport(); michael@0: mSupportsFloat32Textures = mRenderer->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures); michael@0: mSupportsFloat16Textures = mRenderer->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures); michael@0: mSupportsLuminanceTextures = mRenderer->getLuminanceTextureSupport(); michael@0: mSupportsLuminanceAlphaTextures = mRenderer->getLuminanceAlphaTextureSupport(); michael@0: mSupportsDepthTextures = mRenderer->getDepthTextureSupport(); michael@0: mSupportsTextureFilterAnisotropy = mRenderer->getTextureFilterAnisotropySupport(); michael@0: mSupports32bitIndices = mRenderer->get32BitIndexSupport(); michael@0: michael@0: mNumCompressedTextureFormats = 0; michael@0: if (supportsDXT1Textures()) michael@0: { michael@0: mNumCompressedTextureFormats += 2; michael@0: } michael@0: if (supportsDXT3Textures()) michael@0: { michael@0: mNumCompressedTextureFormats += 1; michael@0: } michael@0: if (supportsDXT5Textures()) michael@0: { michael@0: mNumCompressedTextureFormats += 1; michael@0: } michael@0: michael@0: initExtensionString(); michael@0: initRendererString(); michael@0: michael@0: mState.viewport.x = 0; michael@0: mState.viewport.y = 0; michael@0: mState.viewport.width = surface->getWidth(); michael@0: mState.viewport.height = surface->getHeight(); michael@0: michael@0: mState.scissor.x = 0; michael@0: mState.scissor.y = 0; michael@0: mState.scissor.width = surface->getWidth(); michael@0: mState.scissor.height = surface->getHeight(); michael@0: michael@0: mHasBeenCurrent = true; michael@0: } michael@0: michael@0: // Wrap the existing swapchain resources into GL objects and assign them to the '0' names michael@0: rx::SwapChain *swapchain = surface->getSwapChain(); michael@0: michael@0: Colorbuffer *colorbufferZero = new Colorbuffer(mRenderer, swapchain); michael@0: DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(mRenderer, swapchain); michael@0: Framebuffer *framebufferZero = new DefaultFramebuffer(mRenderer, colorbufferZero, depthStencilbufferZero); michael@0: michael@0: setFramebufferZero(framebufferZero); michael@0: } michael@0: michael@0: // NOTE: this function should not assume that this context is current! michael@0: void Context::markContextLost() michael@0: { michael@0: if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT) michael@0: mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT; michael@0: mContextLost = true; michael@0: } michael@0: michael@0: bool Context::isContextLost() michael@0: { michael@0: return mContextLost; michael@0: } michael@0: michael@0: void Context::setClearColor(float red, float green, float blue, float alpha) michael@0: { michael@0: mState.colorClearValue.red = red; michael@0: mState.colorClearValue.green = green; michael@0: mState.colorClearValue.blue = blue; michael@0: mState.colorClearValue.alpha = alpha; michael@0: } michael@0: michael@0: void Context::setClearDepth(float depth) michael@0: { michael@0: mState.depthClearValue = depth; michael@0: } michael@0: michael@0: void Context::setClearStencil(int stencil) michael@0: { michael@0: mState.stencilClearValue = stencil; michael@0: } michael@0: michael@0: void Context::setCullFace(bool enabled) michael@0: { michael@0: mState.rasterizer.cullFace = enabled; michael@0: } michael@0: michael@0: bool Context::isCullFaceEnabled() const michael@0: { michael@0: return mState.rasterizer.cullFace; michael@0: } michael@0: michael@0: void Context::setCullMode(GLenum mode) michael@0: { michael@0: mState.rasterizer.cullMode = mode; michael@0: } michael@0: michael@0: void Context::setFrontFace(GLenum front) michael@0: { michael@0: mState.rasterizer.frontFace = front; michael@0: } michael@0: michael@0: void Context::setDepthTest(bool enabled) michael@0: { michael@0: mState.depthStencil.depthTest = enabled; michael@0: } michael@0: michael@0: bool Context::isDepthTestEnabled() const michael@0: { michael@0: return mState.depthStencil.depthTest; michael@0: } michael@0: michael@0: void Context::setDepthFunc(GLenum depthFunc) michael@0: { michael@0: mState.depthStencil.depthFunc = depthFunc; michael@0: } michael@0: michael@0: void Context::setDepthRange(float zNear, float zFar) michael@0: { michael@0: mState.zNear = zNear; michael@0: mState.zFar = zFar; michael@0: } michael@0: michael@0: void Context::setBlend(bool enabled) michael@0: { michael@0: mState.blend.blend = enabled; michael@0: } michael@0: michael@0: bool Context::isBlendEnabled() const michael@0: { michael@0: return mState.blend.blend; michael@0: } michael@0: michael@0: void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha) michael@0: { michael@0: mState.blend.sourceBlendRGB = sourceRGB; michael@0: mState.blend.destBlendRGB = destRGB; michael@0: mState.blend.sourceBlendAlpha = sourceAlpha; michael@0: mState.blend.destBlendAlpha = destAlpha; michael@0: } michael@0: michael@0: void Context::setBlendColor(float red, float green, float blue, float alpha) michael@0: { michael@0: mState.blendColor.red = red; michael@0: mState.blendColor.green = green; michael@0: mState.blendColor.blue = blue; michael@0: mState.blendColor.alpha = alpha; michael@0: } michael@0: michael@0: void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation) michael@0: { michael@0: mState.blend.blendEquationRGB = rgbEquation; michael@0: mState.blend.blendEquationAlpha = alphaEquation; michael@0: } michael@0: michael@0: void Context::setStencilTest(bool enabled) michael@0: { michael@0: mState.depthStencil.stencilTest = enabled; michael@0: } michael@0: michael@0: bool Context::isStencilTestEnabled() const michael@0: { michael@0: return mState.depthStencil.stencilTest; michael@0: } michael@0: michael@0: void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask) michael@0: { michael@0: mState.depthStencil.stencilFunc = stencilFunc; michael@0: mState.stencilRef = (stencilRef > 0) ? stencilRef : 0; michael@0: mState.depthStencil.stencilMask = stencilMask; michael@0: } michael@0: michael@0: void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask) michael@0: { michael@0: mState.depthStencil.stencilBackFunc = stencilBackFunc; michael@0: mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0; michael@0: mState.depthStencil.stencilBackMask = stencilBackMask; michael@0: } michael@0: michael@0: void Context::setStencilWritemask(GLuint stencilWritemask) michael@0: { michael@0: mState.depthStencil.stencilWritemask = stencilWritemask; michael@0: } michael@0: michael@0: void Context::setStencilBackWritemask(GLuint stencilBackWritemask) michael@0: { michael@0: mState.depthStencil.stencilBackWritemask = stencilBackWritemask; michael@0: } michael@0: michael@0: void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass) michael@0: { michael@0: mState.depthStencil.stencilFail = stencilFail; michael@0: mState.depthStencil.stencilPassDepthFail = stencilPassDepthFail; michael@0: mState.depthStencil.stencilPassDepthPass = stencilPassDepthPass; michael@0: } michael@0: michael@0: void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass) michael@0: { michael@0: mState.depthStencil.stencilBackFail = stencilBackFail; michael@0: mState.depthStencil.stencilBackPassDepthFail = stencilBackPassDepthFail; michael@0: mState.depthStencil.stencilBackPassDepthPass = stencilBackPassDepthPass; michael@0: } michael@0: michael@0: void Context::setPolygonOffsetFill(bool enabled) michael@0: { michael@0: mState.rasterizer.polygonOffsetFill = enabled; michael@0: } michael@0: michael@0: bool Context::isPolygonOffsetFillEnabled() const michael@0: { michael@0: return mState.rasterizer.polygonOffsetFill; michael@0: } michael@0: michael@0: void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units) michael@0: { michael@0: // An application can pass NaN values here, so handle this gracefully michael@0: mState.rasterizer.polygonOffsetFactor = factor != factor ? 0.0f : factor; michael@0: mState.rasterizer.polygonOffsetUnits = units != units ? 0.0f : units; michael@0: } michael@0: michael@0: void Context::setSampleAlphaToCoverage(bool enabled) michael@0: { michael@0: mState.blend.sampleAlphaToCoverage = enabled; michael@0: } michael@0: michael@0: bool Context::isSampleAlphaToCoverageEnabled() const michael@0: { michael@0: return mState.blend.sampleAlphaToCoverage; michael@0: } michael@0: michael@0: void Context::setSampleCoverage(bool enabled) michael@0: { michael@0: mState.sampleCoverage = enabled; michael@0: } michael@0: michael@0: bool Context::isSampleCoverageEnabled() const michael@0: { michael@0: return mState.sampleCoverage; michael@0: } michael@0: michael@0: void Context::setSampleCoverageParams(GLclampf value, bool invert) michael@0: { michael@0: mState.sampleCoverageValue = value; michael@0: mState.sampleCoverageInvert = invert; michael@0: } michael@0: michael@0: void Context::setScissorTest(bool enabled) michael@0: { michael@0: mState.scissorTest = enabled; michael@0: } michael@0: michael@0: bool Context::isScissorTestEnabled() const michael@0: { michael@0: return mState.scissorTest; michael@0: } michael@0: michael@0: void Context::setDither(bool enabled) michael@0: { michael@0: mState.blend.dither = enabled; michael@0: } michael@0: michael@0: bool Context::isDitherEnabled() const michael@0: { michael@0: return mState.blend.dither; michael@0: } michael@0: michael@0: void Context::setLineWidth(GLfloat width) michael@0: { michael@0: mState.lineWidth = width; michael@0: } michael@0: michael@0: void Context::setGenerateMipmapHint(GLenum hint) michael@0: { michael@0: mState.generateMipmapHint = hint; michael@0: } michael@0: michael@0: void Context::setFragmentShaderDerivativeHint(GLenum hint) michael@0: { michael@0: mState.fragmentShaderDerivativeHint = hint; michael@0: // TODO: Propagate the hint to shader translator so we can write michael@0: // ddx, ddx_coarse, or ddx_fine depending on the hint. michael@0: // Ignore for now. It is valid for implementations to ignore hint. michael@0: } michael@0: michael@0: void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height) michael@0: { michael@0: mState.viewport.x = x; michael@0: mState.viewport.y = y; michael@0: mState.viewport.width = width; michael@0: mState.viewport.height = height; michael@0: } michael@0: michael@0: void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height) michael@0: { michael@0: mState.scissor.x = x; michael@0: mState.scissor.y = y; michael@0: mState.scissor.width = width; michael@0: mState.scissor.height = height; michael@0: } michael@0: michael@0: void Context::setColorMask(bool red, bool green, bool blue, bool alpha) michael@0: { michael@0: mState.blend.colorMaskRed = red; michael@0: mState.blend.colorMaskGreen = green; michael@0: mState.blend.colorMaskBlue = blue; michael@0: mState.blend.colorMaskAlpha = alpha; michael@0: } michael@0: michael@0: void Context::setDepthMask(bool mask) michael@0: { michael@0: mState.depthStencil.depthMask = mask; michael@0: } michael@0: michael@0: void Context::setActiveSampler(unsigned int active) michael@0: { michael@0: mState.activeSampler = active; michael@0: } michael@0: michael@0: GLuint Context::getReadFramebufferHandle() const michael@0: { michael@0: return mState.readFramebuffer; michael@0: } michael@0: michael@0: GLuint Context::getDrawFramebufferHandle() const michael@0: { michael@0: return mState.drawFramebuffer; michael@0: } michael@0: michael@0: GLuint Context::getRenderbufferHandle() const michael@0: { michael@0: return mState.renderbuffer.id(); michael@0: } michael@0: michael@0: GLuint Context::getArrayBufferHandle() const michael@0: { michael@0: return mState.arrayBuffer.id(); michael@0: } michael@0: michael@0: GLuint Context::getActiveQuery(GLenum target) const michael@0: { michael@0: Query *queryObject = NULL; michael@0: michael@0: switch (target) michael@0: { michael@0: case GL_ANY_SAMPLES_PASSED_EXT: michael@0: queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get(); michael@0: break; michael@0: case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT: michael@0: queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get(); michael@0: break; michael@0: default: michael@0: ASSERT(false); michael@0: } michael@0: michael@0: if (queryObject) michael@0: { michael@0: return queryObject->id(); michael@0: } michael@0: else michael@0: { michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled) michael@0: { michael@0: mState.vertexAttribute[attribNum].mArrayEnabled = enabled; michael@0: } michael@0: michael@0: const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum) michael@0: { michael@0: return mState.vertexAttribute[attribNum]; michael@0: } michael@0: michael@0: void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized, michael@0: GLsizei stride, const void *pointer) michael@0: { michael@0: mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer); michael@0: mState.vertexAttribute[attribNum].mSize = size; michael@0: mState.vertexAttribute[attribNum].mType = type; michael@0: mState.vertexAttribute[attribNum].mNormalized = normalized; michael@0: mState.vertexAttribute[attribNum].mStride = stride; michael@0: mState.vertexAttribute[attribNum].mPointer = pointer; michael@0: } michael@0: michael@0: const void *Context::getVertexAttribPointer(unsigned int attribNum) const michael@0: { michael@0: return mState.vertexAttribute[attribNum].mPointer; michael@0: } michael@0: michael@0: void Context::setPackAlignment(GLint alignment) michael@0: { michael@0: mState.packAlignment = alignment; michael@0: } michael@0: michael@0: GLint Context::getPackAlignment() const michael@0: { michael@0: return mState.packAlignment; michael@0: } michael@0: michael@0: void Context::setUnpackAlignment(GLint alignment) michael@0: { michael@0: mState.unpackAlignment = alignment; michael@0: } michael@0: michael@0: GLint Context::getUnpackAlignment() const michael@0: { michael@0: return mState.unpackAlignment; michael@0: } michael@0: michael@0: void Context::setPackReverseRowOrder(bool reverseRowOrder) michael@0: { michael@0: mState.packReverseRowOrder = reverseRowOrder; michael@0: } michael@0: michael@0: bool Context::getPackReverseRowOrder() const michael@0: { michael@0: return mState.packReverseRowOrder; michael@0: } michael@0: michael@0: GLuint Context::createBuffer() michael@0: { michael@0: return mResourceManager->createBuffer(); michael@0: } michael@0: michael@0: GLuint Context::createProgram() michael@0: { michael@0: return mResourceManager->createProgram(); michael@0: } michael@0: michael@0: GLuint Context::createShader(GLenum type) michael@0: { michael@0: return mResourceManager->createShader(type); michael@0: } michael@0: michael@0: GLuint Context::createTexture() michael@0: { michael@0: return mResourceManager->createTexture(); michael@0: } michael@0: michael@0: GLuint Context::createRenderbuffer() michael@0: { michael@0: return mResourceManager->createRenderbuffer(); michael@0: } michael@0: michael@0: // Returns an unused framebuffer name michael@0: GLuint Context::createFramebuffer() michael@0: { michael@0: GLuint handle = mFramebufferHandleAllocator.allocate(); michael@0: michael@0: mFramebufferMap[handle] = NULL; michael@0: michael@0: return handle; michael@0: } michael@0: michael@0: GLuint Context::createFence() michael@0: { michael@0: GLuint handle = mFenceHandleAllocator.allocate(); michael@0: michael@0: mFenceMap[handle] = new Fence(mRenderer); michael@0: michael@0: return handle; michael@0: } michael@0: michael@0: // Returns an unused query name michael@0: GLuint Context::createQuery() michael@0: { michael@0: GLuint handle = mQueryHandleAllocator.allocate(); michael@0: michael@0: mQueryMap[handle] = NULL; michael@0: michael@0: return handle; michael@0: } michael@0: michael@0: void Context::deleteBuffer(GLuint buffer) michael@0: { michael@0: if (mResourceManager->getBuffer(buffer)) michael@0: { michael@0: detachBuffer(buffer); michael@0: } michael@0: michael@0: mResourceManager->deleteBuffer(buffer); michael@0: } michael@0: michael@0: void Context::deleteShader(GLuint shader) michael@0: { michael@0: mResourceManager->deleteShader(shader); michael@0: } michael@0: michael@0: void Context::deleteProgram(GLuint program) michael@0: { michael@0: mResourceManager->deleteProgram(program); michael@0: } michael@0: michael@0: void Context::deleteTexture(GLuint texture) michael@0: { michael@0: if (mResourceManager->getTexture(texture)) michael@0: { michael@0: detachTexture(texture); michael@0: } michael@0: michael@0: mResourceManager->deleteTexture(texture); michael@0: } michael@0: michael@0: void Context::deleteRenderbuffer(GLuint renderbuffer) michael@0: { michael@0: if (mResourceManager->getRenderbuffer(renderbuffer)) michael@0: { michael@0: detachRenderbuffer(renderbuffer); michael@0: } michael@0: michael@0: mResourceManager->deleteRenderbuffer(renderbuffer); michael@0: } michael@0: michael@0: void Context::deleteFramebuffer(GLuint framebuffer) michael@0: { michael@0: FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer); michael@0: michael@0: if (framebufferObject != mFramebufferMap.end()) michael@0: { michael@0: detachFramebuffer(framebuffer); michael@0: michael@0: mFramebufferHandleAllocator.release(framebufferObject->first); michael@0: delete framebufferObject->second; michael@0: mFramebufferMap.erase(framebufferObject); michael@0: } michael@0: } michael@0: michael@0: void Context::deleteFence(GLuint fence) michael@0: { michael@0: FenceMap::iterator fenceObject = mFenceMap.find(fence); michael@0: michael@0: if (fenceObject != mFenceMap.end()) michael@0: { michael@0: mFenceHandleAllocator.release(fenceObject->first); michael@0: delete fenceObject->second; michael@0: mFenceMap.erase(fenceObject); michael@0: } michael@0: } michael@0: michael@0: void Context::deleteQuery(GLuint query) michael@0: { michael@0: QueryMap::iterator queryObject = mQueryMap.find(query); michael@0: if (queryObject != mQueryMap.end()) michael@0: { michael@0: mQueryHandleAllocator.release(queryObject->first); michael@0: if (queryObject->second) michael@0: { michael@0: queryObject->second->release(); michael@0: } michael@0: mQueryMap.erase(queryObject); michael@0: } michael@0: } michael@0: michael@0: Buffer *Context::getBuffer(GLuint handle) michael@0: { michael@0: return mResourceManager->getBuffer(handle); michael@0: } michael@0: michael@0: Shader *Context::getShader(GLuint handle) michael@0: { michael@0: return mResourceManager->getShader(handle); michael@0: } michael@0: michael@0: Program *Context::getProgram(GLuint handle) michael@0: { michael@0: return mResourceManager->getProgram(handle); michael@0: } michael@0: michael@0: Texture *Context::getTexture(GLuint handle) michael@0: { michael@0: return mResourceManager->getTexture(handle); michael@0: } michael@0: michael@0: Renderbuffer *Context::getRenderbuffer(GLuint handle) michael@0: { michael@0: return mResourceManager->getRenderbuffer(handle); michael@0: } michael@0: michael@0: Framebuffer *Context::getReadFramebuffer() michael@0: { michael@0: return getFramebuffer(mState.readFramebuffer); michael@0: } michael@0: michael@0: Framebuffer *Context::getDrawFramebuffer() michael@0: { michael@0: return mBoundDrawFramebuffer; michael@0: } michael@0: michael@0: void Context::bindArrayBuffer(unsigned int buffer) michael@0: { michael@0: mResourceManager->checkBufferAllocation(buffer); michael@0: michael@0: mState.arrayBuffer.set(getBuffer(buffer)); michael@0: } michael@0: michael@0: void Context::bindElementArrayBuffer(unsigned int buffer) michael@0: { michael@0: mResourceManager->checkBufferAllocation(buffer); michael@0: michael@0: mState.elementArrayBuffer.set(getBuffer(buffer)); michael@0: } michael@0: michael@0: void Context::bindTexture2D(GLuint texture) michael@0: { michael@0: mResourceManager->checkTextureAllocation(texture, TEXTURE_2D); michael@0: michael@0: mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture)); michael@0: } michael@0: michael@0: void Context::bindTextureCubeMap(GLuint texture) michael@0: { michael@0: mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE); michael@0: michael@0: mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture)); michael@0: } michael@0: michael@0: void Context::bindReadFramebuffer(GLuint framebuffer) michael@0: { michael@0: if (!getFramebuffer(framebuffer)) michael@0: { michael@0: mFramebufferMap[framebuffer] = new Framebuffer(mRenderer); michael@0: } michael@0: michael@0: mState.readFramebuffer = framebuffer; michael@0: } michael@0: michael@0: void Context::bindDrawFramebuffer(GLuint framebuffer) michael@0: { michael@0: if (!getFramebuffer(framebuffer)) michael@0: { michael@0: mFramebufferMap[framebuffer] = new Framebuffer(mRenderer); michael@0: } michael@0: michael@0: mState.drawFramebuffer = framebuffer; michael@0: michael@0: mBoundDrawFramebuffer = getFramebuffer(framebuffer); michael@0: } michael@0: michael@0: void Context::bindRenderbuffer(GLuint renderbuffer) michael@0: { michael@0: mResourceManager->checkRenderbufferAllocation(renderbuffer); michael@0: michael@0: mState.renderbuffer.set(getRenderbuffer(renderbuffer)); michael@0: } michael@0: michael@0: void Context::useProgram(GLuint program) michael@0: { michael@0: GLuint priorProgram = mState.currentProgram; michael@0: mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged. michael@0: michael@0: if (priorProgram != program) michael@0: { michael@0: Program *newProgram = mResourceManager->getProgram(program); michael@0: Program *oldProgram = mResourceManager->getProgram(priorProgram); michael@0: mCurrentProgramBinary.set(NULL); michael@0: michael@0: if (newProgram) michael@0: { michael@0: newProgram->addRef(); michael@0: mCurrentProgramBinary.set(newProgram->getProgramBinary()); michael@0: } michael@0: michael@0: if (oldProgram) michael@0: { michael@0: oldProgram->release(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Context::linkProgram(GLuint program) michael@0: { michael@0: Program *programObject = mResourceManager->getProgram(program); michael@0: michael@0: bool linked = programObject->link(); michael@0: michael@0: // if the current program was relinked successfully we michael@0: // need to install the new executables michael@0: if (linked && program == mState.currentProgram) michael@0: { michael@0: mCurrentProgramBinary.set(programObject->getProgramBinary()); michael@0: } michael@0: } michael@0: michael@0: void Context::setProgramBinary(GLuint program, const void *binary, GLint length) michael@0: { michael@0: Program *programObject = mResourceManager->getProgram(program); michael@0: michael@0: bool loaded = programObject->setProgramBinary(binary, length); michael@0: michael@0: // if the current program was reloaded successfully we michael@0: // need to install the new executables michael@0: if (loaded && program == mState.currentProgram) michael@0: { michael@0: mCurrentProgramBinary.set(programObject->getProgramBinary()); michael@0: } michael@0: michael@0: } michael@0: michael@0: void Context::beginQuery(GLenum target, GLuint query) michael@0: { michael@0: // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an michael@0: // of zero, if the active query object name for is non-zero (for the michael@0: // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if michael@0: // the active query for either target is non-zero), if is the name of an michael@0: // existing query object whose type does not match , or if is the michael@0: // active query object name for any query type, the error INVALID_OPERATION is michael@0: // generated. michael@0: michael@0: // Ensure no other queries are active michael@0: // NOTE: If other queries than occlusion are supported, we will need to check michael@0: // separately that: michael@0: // a) The query ID passed is not the current active query for any target/type michael@0: // b) There are no active queries for the requested target (and in the case michael@0: // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, michael@0: // no query may be active for either if glBeginQuery targets either. michael@0: for (int i = 0; i < QUERY_TYPE_COUNT; i++) michael@0: { michael@0: if (mState.activeQuery[i].get() != NULL) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: } michael@0: michael@0: QueryType qType; michael@0: switch (target) michael@0: { michael@0: case GL_ANY_SAMPLES_PASSED_EXT: michael@0: qType = QUERY_ANY_SAMPLES_PASSED; michael@0: break; michael@0: case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT: michael@0: qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE; michael@0: break; michael@0: default: michael@0: ASSERT(false); michael@0: return; michael@0: } michael@0: michael@0: Query *queryObject = getQuery(query, true, target); michael@0: michael@0: // check that name was obtained with glGenQueries michael@0: if (!queryObject) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: // check for type mismatch michael@0: if (queryObject->getType() != target) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: // set query as active for specified target michael@0: mState.activeQuery[qType].set(queryObject); michael@0: michael@0: // begin query michael@0: queryObject->begin(); michael@0: } michael@0: michael@0: void Context::endQuery(GLenum target) michael@0: { michael@0: QueryType qType; michael@0: michael@0: switch (target) michael@0: { michael@0: case GL_ANY_SAMPLES_PASSED_EXT: michael@0: qType = QUERY_ANY_SAMPLES_PASSED; michael@0: break; michael@0: case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT: michael@0: qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE; michael@0: break; michael@0: default: michael@0: ASSERT(false); michael@0: return; michael@0: } michael@0: michael@0: Query *queryObject = mState.activeQuery[qType].get(); michael@0: michael@0: if (queryObject == NULL) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: queryObject->end(); michael@0: michael@0: mState.activeQuery[qType].set(NULL); michael@0: } michael@0: michael@0: void Context::setFramebufferZero(Framebuffer *buffer) michael@0: { michael@0: delete mFramebufferMap[0]; michael@0: mFramebufferMap[0] = buffer; michael@0: if (mState.drawFramebuffer == 0) michael@0: { michael@0: mBoundDrawFramebuffer = buffer; michael@0: } michael@0: } michael@0: michael@0: void Context::setRenderbufferStorage(GLsizei width, GLsizei height, GLenum internalformat, GLsizei samples) michael@0: { michael@0: RenderbufferStorage *renderbuffer = NULL; michael@0: switch (internalformat) michael@0: { michael@0: case GL_DEPTH_COMPONENT16: michael@0: renderbuffer = new gl::Depthbuffer(mRenderer, width, height, samples); michael@0: break; michael@0: case GL_RGBA4: michael@0: case GL_RGB5_A1: michael@0: case GL_RGB565: michael@0: case GL_RGB8_OES: michael@0: case GL_RGBA8_OES: michael@0: renderbuffer = new gl::Colorbuffer(mRenderer,width, height, internalformat, samples); michael@0: break; michael@0: case GL_STENCIL_INDEX8: michael@0: renderbuffer = new gl::Stencilbuffer(mRenderer, width, height, samples); michael@0: break; michael@0: case GL_DEPTH24_STENCIL8_OES: michael@0: renderbuffer = new gl::DepthStencilbuffer(mRenderer, width, height, samples); michael@0: break; michael@0: default: michael@0: UNREACHABLE(); return; michael@0: } michael@0: michael@0: Renderbuffer *renderbufferObject = mState.renderbuffer.get(); michael@0: renderbufferObject->setStorage(renderbuffer); michael@0: } michael@0: michael@0: Framebuffer *Context::getFramebuffer(unsigned int handle) michael@0: { michael@0: FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle); michael@0: michael@0: if (framebuffer == mFramebufferMap.end()) michael@0: { michael@0: return NULL; michael@0: } michael@0: else michael@0: { michael@0: return framebuffer->second; michael@0: } michael@0: } michael@0: michael@0: Fence *Context::getFence(unsigned int handle) michael@0: { michael@0: FenceMap::iterator fence = mFenceMap.find(handle); michael@0: michael@0: if (fence == mFenceMap.end()) michael@0: { michael@0: return NULL; michael@0: } michael@0: else michael@0: { michael@0: return fence->second; michael@0: } michael@0: } michael@0: michael@0: Query *Context::getQuery(unsigned int handle, bool create, GLenum type) michael@0: { michael@0: QueryMap::iterator query = mQueryMap.find(handle); michael@0: michael@0: if (query == mQueryMap.end()) michael@0: { michael@0: return NULL; michael@0: } michael@0: else michael@0: { michael@0: if (!query->second && create) michael@0: { michael@0: query->second = new Query(mRenderer, type, handle); michael@0: query->second->addRef(); michael@0: } michael@0: return query->second; michael@0: } michael@0: } michael@0: michael@0: Buffer *Context::getArrayBuffer() michael@0: { michael@0: return mState.arrayBuffer.get(); michael@0: } michael@0: michael@0: Buffer *Context::getElementArrayBuffer() michael@0: { michael@0: return mState.elementArrayBuffer.get(); michael@0: } michael@0: michael@0: ProgramBinary *Context::getCurrentProgramBinary() michael@0: { michael@0: return mCurrentProgramBinary.get(); michael@0: } michael@0: michael@0: Texture2D *Context::getTexture2D() michael@0: { michael@0: return static_cast(getSamplerTexture(mState.activeSampler, TEXTURE_2D)); michael@0: } michael@0: michael@0: TextureCubeMap *Context::getTextureCubeMap() michael@0: { michael@0: return static_cast(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE)); michael@0: } michael@0: michael@0: Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type) michael@0: { michael@0: GLuint texid = mState.samplerTexture[type][sampler].id(); michael@0: michael@0: if (texid == 0) // Special case: 0 refers to different initial textures based on the target michael@0: { michael@0: switch (type) michael@0: { michael@0: default: UNREACHABLE(); michael@0: case TEXTURE_2D: return mTexture2DZero.get(); michael@0: case TEXTURE_CUBE: return mTextureCubeMapZero.get(); michael@0: } michael@0: } michael@0: michael@0: return mState.samplerTexture[type][sampler].get(); michael@0: } michael@0: michael@0: bool Context::getBooleanv(GLenum pname, GLboolean *params) michael@0: { michael@0: switch (pname) michael@0: { michael@0: case GL_SHADER_COMPILER: *params = GL_TRUE; break; michael@0: case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break; michael@0: case GL_DEPTH_WRITEMASK: *params = mState.depthStencil.depthMask; break; michael@0: case GL_COLOR_WRITEMASK: michael@0: params[0] = mState.blend.colorMaskRed; michael@0: params[1] = mState.blend.colorMaskGreen; michael@0: params[2] = mState.blend.colorMaskBlue; michael@0: params[3] = mState.blend.colorMaskAlpha; michael@0: break; michael@0: case GL_CULL_FACE: *params = mState.rasterizer.cullFace; break; michael@0: case GL_POLYGON_OFFSET_FILL: *params = mState.rasterizer.polygonOffsetFill; break; michael@0: case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.blend.sampleAlphaToCoverage; break; michael@0: case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break; michael@0: case GL_SCISSOR_TEST: *params = mState.scissorTest; break; michael@0: case GL_STENCIL_TEST: *params = mState.depthStencil.stencilTest; break; michael@0: case GL_DEPTH_TEST: *params = mState.depthStencil.depthTest; break; michael@0: case GL_BLEND: *params = mState.blend.blend; break; michael@0: case GL_DITHER: *params = mState.blend.dither; break; michael@0: case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break; michael@0: default: michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool Context::getFloatv(GLenum pname, GLfloat *params) michael@0: { michael@0: // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation michael@0: // because it is stored as a float, despite the fact that the GL ES 2.0 spec names michael@0: // GetIntegerv as its native query function. As it would require conversion in any michael@0: // case, this should make no difference to the calling application. michael@0: switch (pname) michael@0: { michael@0: case GL_LINE_WIDTH: *params = mState.lineWidth; break; michael@0: case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break; michael@0: case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break; michael@0: case GL_POLYGON_OFFSET_FACTOR: *params = mState.rasterizer.polygonOffsetFactor; break; michael@0: case GL_POLYGON_OFFSET_UNITS: *params = mState.rasterizer.polygonOffsetUnits; break; michael@0: case GL_ALIASED_LINE_WIDTH_RANGE: michael@0: params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN; michael@0: params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX; michael@0: break; michael@0: case GL_ALIASED_POINT_SIZE_RANGE: michael@0: params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN; michael@0: params[1] = getMaximumPointSize(); michael@0: break; michael@0: case GL_DEPTH_RANGE: michael@0: params[0] = mState.zNear; michael@0: params[1] = mState.zFar; michael@0: break; michael@0: case GL_COLOR_CLEAR_VALUE: michael@0: params[0] = mState.colorClearValue.red; michael@0: params[1] = mState.colorClearValue.green; michael@0: params[2] = mState.colorClearValue.blue; michael@0: params[3] = mState.colorClearValue.alpha; michael@0: break; michael@0: case GL_BLEND_COLOR: michael@0: params[0] = mState.blendColor.red; michael@0: params[1] = mState.blendColor.green; michael@0: params[2] = mState.blendColor.blue; michael@0: params[3] = mState.blendColor.alpha; michael@0: break; michael@0: case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT: michael@0: if (!supportsTextureFilterAnisotropy()) michael@0: { michael@0: return false; michael@0: } michael@0: *params = mMaxTextureAnisotropy; michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool Context::getIntegerv(GLenum pname, GLint *params) michael@0: { michael@0: if (pname >= GL_DRAW_BUFFER0_EXT && pname <= GL_DRAW_BUFFER15_EXT) michael@0: { michael@0: unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0_EXT); michael@0: michael@0: if (colorAttachment >= mRenderer->getMaxRenderTargets()) michael@0: { michael@0: // return true to stop further operation in the parent call michael@0: return gl::error(GL_INVALID_OPERATION, true); michael@0: } michael@0: michael@0: Framebuffer *framebuffer = getDrawFramebuffer(); michael@0: michael@0: *params = framebuffer->getDrawBufferState(colorAttachment); michael@0: return true; michael@0: } michael@0: michael@0: // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation michael@0: // because it is stored as a float, despite the fact that the GL ES 2.0 spec names michael@0: // GetIntegerv as its native query function. As it would require conversion in any michael@0: // case, this should make no difference to the calling application. You may find it in michael@0: // Context::getFloatv. michael@0: switch (pname) michael@0: { michael@0: case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break; michael@0: case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = mRenderer->getMaxVertexUniformVectors(); break; michael@0: case GL_MAX_VARYING_VECTORS: *params = mRenderer->getMaxVaryingVectors(); break; michael@0: case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = mRenderer->getMaxCombinedTextureImageUnits(); break; michael@0: case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = mRenderer->getMaxVertexTextureImageUnits(); break; michael@0: case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break; michael@0: case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = mRenderer->getMaxFragmentUniformVectors(); break; michael@0: case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break; michael@0: case GL_MAX_COLOR_ATTACHMENTS_EXT: *params = mRenderer->getMaxRenderTargets(); break; michael@0: case GL_MAX_DRAW_BUFFERS_EXT: *params = mRenderer->getMaxRenderTargets(); break; michael@0: case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break; michael@0: case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break; michael@0: case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break; michael@0: case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break; michael@0: //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE michael@0: case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break; michael@0: case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break; michael@0: case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break; michael@0: case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break; michael@0: case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break; michael@0: case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break; michael@0: case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break; michael@0: case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break; michael@0: case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break; michael@0: case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break; michael@0: case GL_STENCIL_FUNC: *params = mState.depthStencil.stencilFunc; break; michael@0: case GL_STENCIL_REF: *params = mState.stencilRef; break; michael@0: case GL_STENCIL_VALUE_MASK: *params = mState.depthStencil.stencilMask; break; michael@0: case GL_STENCIL_BACK_FUNC: *params = mState.depthStencil.stencilBackFunc; break; michael@0: case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break; michael@0: case GL_STENCIL_BACK_VALUE_MASK: *params = mState.depthStencil.stencilBackMask; break; michael@0: case GL_STENCIL_FAIL: *params = mState.depthStencil.stencilFail; break; michael@0: case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.depthStencil.stencilPassDepthFail; break; michael@0: case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.depthStencil.stencilPassDepthPass; break; michael@0: case GL_STENCIL_BACK_FAIL: *params = mState.depthStencil.stencilBackFail; break; michael@0: case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.depthStencil.stencilBackPassDepthFail; break; michael@0: case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.depthStencil.stencilBackPassDepthPass; break; michael@0: case GL_DEPTH_FUNC: *params = mState.depthStencil.depthFunc; break; michael@0: case GL_BLEND_SRC_RGB: *params = mState.blend.sourceBlendRGB; break; michael@0: case GL_BLEND_SRC_ALPHA: *params = mState.blend.sourceBlendAlpha; break; michael@0: case GL_BLEND_DST_RGB: *params = mState.blend.destBlendRGB; break; michael@0: case GL_BLEND_DST_ALPHA: *params = mState.blend.destBlendAlpha; break; michael@0: case GL_BLEND_EQUATION_RGB: *params = mState.blend.blendEquationRGB; break; michael@0: case GL_BLEND_EQUATION_ALPHA: *params = mState.blend.blendEquationAlpha; break; michael@0: case GL_STENCIL_WRITEMASK: *params = mState.depthStencil.stencilWritemask; break; michael@0: case GL_STENCIL_BACK_WRITEMASK: *params = mState.depthStencil.stencilBackWritemask; break; michael@0: case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break; michael@0: case GL_SUBPIXEL_BITS: *params = 4; break; michael@0: case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break; michael@0: case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break; michael@0: case GL_NUM_COMPRESSED_TEXTURE_FORMATS: michael@0: params[0] = mNumCompressedTextureFormats; michael@0: break; michael@0: case GL_MAX_SAMPLES_ANGLE: michael@0: { michael@0: GLsizei maxSamples = getMaxSupportedSamples(); michael@0: if (maxSamples != 0) michael@0: { michael@0: *params = maxSamples; michael@0: } michael@0: else michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: break; michael@0: } michael@0: case GL_SAMPLE_BUFFERS: michael@0: case GL_SAMPLES: michael@0: { michael@0: gl::Framebuffer *framebuffer = getDrawFramebuffer(); michael@0: if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE) michael@0: { michael@0: switch (pname) michael@0: { michael@0: case GL_SAMPLE_BUFFERS: michael@0: if (framebuffer->getSamples() != 0) michael@0: { michael@0: *params = 1; michael@0: } michael@0: else michael@0: { michael@0: *params = 0; michael@0: } michael@0: break; michael@0: case GL_SAMPLES: michael@0: *params = framebuffer->getSamples(); michael@0: break; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: *params = 0; michael@0: } michael@0: } michael@0: break; michael@0: case GL_IMPLEMENTATION_COLOR_READ_TYPE: michael@0: case GL_IMPLEMENTATION_COLOR_READ_FORMAT: michael@0: { michael@0: GLenum format, type; michael@0: if (getCurrentReadFormatType(&format, &type)) michael@0: { michael@0: if (pname == GL_IMPLEMENTATION_COLOR_READ_FORMAT) michael@0: *params = format; michael@0: else michael@0: *params = type; michael@0: } michael@0: } michael@0: break; michael@0: case GL_MAX_VIEWPORT_DIMS: michael@0: { michael@0: params[0] = mMaxViewportDimension; michael@0: params[1] = mMaxViewportDimension; michael@0: } michael@0: break; michael@0: case GL_COMPRESSED_TEXTURE_FORMATS: michael@0: { michael@0: if (supportsDXT1Textures()) michael@0: { michael@0: *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; michael@0: *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; michael@0: } michael@0: if (supportsDXT3Textures()) michael@0: { michael@0: *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE; michael@0: } michael@0: if (supportsDXT5Textures()) michael@0: { michael@0: *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE; michael@0: } michael@0: } michael@0: break; michael@0: case GL_VIEWPORT: michael@0: params[0] = mState.viewport.x; michael@0: params[1] = mState.viewport.y; michael@0: params[2] = mState.viewport.width; michael@0: params[3] = mState.viewport.height; michael@0: break; michael@0: case GL_SCISSOR_BOX: michael@0: params[0] = mState.scissor.x; michael@0: params[1] = mState.scissor.y; michael@0: params[2] = mState.scissor.width; michael@0: params[3] = mState.scissor.height; michael@0: break; michael@0: case GL_CULL_FACE_MODE: *params = mState.rasterizer.cullMode; break; michael@0: case GL_FRONT_FACE: *params = mState.rasterizer.frontFace; break; michael@0: case GL_RED_BITS: michael@0: case GL_GREEN_BITS: michael@0: case GL_BLUE_BITS: michael@0: case GL_ALPHA_BITS: michael@0: { michael@0: gl::Framebuffer *framebuffer = getDrawFramebuffer(); michael@0: gl::Renderbuffer *colorbuffer = framebuffer->getFirstColorbuffer(); michael@0: michael@0: if (colorbuffer) michael@0: { michael@0: switch (pname) michael@0: { michael@0: case GL_RED_BITS: *params = colorbuffer->getRedSize(); break; michael@0: case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break; michael@0: case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break; michael@0: case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: *params = 0; michael@0: } michael@0: } michael@0: break; michael@0: case GL_DEPTH_BITS: michael@0: { michael@0: gl::Framebuffer *framebuffer = getDrawFramebuffer(); michael@0: gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer(); michael@0: michael@0: if (depthbuffer) michael@0: { michael@0: *params = depthbuffer->getDepthSize(); michael@0: } michael@0: else michael@0: { michael@0: *params = 0; michael@0: } michael@0: } michael@0: break; michael@0: case GL_STENCIL_BITS: michael@0: { michael@0: gl::Framebuffer *framebuffer = getDrawFramebuffer(); michael@0: gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer(); michael@0: michael@0: if (stencilbuffer) michael@0: { michael@0: *params = stencilbuffer->getStencilSize(); michael@0: } michael@0: else michael@0: { michael@0: *params = 0; michael@0: } michael@0: } michael@0: break; michael@0: case GL_TEXTURE_BINDING_2D: michael@0: { michael@0: if (mState.activeSampler > mRenderer->getMaxCombinedTextureImageUnits() - 1) michael@0: { michael@0: gl::error(GL_INVALID_OPERATION); michael@0: return false; michael@0: } michael@0: michael@0: *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id(); michael@0: } michael@0: break; michael@0: case GL_TEXTURE_BINDING_CUBE_MAP: michael@0: { michael@0: if (mState.activeSampler > mRenderer->getMaxCombinedTextureImageUnits() - 1) michael@0: { michael@0: gl::error(GL_INVALID_OPERATION); michael@0: return false; michael@0: } michael@0: michael@0: *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id(); michael@0: } michael@0: break; michael@0: case GL_RESET_NOTIFICATION_STRATEGY_EXT: michael@0: *params = mResetStrategy; michael@0: break; michael@0: case GL_NUM_PROGRAM_BINARY_FORMATS_OES: michael@0: *params = 1; michael@0: break; michael@0: case GL_PROGRAM_BINARY_FORMATS_OES: michael@0: *params = GL_PROGRAM_BINARY_ANGLE; michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams) michael@0: { michael@0: if (pname >= GL_DRAW_BUFFER0_EXT && pname <= GL_DRAW_BUFFER15_EXT) michael@0: { michael@0: *type = GL_INT; michael@0: *numParams = 1; michael@0: return true; michael@0: } michael@0: michael@0: // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation michael@0: // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due michael@0: // to the fact that it is stored internally as a float, and so would require conversion michael@0: // if returned from Context::getIntegerv. Since this conversion is already implemented michael@0: // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we michael@0: // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling michael@0: // application. michael@0: switch (pname) michael@0: { michael@0: case GL_COMPRESSED_TEXTURE_FORMATS: michael@0: { michael@0: *type = GL_INT; michael@0: *numParams = mNumCompressedTextureFormats; michael@0: } michael@0: break; michael@0: case GL_SHADER_BINARY_FORMATS: michael@0: { michael@0: *type = GL_INT; michael@0: *numParams = 0; michael@0: } michael@0: break; michael@0: case GL_MAX_VERTEX_ATTRIBS: michael@0: case GL_MAX_VERTEX_UNIFORM_VECTORS: michael@0: case GL_MAX_VARYING_VECTORS: michael@0: case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: michael@0: case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: michael@0: case GL_MAX_TEXTURE_IMAGE_UNITS: michael@0: case GL_MAX_FRAGMENT_UNIFORM_VECTORS: michael@0: case GL_MAX_RENDERBUFFER_SIZE: michael@0: case GL_MAX_COLOR_ATTACHMENTS_EXT: michael@0: case GL_MAX_DRAW_BUFFERS_EXT: michael@0: case GL_NUM_SHADER_BINARY_FORMATS: michael@0: case GL_NUM_COMPRESSED_TEXTURE_FORMATS: michael@0: case GL_ARRAY_BUFFER_BINDING: michael@0: case GL_FRAMEBUFFER_BINDING: michael@0: case GL_RENDERBUFFER_BINDING: michael@0: case GL_CURRENT_PROGRAM: michael@0: case GL_PACK_ALIGNMENT: michael@0: case GL_PACK_REVERSE_ROW_ORDER_ANGLE: michael@0: case GL_UNPACK_ALIGNMENT: michael@0: case GL_GENERATE_MIPMAP_HINT: michael@0: case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: michael@0: case GL_RED_BITS: michael@0: case GL_GREEN_BITS: michael@0: case GL_BLUE_BITS: michael@0: case GL_ALPHA_BITS: michael@0: case GL_DEPTH_BITS: michael@0: case GL_STENCIL_BITS: michael@0: case GL_ELEMENT_ARRAY_BUFFER_BINDING: michael@0: case GL_CULL_FACE_MODE: michael@0: case GL_FRONT_FACE: michael@0: case GL_ACTIVE_TEXTURE: michael@0: case GL_STENCIL_FUNC: michael@0: case GL_STENCIL_VALUE_MASK: michael@0: case GL_STENCIL_REF: michael@0: case GL_STENCIL_FAIL: michael@0: case GL_STENCIL_PASS_DEPTH_FAIL: michael@0: case GL_STENCIL_PASS_DEPTH_PASS: michael@0: case GL_STENCIL_BACK_FUNC: michael@0: case GL_STENCIL_BACK_VALUE_MASK: michael@0: case GL_STENCIL_BACK_REF: michael@0: case GL_STENCIL_BACK_FAIL: michael@0: case GL_STENCIL_BACK_PASS_DEPTH_FAIL: michael@0: case GL_STENCIL_BACK_PASS_DEPTH_PASS: michael@0: case GL_DEPTH_FUNC: michael@0: case GL_BLEND_SRC_RGB: michael@0: case GL_BLEND_SRC_ALPHA: michael@0: case GL_BLEND_DST_RGB: michael@0: case GL_BLEND_DST_ALPHA: michael@0: case GL_BLEND_EQUATION_RGB: michael@0: case GL_BLEND_EQUATION_ALPHA: michael@0: case GL_STENCIL_WRITEMASK: michael@0: case GL_STENCIL_BACK_WRITEMASK: michael@0: case GL_STENCIL_CLEAR_VALUE: michael@0: case GL_SUBPIXEL_BITS: michael@0: case GL_MAX_TEXTURE_SIZE: michael@0: case GL_MAX_CUBE_MAP_TEXTURE_SIZE: michael@0: case GL_SAMPLE_BUFFERS: michael@0: case GL_SAMPLES: michael@0: case GL_IMPLEMENTATION_COLOR_READ_TYPE: michael@0: case GL_IMPLEMENTATION_COLOR_READ_FORMAT: michael@0: case GL_TEXTURE_BINDING_2D: michael@0: case GL_TEXTURE_BINDING_CUBE_MAP: michael@0: case GL_RESET_NOTIFICATION_STRATEGY_EXT: michael@0: case GL_NUM_PROGRAM_BINARY_FORMATS_OES: michael@0: case GL_PROGRAM_BINARY_FORMATS_OES: michael@0: { michael@0: *type = GL_INT; michael@0: *numParams = 1; michael@0: } michael@0: break; michael@0: case GL_MAX_SAMPLES_ANGLE: michael@0: { michael@0: if (getMaxSupportedSamples() != 0) michael@0: { michael@0: *type = GL_INT; michael@0: *numParams = 1; michael@0: } michael@0: else michael@0: { michael@0: return false; michael@0: } michael@0: } michael@0: break; michael@0: case GL_MAX_VIEWPORT_DIMS: michael@0: { michael@0: *type = GL_INT; michael@0: *numParams = 2; michael@0: } michael@0: break; michael@0: case GL_VIEWPORT: michael@0: case GL_SCISSOR_BOX: michael@0: { michael@0: *type = GL_INT; michael@0: *numParams = 4; michael@0: } michael@0: break; michael@0: case GL_SHADER_COMPILER: michael@0: case GL_SAMPLE_COVERAGE_INVERT: michael@0: case GL_DEPTH_WRITEMASK: michael@0: case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled, michael@0: case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries. michael@0: case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural michael@0: case GL_SAMPLE_COVERAGE: michael@0: case GL_SCISSOR_TEST: michael@0: case GL_STENCIL_TEST: michael@0: case GL_DEPTH_TEST: michael@0: case GL_BLEND: michael@0: case GL_DITHER: michael@0: case GL_CONTEXT_ROBUST_ACCESS_EXT: michael@0: { michael@0: *type = GL_BOOL; michael@0: *numParams = 1; michael@0: } michael@0: break; michael@0: case GL_COLOR_WRITEMASK: michael@0: { michael@0: *type = GL_BOOL; michael@0: *numParams = 4; michael@0: } michael@0: break; michael@0: case GL_POLYGON_OFFSET_FACTOR: michael@0: case GL_POLYGON_OFFSET_UNITS: michael@0: case GL_SAMPLE_COVERAGE_VALUE: michael@0: case GL_DEPTH_CLEAR_VALUE: michael@0: case GL_LINE_WIDTH: michael@0: { michael@0: *type = GL_FLOAT; michael@0: *numParams = 1; michael@0: } michael@0: break; michael@0: case GL_ALIASED_LINE_WIDTH_RANGE: michael@0: case GL_ALIASED_POINT_SIZE_RANGE: michael@0: case GL_DEPTH_RANGE: michael@0: { michael@0: *type = GL_FLOAT; michael@0: *numParams = 2; michael@0: } michael@0: break; michael@0: case GL_COLOR_CLEAR_VALUE: michael@0: case GL_BLEND_COLOR: michael@0: { michael@0: *type = GL_FLOAT; michael@0: *numParams = 4; michael@0: } michael@0: break; michael@0: case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT: michael@0: if (!supportsTextureFilterAnisotropy()) michael@0: { michael@0: return false; michael@0: } michael@0: *type = GL_FLOAT; michael@0: *numParams = 1; michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Applies the render target surface, depth stencil surface, viewport rectangle and michael@0: // scissor rectangle to the renderer michael@0: bool Context::applyRenderTarget(GLenum drawMode, bool ignoreViewport) michael@0: { michael@0: Framebuffer *framebufferObject = getDrawFramebuffer(); michael@0: michael@0: if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE) michael@0: { michael@0: return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION, false); michael@0: } michael@0: michael@0: mRenderer->applyRenderTarget(framebufferObject); michael@0: michael@0: if (!mRenderer->setViewport(mState.viewport, mState.zNear, mState.zFar, drawMode, mState.rasterizer.frontFace, michael@0: ignoreViewport)) michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: mRenderer->setScissorRectangle(mState.scissor, mState.scissorTest); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device michael@0: void Context::applyState(GLenum drawMode) michael@0: { michael@0: Framebuffer *framebufferObject = getDrawFramebuffer(); michael@0: int samples = framebufferObject->getSamples(); michael@0: michael@0: mState.rasterizer.pointDrawMode = (drawMode == GL_POINTS); michael@0: mState.rasterizer.multiSample = (samples != 0); michael@0: mRenderer->setRasterizerState(mState.rasterizer); michael@0: michael@0: unsigned int mask = 0; michael@0: if (mState.sampleCoverage) michael@0: { michael@0: if (mState.sampleCoverageValue != 0) michael@0: { michael@0: michael@0: float threshold = 0.5f; michael@0: michael@0: for (int i = 0; i < samples; ++i) michael@0: { michael@0: mask <<= 1; michael@0: michael@0: if ((i + 1) * mState.sampleCoverageValue >= threshold) michael@0: { michael@0: threshold += 1.0f; michael@0: mask |= 1; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (mState.sampleCoverageInvert) michael@0: { michael@0: mask = ~mask; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: mask = 0xFFFFFFFF; michael@0: } michael@0: mRenderer->setBlendState(mState.blend, mState.blendColor, mask); michael@0: michael@0: mRenderer->setDepthStencilState(mState.depthStencil, mState.stencilRef, mState.stencilBackRef, michael@0: mState.rasterizer.frontFace == GL_CCW); michael@0: } michael@0: michael@0: // Applies the shaders and shader constants to the Direct3D 9 device michael@0: void Context::applyShaders() michael@0: { michael@0: ProgramBinary *programBinary = getCurrentProgramBinary(); michael@0: michael@0: mRenderer->applyShaders(programBinary); michael@0: michael@0: programBinary->applyUniforms(); michael@0: } michael@0: michael@0: // Applies the textures and sampler states to the Direct3D 9 device michael@0: void Context::applyTextures() michael@0: { michael@0: applyTextures(SAMPLER_PIXEL); michael@0: michael@0: if (mSupportsVertexTexture) michael@0: { michael@0: applyTextures(SAMPLER_VERTEX); michael@0: } michael@0: } michael@0: michael@0: // For each Direct3D 9 sampler of either the pixel or vertex stage, michael@0: // looks up the corresponding OpenGL texture image unit and texture type, michael@0: // and sets the texture and its addressing/filtering state (or NULL when inactive). michael@0: void Context::applyTextures(SamplerType type) michael@0: { michael@0: ProgramBinary *programBinary = getCurrentProgramBinary(); michael@0: michael@0: // Range of Direct3D samplers of given sampler type michael@0: int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : mRenderer->getMaxVertexTextureImageUnits(); michael@0: int samplerRange = programBinary->getUsedSamplerRange(type); michael@0: michael@0: for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++) michael@0: { michael@0: int textureUnit = programBinary->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index michael@0: michael@0: if (textureUnit != -1) michael@0: { michael@0: TextureType textureType = programBinary->getSamplerTextureType(type, samplerIndex); michael@0: Texture *texture = getSamplerTexture(textureUnit, textureType); michael@0: michael@0: if (texture->isSamplerComplete()) michael@0: { michael@0: SamplerState samplerState; michael@0: texture->getSamplerState(&samplerState); michael@0: mRenderer->setSamplerState(type, samplerIndex, samplerState); michael@0: michael@0: mRenderer->setTexture(type, samplerIndex, texture); michael@0: michael@0: texture->resetDirty(); michael@0: } michael@0: else michael@0: { michael@0: mRenderer->setTexture(type, samplerIndex, getIncompleteTexture(textureType)); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: mRenderer->setTexture(type, samplerIndex, NULL); michael@0: } michael@0: } michael@0: michael@0: for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++) michael@0: { michael@0: mRenderer->setTexture(type, samplerIndex, NULL); michael@0: } michael@0: } michael@0: michael@0: void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, michael@0: GLenum format, GLenum type, GLsizei *bufSize, void* pixels) michael@0: { michael@0: Framebuffer *framebuffer = getReadFramebuffer(); michael@0: michael@0: if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE) michael@0: { michael@0: return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION); michael@0: } michael@0: michael@0: if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: GLsizei outputPitch = ComputePitch(width, ConvertSizedInternalFormat(format, type), getPackAlignment()); michael@0: // sized query sanity check michael@0: if (bufSize) michael@0: { michael@0: int requiredSize = outputPitch * height; michael@0: if (requiredSize > *bufSize) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: } michael@0: michael@0: mRenderer->readPixels(framebuffer, x, y, width, height, format, type, outputPitch, getPackReverseRowOrder(), getPackAlignment(), pixels); michael@0: } michael@0: michael@0: void Context::clear(GLbitfield mask) michael@0: { michael@0: Framebuffer *framebufferObject = getDrawFramebuffer(); michael@0: michael@0: if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE) michael@0: { michael@0: return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION); michael@0: } michael@0: michael@0: DWORD flags = 0; michael@0: GLbitfield finalMask = 0; michael@0: michael@0: if (mask & GL_COLOR_BUFFER_BIT) michael@0: { michael@0: mask &= ~GL_COLOR_BUFFER_BIT; michael@0: michael@0: if (framebufferObject->hasEnabledColorAttachment()) michael@0: { michael@0: finalMask |= GL_COLOR_BUFFER_BIT; michael@0: } michael@0: } michael@0: michael@0: if (mask & GL_DEPTH_BUFFER_BIT) michael@0: { michael@0: mask &= ~GL_DEPTH_BUFFER_BIT; michael@0: if (mState.depthStencil.depthMask && framebufferObject->getDepthbufferType() != GL_NONE) michael@0: { michael@0: finalMask |= GL_DEPTH_BUFFER_BIT; michael@0: } michael@0: } michael@0: michael@0: if (mask & GL_STENCIL_BUFFER_BIT) michael@0: { michael@0: mask &= ~GL_STENCIL_BUFFER_BIT; michael@0: if (framebufferObject->getStencilbufferType() != GL_NONE) michael@0: { michael@0: rx::RenderTarget *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil(); michael@0: if (!depthStencil) michael@0: { michael@0: ERR("Depth stencil pointer unexpectedly null."); michael@0: return; michael@0: } michael@0: michael@0: if (GetStencilSize(depthStencil->getActualFormat()) > 0) michael@0: { michael@0: finalMask |= GL_STENCIL_BUFFER_BIT; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (mask != 0) michael@0: { michael@0: return gl::error(GL_INVALID_VALUE); michael@0: } michael@0: michael@0: if (!applyRenderTarget(GL_TRIANGLES, true)) // Clips the clear to the scissor rectangle but not the viewport michael@0: { michael@0: return; michael@0: } michael@0: michael@0: ClearParameters clearParams; michael@0: clearParams.mask = finalMask; michael@0: clearParams.colorClearValue = mState.colorClearValue; michael@0: clearParams.colorMaskRed = mState.blend.colorMaskRed; michael@0: clearParams.colorMaskGreen = mState.blend.colorMaskGreen; michael@0: clearParams.colorMaskBlue = mState.blend.colorMaskBlue; michael@0: clearParams.colorMaskAlpha = mState.blend.colorMaskAlpha; michael@0: clearParams.depthClearValue = mState.depthClearValue; michael@0: clearParams.stencilClearValue = mState.stencilClearValue; michael@0: clearParams.stencilWriteMask = mState.depthStencil.stencilWritemask; michael@0: michael@0: mRenderer->clear(clearParams, framebufferObject); michael@0: } michael@0: michael@0: void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances) michael@0: { michael@0: if (!mState.currentProgram) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: if (!mRenderer->applyPrimitiveType(mode, count)) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: if (!applyRenderTarget(mode, false)) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: applyState(mode); michael@0: michael@0: ProgramBinary *programBinary = getCurrentProgramBinary(); michael@0: michael@0: GLenum err = mRenderer->applyVertexBuffer(programBinary, mState.vertexAttribute, first, count, instances); michael@0: if (err != GL_NO_ERROR) michael@0: { michael@0: return gl::error(err); michael@0: } michael@0: michael@0: applyShaders(); michael@0: applyTextures(); michael@0: michael@0: if (!programBinary->validateSamplers(NULL)) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: if (!skipDraw(mode)) michael@0: { michael@0: mRenderer->drawArrays(mode, count, instances); michael@0: } michael@0: } michael@0: michael@0: void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances) michael@0: { michael@0: if (!mState.currentProgram) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: if (!indices && !mState.elementArrayBuffer) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: if (!mRenderer->applyPrimitiveType(mode, count)) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: if (!applyRenderTarget(mode, false)) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: applyState(mode); michael@0: michael@0: rx::TranslatedIndexData indexInfo; michael@0: GLenum err = mRenderer->applyIndexBuffer(indices, mState.elementArrayBuffer.get(), count, mode, type, &indexInfo); michael@0: if (err != GL_NO_ERROR) michael@0: { michael@0: return gl::error(err); michael@0: } michael@0: michael@0: ProgramBinary *programBinary = getCurrentProgramBinary(); michael@0: michael@0: GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1; michael@0: err = mRenderer->applyVertexBuffer(programBinary, mState.vertexAttribute, indexInfo.minIndex, vertexCount, instances); michael@0: if (err != GL_NO_ERROR) michael@0: { michael@0: return gl::error(err); michael@0: } michael@0: michael@0: applyShaders(); michael@0: applyTextures(); michael@0: michael@0: if (!programBinary->validateSamplers(NULL)) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: if (!skipDraw(mode)) michael@0: { michael@0: mRenderer->drawElements(mode, count, type, indices, mState.elementArrayBuffer.get(), indexInfo, instances); michael@0: } michael@0: } michael@0: michael@0: // Implements glFlush when block is false, glFinish when block is true michael@0: void Context::sync(bool block) michael@0: { michael@0: mRenderer->sync(block); michael@0: } michael@0: michael@0: void Context::recordInvalidEnum() michael@0: { michael@0: mInvalidEnum = true; michael@0: } michael@0: michael@0: void Context::recordInvalidValue() michael@0: { michael@0: mInvalidValue = true; michael@0: } michael@0: michael@0: void Context::recordInvalidOperation() michael@0: { michael@0: mInvalidOperation = true; michael@0: } michael@0: michael@0: void Context::recordOutOfMemory() michael@0: { michael@0: mOutOfMemory = true; michael@0: } michael@0: michael@0: void Context::recordInvalidFramebufferOperation() michael@0: { michael@0: mInvalidFramebufferOperation = true; michael@0: } michael@0: michael@0: // Get one of the recorded errors and clear its flag, if any. michael@0: // [OpenGL ES 2.0.24] section 2.5 page 13. michael@0: GLenum Context::getError() michael@0: { michael@0: if (mInvalidEnum) michael@0: { michael@0: mInvalidEnum = false; michael@0: michael@0: return GL_INVALID_ENUM; michael@0: } michael@0: michael@0: if (mInvalidValue) michael@0: { michael@0: mInvalidValue = false; michael@0: michael@0: return GL_INVALID_VALUE; michael@0: } michael@0: michael@0: if (mInvalidOperation) michael@0: { michael@0: mInvalidOperation = false; michael@0: michael@0: return GL_INVALID_OPERATION; michael@0: } michael@0: michael@0: if (mOutOfMemory) michael@0: { michael@0: mOutOfMemory = false; michael@0: michael@0: return GL_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: if (mInvalidFramebufferOperation) michael@0: { michael@0: mInvalidFramebufferOperation = false; michael@0: michael@0: return GL_INVALID_FRAMEBUFFER_OPERATION; michael@0: } michael@0: michael@0: return GL_NO_ERROR; michael@0: } michael@0: michael@0: GLenum Context::getResetStatus() michael@0: { michael@0: if (mResetStatus == GL_NO_ERROR && !mContextLost) michael@0: { michael@0: // mResetStatus will be set by the markContextLost callback michael@0: // in the case a notification is sent michael@0: mRenderer->testDeviceLost(true); michael@0: } michael@0: michael@0: GLenum status = mResetStatus; michael@0: michael@0: if (mResetStatus != GL_NO_ERROR) michael@0: { michael@0: ASSERT(mContextLost); michael@0: michael@0: if (mRenderer->testDeviceResettable()) michael@0: { michael@0: mResetStatus = GL_NO_ERROR; michael@0: } michael@0: } michael@0: michael@0: return status; michael@0: } michael@0: michael@0: bool Context::isResetNotificationEnabled() michael@0: { michael@0: return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT); michael@0: } michael@0: michael@0: int Context::getMajorShaderModel() const michael@0: { michael@0: return mMajorShaderModel; michael@0: } michael@0: michael@0: float Context::getMaximumPointSize() const michael@0: { michael@0: return mMaximumPointSize; michael@0: } michael@0: michael@0: unsigned int Context::getMaximumCombinedTextureImageUnits() const michael@0: { michael@0: return mRenderer->getMaxCombinedTextureImageUnits(); michael@0: } michael@0: michael@0: int Context::getMaxSupportedSamples() const michael@0: { michael@0: return mRenderer->getMaxSupportedSamples(); michael@0: } michael@0: michael@0: unsigned int Context::getMaximumRenderTargets() const michael@0: { michael@0: return mRenderer->getMaxRenderTargets(); michael@0: } michael@0: michael@0: bool Context::supportsEventQueries() const michael@0: { michael@0: return mSupportsEventQueries; michael@0: } michael@0: michael@0: bool Context::supportsOcclusionQueries() const michael@0: { michael@0: return mSupportsOcclusionQueries; michael@0: } michael@0: michael@0: bool Context::supportsBGRATextures() const michael@0: { michael@0: return mSupportsBGRATextures; michael@0: } michael@0: michael@0: bool Context::supportsDXT1Textures() const michael@0: { michael@0: return mSupportsDXT1Textures; michael@0: } michael@0: michael@0: bool Context::supportsDXT3Textures() const michael@0: { michael@0: return mSupportsDXT3Textures; michael@0: } michael@0: michael@0: bool Context::supportsDXT5Textures() const michael@0: { michael@0: return mSupportsDXT5Textures; michael@0: } michael@0: michael@0: bool Context::supportsFloat32Textures() const michael@0: { michael@0: return mSupportsFloat32Textures; michael@0: } michael@0: michael@0: bool Context::supportsFloat32LinearFilter() const michael@0: { michael@0: return mSupportsFloat32LinearFilter; michael@0: } michael@0: michael@0: bool Context::supportsFloat32RenderableTextures() const michael@0: { michael@0: return mSupportsFloat32RenderableTextures; michael@0: } michael@0: michael@0: bool Context::supportsFloat16Textures() const michael@0: { michael@0: return mSupportsFloat16Textures; michael@0: } michael@0: michael@0: bool Context::supportsFloat16LinearFilter() const michael@0: { michael@0: return mSupportsFloat16LinearFilter; michael@0: } michael@0: michael@0: bool Context::supportsFloat16RenderableTextures() const michael@0: { michael@0: return mSupportsFloat16RenderableTextures; michael@0: } michael@0: michael@0: int Context::getMaximumRenderbufferDimension() const michael@0: { michael@0: return mMaxRenderbufferDimension; michael@0: } michael@0: michael@0: int Context::getMaximumTextureDimension() const michael@0: { michael@0: return mMaxTextureDimension; michael@0: } michael@0: michael@0: int Context::getMaximumCubeTextureDimension() const michael@0: { michael@0: return mMaxCubeTextureDimension; michael@0: } michael@0: michael@0: int Context::getMaximumTextureLevel() const michael@0: { michael@0: return mMaxTextureLevel; michael@0: } michael@0: michael@0: bool Context::supportsLuminanceTextures() const michael@0: { michael@0: return mSupportsLuminanceTextures; michael@0: } michael@0: michael@0: bool Context::supportsLuminanceAlphaTextures() const michael@0: { michael@0: return mSupportsLuminanceAlphaTextures; michael@0: } michael@0: michael@0: bool Context::supportsDepthTextures() const michael@0: { michael@0: return mSupportsDepthTextures; michael@0: } michael@0: michael@0: bool Context::supports32bitIndices() const michael@0: { michael@0: return mSupports32bitIndices; michael@0: } michael@0: michael@0: bool Context::supportsNonPower2Texture() const michael@0: { michael@0: return mSupportsNonPower2Texture; michael@0: } michael@0: michael@0: bool Context::supportsInstancing() const michael@0: { michael@0: return mSupportsInstancing; michael@0: } michael@0: michael@0: bool Context::supportsTextureFilterAnisotropy() const michael@0: { michael@0: return mSupportsTextureFilterAnisotropy; michael@0: } michael@0: michael@0: float Context::getTextureMaxAnisotropy() const michael@0: { michael@0: return mMaxTextureAnisotropy; michael@0: } michael@0: michael@0: bool Context::getCurrentReadFormatType(GLenum *format, GLenum *type) michael@0: { michael@0: Framebuffer *framebuffer = getReadFramebuffer(); michael@0: if (!framebuffer || framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION, false); michael@0: } michael@0: michael@0: Renderbuffer *renderbuffer = framebuffer->getReadColorbuffer(); michael@0: if (!renderbuffer) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION, false); michael@0: } michael@0: michael@0: *format = gl::ExtractFormat(renderbuffer->getActualFormat()); michael@0: *type = gl::ExtractType(renderbuffer->getActualFormat()); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void Context::detachBuffer(GLuint buffer) michael@0: { michael@0: // [OpenGL ES 2.0.24] section 2.9 page 22: michael@0: // If a buffer object is deleted while it is bound, all bindings to that object in the current context michael@0: // (i.e. in the thread that called Delete-Buffers) are reset to zero. michael@0: michael@0: if (mState.arrayBuffer.id() == buffer) michael@0: { michael@0: mState.arrayBuffer.set(NULL); michael@0: } michael@0: michael@0: if (mState.elementArrayBuffer.id() == buffer) michael@0: { michael@0: mState.elementArrayBuffer.set(NULL); michael@0: } michael@0: michael@0: for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++) michael@0: { michael@0: if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer) michael@0: { michael@0: mState.vertexAttribute[attribute].mBoundBuffer.set(NULL); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Context::detachTexture(GLuint texture) michael@0: { michael@0: // [OpenGL ES 2.0.24] section 3.8 page 84: michael@0: // If a texture object is deleted, it is as if all texture units which are bound to that texture object are michael@0: // rebound to texture object zero michael@0: michael@0: for (int type = 0; type < TEXTURE_TYPE_COUNT; type++) michael@0: { michael@0: for (int sampler = 0; sampler < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; sampler++) michael@0: { michael@0: if (mState.samplerTexture[type][sampler].id() == texture) michael@0: { michael@0: mState.samplerTexture[type][sampler].set(NULL); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // [OpenGL ES 2.0.24] section 4.4 page 112: michael@0: // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is michael@0: // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this michael@0: // image was attached in the currently bound framebuffer. michael@0: michael@0: Framebuffer *readFramebuffer = getReadFramebuffer(); michael@0: Framebuffer *drawFramebuffer = getDrawFramebuffer(); michael@0: michael@0: if (readFramebuffer) michael@0: { michael@0: readFramebuffer->detachTexture(texture); michael@0: } michael@0: michael@0: if (drawFramebuffer && drawFramebuffer != readFramebuffer) michael@0: { michael@0: drawFramebuffer->detachTexture(texture); michael@0: } michael@0: } michael@0: michael@0: void Context::detachFramebuffer(GLuint framebuffer) michael@0: { michael@0: // [OpenGL ES 2.0.24] section 4.4 page 107: michael@0: // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though michael@0: // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero. michael@0: michael@0: if (mState.readFramebuffer == framebuffer) michael@0: { michael@0: bindReadFramebuffer(0); michael@0: } michael@0: michael@0: if (mState.drawFramebuffer == framebuffer) michael@0: { michael@0: bindDrawFramebuffer(0); michael@0: } michael@0: } michael@0: michael@0: void Context::detachRenderbuffer(GLuint renderbuffer) michael@0: { michael@0: // [OpenGL ES 2.0.24] section 4.4 page 109: michael@0: // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer michael@0: // had been executed with the target RENDERBUFFER and name of zero. michael@0: michael@0: if (mState.renderbuffer.id() == renderbuffer) michael@0: { michael@0: bindRenderbuffer(0); michael@0: } michael@0: michael@0: // [OpenGL ES 2.0.24] section 4.4 page 111: michael@0: // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer, michael@0: // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment michael@0: // point to which this image was attached in the currently bound framebuffer. michael@0: michael@0: Framebuffer *readFramebuffer = getReadFramebuffer(); michael@0: Framebuffer *drawFramebuffer = getDrawFramebuffer(); michael@0: michael@0: if (readFramebuffer) michael@0: { michael@0: readFramebuffer->detachRenderbuffer(renderbuffer); michael@0: } michael@0: michael@0: if (drawFramebuffer && drawFramebuffer != readFramebuffer) michael@0: { michael@0: drawFramebuffer->detachRenderbuffer(renderbuffer); michael@0: } michael@0: } michael@0: michael@0: Texture *Context::getIncompleteTexture(TextureType type) michael@0: { michael@0: Texture *t = mIncompleteTextures[type].get(); michael@0: michael@0: if (t == NULL) michael@0: { michael@0: static const GLubyte color[] = { 0, 0, 0, 255 }; michael@0: michael@0: switch (type) michael@0: { michael@0: default: michael@0: UNREACHABLE(); michael@0: // default falls through to TEXTURE_2D michael@0: michael@0: case TEXTURE_2D: michael@0: { michael@0: Texture2D *incomplete2d = new Texture2D(mRenderer, Texture::INCOMPLETE_TEXTURE_ID); michael@0: incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); michael@0: t = incomplete2d; michael@0: } michael@0: break; michael@0: michael@0: case TEXTURE_CUBE: michael@0: { michael@0: TextureCubeMap *incompleteCube = new TextureCubeMap(mRenderer, Texture::INCOMPLETE_TEXTURE_ID); michael@0: michael@0: incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); michael@0: incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); michael@0: incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); michael@0: incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); michael@0: incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); michael@0: incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color); michael@0: michael@0: t = incompleteCube; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: mIncompleteTextures[type].set(t); michael@0: } michael@0: michael@0: return t; michael@0: } michael@0: michael@0: bool Context::skipDraw(GLenum drawMode) michael@0: { michael@0: if (drawMode == GL_POINTS) michael@0: { michael@0: // ProgramBinary assumes non-point rendering if gl_PointSize isn't written, michael@0: // which affects varying interpolation. Since the value of gl_PointSize is michael@0: // undefined when not written, just skip drawing to avoid unexpected results. michael@0: if (!getCurrentProgramBinary()->usesPointSize()) michael@0: { michael@0: // This is stictly speaking not an error, but developers should be michael@0: // notified of risking undefined behavior. michael@0: ERR("Point rendering without writing to gl_PointSize."); michael@0: michael@0: return true; michael@0: } michael@0: } michael@0: else if (IsTriangleMode(drawMode)) michael@0: { michael@0: if (mState.rasterizer.cullFace && mState.rasterizer.cullMode == GL_FRONT_AND_BACK) michael@0: { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void Context::setVertexAttrib(GLuint index, const GLfloat *values) michael@0: { michael@0: ASSERT(index < gl::MAX_VERTEX_ATTRIBS); michael@0: michael@0: mState.vertexAttribute[index].mCurrentValue[0] = values[0]; michael@0: mState.vertexAttribute[index].mCurrentValue[1] = values[1]; michael@0: mState.vertexAttribute[index].mCurrentValue[2] = values[2]; michael@0: mState.vertexAttribute[index].mCurrentValue[3] = values[3]; michael@0: } michael@0: michael@0: void Context::setVertexAttribDivisor(GLuint index, GLuint divisor) michael@0: { michael@0: ASSERT(index < gl::MAX_VERTEX_ATTRIBS); michael@0: michael@0: mState.vertexAttribute[index].mDivisor = divisor; michael@0: } michael@0: michael@0: // keep list sorted in following order michael@0: // OES extensions michael@0: // EXT extensions michael@0: // Vendor extensions michael@0: void Context::initExtensionString() michael@0: { michael@0: std::string extensionString = ""; michael@0: michael@0: // OES extensions michael@0: if (supports32bitIndices()) michael@0: { michael@0: extensionString += "GL_OES_element_index_uint "; michael@0: } michael@0: michael@0: extensionString += "GL_OES_packed_depth_stencil "; michael@0: extensionString += "GL_OES_get_program_binary "; michael@0: extensionString += "GL_OES_rgb8_rgba8 "; michael@0: if (mRenderer->getDerivativeInstructionSupport()) michael@0: { michael@0: extensionString += "GL_OES_standard_derivatives "; michael@0: } michael@0: michael@0: if (supportsFloat16Textures()) michael@0: { michael@0: extensionString += "GL_OES_texture_half_float "; michael@0: } michael@0: if (supportsFloat16LinearFilter()) michael@0: { michael@0: extensionString += "GL_OES_texture_half_float_linear "; michael@0: } michael@0: if (supportsFloat32Textures()) michael@0: { michael@0: extensionString += "GL_OES_texture_float "; michael@0: } michael@0: if (supportsFloat32LinearFilter()) michael@0: { michael@0: extensionString += "GL_OES_texture_float_linear "; michael@0: } michael@0: michael@0: if (supportsNonPower2Texture()) michael@0: { michael@0: extensionString += "GL_OES_texture_npot "; michael@0: } michael@0: michael@0: // Multi-vendor (EXT) extensions michael@0: if (supportsOcclusionQueries()) michael@0: { michael@0: extensionString += "GL_EXT_occlusion_query_boolean "; michael@0: } michael@0: michael@0: extensionString += "GL_EXT_read_format_bgra "; michael@0: extensionString += "GL_EXT_robustness "; michael@0: michael@0: if (supportsDXT1Textures()) michael@0: { michael@0: extensionString += "GL_EXT_texture_compression_dxt1 "; michael@0: } michael@0: michael@0: if (supportsTextureFilterAnisotropy()) michael@0: { michael@0: extensionString += "GL_EXT_texture_filter_anisotropic "; michael@0: } michael@0: michael@0: if (supportsBGRATextures()) michael@0: { michael@0: extensionString += "GL_EXT_texture_format_BGRA8888 "; michael@0: } michael@0: michael@0: if (mRenderer->getMaxRenderTargets() > 1) michael@0: { michael@0: extensionString += "GL_EXT_draw_buffers "; michael@0: } michael@0: michael@0: extensionString += "GL_EXT_texture_storage "; michael@0: extensionString += "GL_EXT_frag_depth "; michael@0: michael@0: // ANGLE-specific extensions michael@0: if (supportsDepthTextures()) michael@0: { michael@0: extensionString += "GL_ANGLE_depth_texture "; michael@0: } michael@0: michael@0: extensionString += "GL_ANGLE_framebuffer_blit "; michael@0: if (getMaxSupportedSamples() != 0) michael@0: { michael@0: extensionString += "GL_ANGLE_framebuffer_multisample "; michael@0: } michael@0: michael@0: if (supportsInstancing()) michael@0: { michael@0: extensionString += "GL_ANGLE_instanced_arrays "; michael@0: } michael@0: michael@0: extensionString += "GL_ANGLE_pack_reverse_row_order "; michael@0: michael@0: if (supportsDXT3Textures()) michael@0: { michael@0: extensionString += "GL_ANGLE_texture_compression_dxt3 "; michael@0: } michael@0: if (supportsDXT5Textures()) michael@0: { michael@0: extensionString += "GL_ANGLE_texture_compression_dxt5 "; michael@0: } michael@0: michael@0: extensionString += "GL_ANGLE_texture_usage "; michael@0: extensionString += "GL_ANGLE_translated_shader_source "; michael@0: michael@0: // Other vendor-specific extensions michael@0: if (supportsEventQueries()) michael@0: { michael@0: extensionString += "GL_NV_fence "; michael@0: } michael@0: michael@0: std::string::size_type end = extensionString.find_last_not_of(' '); michael@0: if (end != std::string::npos) michael@0: { michael@0: extensionString.resize(end+1); michael@0: } michael@0: michael@0: mExtensionString = makeStaticString(extensionString); michael@0: } michael@0: michael@0: const char *Context::getExtensionString() const michael@0: { michael@0: return mExtensionString; michael@0: } michael@0: michael@0: void Context::initRendererString() michael@0: { michael@0: std::ostringstream rendererString; michael@0: rendererString << "ANGLE ("; michael@0: rendererString << mRenderer->getRendererDescription(); michael@0: rendererString << ")"; michael@0: michael@0: mRendererString = makeStaticString(rendererString.str()); michael@0: } michael@0: michael@0: const char *Context::getRendererString() const michael@0: { michael@0: return mRendererString; michael@0: } michael@0: michael@0: void Context::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: Framebuffer *readFramebuffer = getReadFramebuffer(); michael@0: Framebuffer *drawFramebuffer = getDrawFramebuffer(); michael@0: michael@0: if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE || michael@0: !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE) michael@0: { michael@0: return gl::error(GL_INVALID_FRAMEBUFFER_OPERATION); michael@0: } michael@0: michael@0: if (drawFramebuffer->getSamples() != 0) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: Renderbuffer *readColorBuffer = readFramebuffer->getReadColorbuffer(); michael@0: Renderbuffer *drawColorBuffer = drawFramebuffer->getFirstColorbuffer(); michael@0: michael@0: if (drawColorBuffer == NULL) michael@0: { michael@0: ERR("Draw buffers formats don't match, which is not supported in this implementation of BlitFramebufferANGLE"); michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: int readBufferWidth = readColorBuffer->getWidth(); michael@0: int readBufferHeight = readColorBuffer->getHeight(); michael@0: int drawBufferWidth = drawColorBuffer->getWidth(); michael@0: int drawBufferHeight = drawColorBuffer->getHeight(); michael@0: michael@0: Rectangle sourceRect; michael@0: Rectangle destRect; michael@0: michael@0: if (srcX0 < srcX1) michael@0: { michael@0: sourceRect.x = srcX0; michael@0: destRect.x = dstX0; michael@0: sourceRect.width = srcX1 - srcX0; michael@0: destRect.width = dstX1 - dstX0; michael@0: } michael@0: else michael@0: { michael@0: sourceRect.x = srcX1; michael@0: destRect.x = dstX1; michael@0: sourceRect.width = srcX0 - srcX1; michael@0: destRect.width = dstX0 - dstX1; michael@0: } michael@0: michael@0: if (srcY0 < srcY1) michael@0: { michael@0: sourceRect.height = srcY1 - srcY0; michael@0: destRect.height = dstY1 - dstY0; michael@0: sourceRect.y = srcY0; michael@0: destRect.y = dstY0; michael@0: } michael@0: else michael@0: { michael@0: sourceRect.height = srcY0 - srcY1; michael@0: destRect.height = dstY0 - srcY1; michael@0: sourceRect.y = srcY1; michael@0: destRect.y = dstY1; michael@0: } michael@0: michael@0: Rectangle sourceScissoredRect = sourceRect; michael@0: Rectangle destScissoredRect = destRect; michael@0: michael@0: if (mState.scissorTest) michael@0: { michael@0: // Only write to parts of the destination framebuffer which pass the scissor test. michael@0: if (destRect.x < mState.scissor.x) michael@0: { michael@0: int xDiff = mState.scissor.x - destRect.x; michael@0: destScissoredRect.x = mState.scissor.x; michael@0: destScissoredRect.width -= xDiff; michael@0: sourceScissoredRect.x += xDiff; michael@0: sourceScissoredRect.width -= xDiff; michael@0: michael@0: } michael@0: michael@0: if (destRect.x + destRect.width > mState.scissor.x + mState.scissor.width) michael@0: { michael@0: int xDiff = (destRect.x + destRect.width) - (mState.scissor.x + mState.scissor.width); michael@0: destScissoredRect.width -= xDiff; michael@0: sourceScissoredRect.width -= xDiff; michael@0: } michael@0: michael@0: if (destRect.y < mState.scissor.y) michael@0: { michael@0: int yDiff = mState.scissor.y - destRect.y; michael@0: destScissoredRect.y = mState.scissor.y; michael@0: destScissoredRect.height -= yDiff; michael@0: sourceScissoredRect.y += yDiff; michael@0: sourceScissoredRect.height -= yDiff; michael@0: } michael@0: michael@0: if (destRect.y + destRect.height > mState.scissor.y + mState.scissor.height) michael@0: { michael@0: int yDiff = (destRect.y + destRect.height) - (mState.scissor.y + mState.scissor.height); michael@0: destScissoredRect.height -= yDiff; michael@0: sourceScissoredRect.height -= yDiff; michael@0: } michael@0: } michael@0: michael@0: bool blitRenderTarget = false; michael@0: bool blitDepthStencil = false; michael@0: michael@0: Rectangle sourceTrimmedRect = sourceScissoredRect; michael@0: Rectangle destTrimmedRect = destScissoredRect; michael@0: michael@0: // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of michael@0: // the actual draw and read surfaces. michael@0: if (sourceTrimmedRect.x < 0) michael@0: { michael@0: int xDiff = 0 - sourceTrimmedRect.x; michael@0: sourceTrimmedRect.x = 0; michael@0: sourceTrimmedRect.width -= xDiff; michael@0: destTrimmedRect.x += xDiff; michael@0: destTrimmedRect.width -= xDiff; michael@0: } michael@0: michael@0: if (sourceTrimmedRect.x + sourceTrimmedRect.width > readBufferWidth) michael@0: { michael@0: int xDiff = (sourceTrimmedRect.x + sourceTrimmedRect.width) - readBufferWidth; michael@0: sourceTrimmedRect.width -= xDiff; michael@0: destTrimmedRect.width -= xDiff; michael@0: } michael@0: michael@0: if (sourceTrimmedRect.y < 0) michael@0: { michael@0: int yDiff = 0 - sourceTrimmedRect.y; michael@0: sourceTrimmedRect.y = 0; michael@0: sourceTrimmedRect.height -= yDiff; michael@0: destTrimmedRect.y += yDiff; michael@0: destTrimmedRect.height -= yDiff; michael@0: } michael@0: michael@0: if (sourceTrimmedRect.y + sourceTrimmedRect.height > readBufferHeight) michael@0: { michael@0: int yDiff = (sourceTrimmedRect.y + sourceTrimmedRect.height) - readBufferHeight; michael@0: sourceTrimmedRect.height -= yDiff; michael@0: destTrimmedRect.height -= yDiff; michael@0: } michael@0: michael@0: if (destTrimmedRect.x < 0) michael@0: { michael@0: int xDiff = 0 - destTrimmedRect.x; michael@0: destTrimmedRect.x = 0; michael@0: destTrimmedRect.width -= xDiff; michael@0: sourceTrimmedRect.x += xDiff; michael@0: sourceTrimmedRect.width -= xDiff; michael@0: } michael@0: michael@0: if (destTrimmedRect.x + destTrimmedRect.width > drawBufferWidth) michael@0: { michael@0: int xDiff = (destTrimmedRect.x + destTrimmedRect.width) - drawBufferWidth; michael@0: destTrimmedRect.width -= xDiff; michael@0: sourceTrimmedRect.width -= xDiff; michael@0: } michael@0: michael@0: if (destTrimmedRect.y < 0) michael@0: { michael@0: int yDiff = 0 - destTrimmedRect.y; michael@0: destTrimmedRect.y = 0; michael@0: destTrimmedRect.height -= yDiff; michael@0: sourceTrimmedRect.y += yDiff; michael@0: sourceTrimmedRect.height -= yDiff; michael@0: } michael@0: michael@0: if (destTrimmedRect.y + destTrimmedRect.height > drawBufferHeight) michael@0: { michael@0: int yDiff = (destTrimmedRect.y + destTrimmedRect.height) - drawBufferHeight; michael@0: destTrimmedRect.height -= yDiff; michael@0: sourceTrimmedRect.height -= yDiff; michael@0: } michael@0: michael@0: bool partialBufferCopy = false; michael@0: if (sourceTrimmedRect.height < readBufferHeight || michael@0: sourceTrimmedRect.width < readBufferWidth || michael@0: destTrimmedRect.height < drawBufferHeight || michael@0: destTrimmedRect.width < drawBufferWidth || michael@0: sourceTrimmedRect.y != 0 || destTrimmedRect.y != 0 || sourceTrimmedRect.x != 0 || destTrimmedRect.x != 0) michael@0: { michael@0: partialBufferCopy = true; michael@0: } michael@0: michael@0: if (mask & GL_COLOR_BUFFER_BIT) michael@0: { michael@0: const GLenum readColorbufferType = readFramebuffer->getReadColorbufferType(); michael@0: const bool validReadType = (readColorbufferType == GL_TEXTURE_2D) || (readColorbufferType == GL_RENDERBUFFER); michael@0: bool validDrawType = true; michael@0: bool validDrawFormat = true; michael@0: michael@0: for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++) michael@0: { michael@0: if (drawFramebuffer->isEnabledColorAttachment(colorAttachment)) michael@0: { michael@0: if (drawFramebuffer->getColorbufferType(colorAttachment) != GL_TEXTURE_2D && michael@0: drawFramebuffer->getColorbufferType(colorAttachment) != GL_RENDERBUFFER) michael@0: { michael@0: validDrawType = false; michael@0: } michael@0: michael@0: if (drawFramebuffer->getColorbuffer(colorAttachment)->getActualFormat() != readColorBuffer->getActualFormat()) michael@0: { michael@0: validDrawFormat = false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (!validReadType || !validDrawType || !validDrawFormat) michael@0: { michael@0: ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation"); michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: if (partialBufferCopy && readFramebuffer->getSamples() != 0) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: blitRenderTarget = true; michael@0: michael@0: } michael@0: michael@0: if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)) michael@0: { michael@0: Renderbuffer *readDSBuffer = NULL; michael@0: Renderbuffer *drawDSBuffer = NULL; michael@0: michael@0: // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have michael@0: // both a depth and stencil buffer, it will be the same buffer. michael@0: michael@0: if (mask & GL_DEPTH_BUFFER_BIT) michael@0: { michael@0: if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer()) michael@0: { michael@0: if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() || michael@0: readFramebuffer->getDepthbuffer()->getActualFormat() != drawFramebuffer->getDepthbuffer()->getActualFormat()) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: blitDepthStencil = true; michael@0: readDSBuffer = readFramebuffer->getDepthbuffer(); michael@0: drawDSBuffer = drawFramebuffer->getDepthbuffer(); michael@0: } michael@0: } michael@0: michael@0: if (mask & GL_STENCIL_BUFFER_BIT) michael@0: { michael@0: if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer()) michael@0: { michael@0: if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() || michael@0: readFramebuffer->getStencilbuffer()->getActualFormat() != drawFramebuffer->getStencilbuffer()->getActualFormat()) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: michael@0: blitDepthStencil = true; michael@0: readDSBuffer = readFramebuffer->getStencilbuffer(); michael@0: drawDSBuffer = drawFramebuffer->getStencilbuffer(); michael@0: } michael@0: } michael@0: michael@0: if (partialBufferCopy) michael@0: { michael@0: ERR("Only whole-buffer depth and stencil blits are supported by this implementation."); michael@0: return gl::error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted michael@0: } michael@0: michael@0: if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) || michael@0: (readDSBuffer && readDSBuffer->getSamples() != 0)) michael@0: { michael@0: return gl::error(GL_INVALID_OPERATION); michael@0: } michael@0: } michael@0: michael@0: if (blitRenderTarget || blitDepthStencil) michael@0: { michael@0: mRenderer->blitRect(readFramebuffer, sourceTrimmedRect, drawFramebuffer, destTrimmedRect, blitRenderTarget, blitDepthStencil); michael@0: } michael@0: } michael@0: michael@0: } michael@0: michael@0: extern "C" michael@0: { michael@0: gl::Context *glCreateContext(const gl::Context *shareContext, rx::Renderer *renderer, bool notifyResets, bool robustAccess) michael@0: { michael@0: return new gl::Context(shareContext, renderer, notifyResets, robustAccess); michael@0: } michael@0: michael@0: void glDestroyContext(gl::Context *context) michael@0: { michael@0: delete context; michael@0: michael@0: if (context == gl::getContext()) michael@0: { michael@0: gl::makeCurrent(NULL, NULL, NULL); michael@0: } michael@0: } michael@0: michael@0: void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface) michael@0: { michael@0: gl::makeCurrent(context, display, surface); michael@0: } michael@0: michael@0: gl::Context *glGetCurrentContext() michael@0: { michael@0: return gl::getContext(); michael@0: } michael@0: michael@0: }