blob: f6ce66743f09ed5da9a51b012020ac723f81a104 [file] [log] [blame]
//
// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Context.cpp: Implements the gl::Context class, managing all GL state and performing
// rendering operations. It is the GLES2 specific implementation of EGLContext.
#include "libGLESv2/Context.h"
#include <algorithm>
#include <sstream>
#include "libEGL/Display.h"
#include "libGLESv2/main.h"
#include "libGLESv2/mathutil.h"
#include "libGLESv2/utilities.h"
#include "libGLESv2/Blit.h"
#include "libGLESv2/ResourceManager.h"
#include "libGLESv2/Buffer.h"
#include "libGLESv2/Fence.h"
#include "libGLESv2/Framebuffer.h"
#include "libGLESv2/Program.h"
#include "libGLESv2/ProgramBinary.h"
#include "libGLESv2/Query.h"
#include "libGLESv2/Renderbuffer.h"
#include "libGLESv2/Shader.h"
#include "libGLESv2/Texture.h"
#include "libGLESv2/VertexDataManager.h"
#include "libGLESv2/IndexDataManager.h"
#undef near
#undef far
namespace gl
{
static const char* makeStaticString(const std::string& str)
{
static std::set<std::string> strings;
std::set<std::string>::iterator it = strings.find(str);
if (it != strings.end())
return it->c_str();
return strings.insert(str).first->c_str();
}
Context::Context(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess) : mConfig(config)
{
ASSERT(robustAccess == false); // Unimplemented
mDisplay = NULL;
mDevice = NULL;
mFenceHandleAllocator.setBaseHandle(0);
setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
mState.depthClearValue = 1.0f;
mState.stencilClearValue = 0;
mState.cullFace = false;
mState.cullMode = GL_BACK;
mState.frontFace = GL_CCW;
mState.depthTest = false;
mState.depthFunc = GL_LESS;
mState.blend = false;
mState.sourceBlendRGB = GL_ONE;
mState.sourceBlendAlpha = GL_ONE;
mState.destBlendRGB = GL_ZERO;
mState.destBlendAlpha = GL_ZERO;
mState.blendEquationRGB = GL_FUNC_ADD;
mState.blendEquationAlpha = GL_FUNC_ADD;
mState.blendColor.red = 0;
mState.blendColor.green = 0;
mState.blendColor.blue = 0;
mState.blendColor.alpha = 0;
mState.stencilTest = false;
mState.stencilFunc = GL_ALWAYS;
mState.stencilRef = 0;
mState.stencilMask = -1;
mState.stencilWritemask = -1;
mState.stencilBackFunc = GL_ALWAYS;
mState.stencilBackRef = 0;
mState.stencilBackMask = - 1;
mState.stencilBackWritemask = -1;
mState.stencilFail = GL_KEEP;
mState.stencilPassDepthFail = GL_KEEP;
mState.stencilPassDepthPass = GL_KEEP;
mState.stencilBackFail = GL_KEEP;
mState.stencilBackPassDepthFail = GL_KEEP;
mState.stencilBackPassDepthPass = GL_KEEP;
mState.polygonOffsetFill = false;
mState.polygonOffsetFactor = 0.0f;
mState.polygonOffsetUnits = 0.0f;
mState.sampleAlphaToCoverage = false;
mState.sampleCoverage = false;
mState.sampleCoverageValue = 1.0f;
mState.sampleCoverageInvert = false;
mState.scissorTest = false;
mState.dither = true;
mState.generateMipmapHint = GL_DONT_CARE;
mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
mState.lineWidth = 1.0f;
mState.viewportX = 0;
mState.viewportY = 0;
mState.viewportWidth = config->mDisplayMode.Width;
mState.viewportHeight = config->mDisplayMode.Height;
mState.zNear = 0.0f;
mState.zFar = 1.0f;
mState.scissorX = 0;
mState.scissorY = 0;
mState.scissorWidth = config->mDisplayMode.Width;
mState.scissorHeight = config->mDisplayMode.Height;
mState.colorMaskRed = true;
mState.colorMaskGreen = true;
mState.colorMaskBlue = true;
mState.colorMaskAlpha = true;
mState.depthMask = true;
if (shareContext != NULL)
{
mResourceManager = shareContext->mResourceManager;
mResourceManager->addRef();
}
else
{
mResourceManager = new ResourceManager();
}
// [OpenGL ES 2.0.24] section 3.7 page 83:
// In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
// and cube map texture state vectors respectively associated with them.
// In order that access to these initial textures not be lost, they are treated as texture
// objects all of whose names are 0.
mTexture2DZero.set(new Texture2D(0));
mTextureCubeMapZero.set(new TextureCubeMap(0));
mState.activeSampler = 0;
bindArrayBuffer(0);
bindElementArrayBuffer(0);
bindTextureCubeMap(0);
bindTexture2D(0);
bindReadFramebuffer(0);
bindDrawFramebuffer(0);
bindRenderbuffer(0);
mState.currentProgram = 0;
mCurrentProgramBinary.set(NULL);
mState.packAlignment = 4;
mState.unpackAlignment = 4;
mState.packReverseRowOrder = false;
mExtensionString = NULL;
mRendererString = NULL;
mVertexDataManager = NULL;
mIndexDataManager = NULL;
mBlit = NULL;
mLineLoopIB = NULL;
mInvalidEnum = false;
mInvalidValue = false;
mInvalidOperation = false;
mOutOfMemory = false;
mInvalidFramebufferOperation = false;
mHasBeenCurrent = false;
mContextLost = false;
mResetStatus = GL_NO_ERROR;
mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
mRobustAccess = robustAccess;
mSupportsDXT1Textures = false;
mSupportsDXT3Textures = false;
mSupportsDXT5Textures = false;
mSupportsEventQueries = false;
mSupportsOcclusionQueries = false;
mNumCompressedTextureFormats = 0;
mMaxSupportedSamples = 0;
mMaskedClearSavedState = NULL;
markAllStateDirty();
}
Context::~Context()
{
if (mState.currentProgram != 0)
{
Program *programObject = mResourceManager->getProgram(mState.currentProgram);
if (programObject)
{
programObject->release();
}
mState.currentProgram = 0;
}
mCurrentProgramBinary.set(NULL);
while (!mFramebufferMap.empty())
{
deleteFramebuffer(mFramebufferMap.begin()->first);
}
while (!mFenceMap.empty())
{
deleteFence(mFenceMap.begin()->first);
}
while (!mQueryMap.empty())
{
deleteQuery(mQueryMap.begin()->first);
}
while (!mMultiSampleSupport.empty())
{
delete [] mMultiSampleSupport.begin()->second;
mMultiSampleSupport.erase(mMultiSampleSupport.begin());
}
for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
{
for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
{
mState.samplerTexture[type][sampler].set(NULL);
}
}
for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
{
mIncompleteTextures[type].set(NULL);
}
for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
{
mState.vertexAttribute[i].mBoundBuffer.set(NULL);
}
for (int i = 0; i < QUERY_TYPE_COUNT; i++)
{
mState.activeQuery[i].set(NULL);
}
mState.arrayBuffer.set(NULL);
mState.elementArrayBuffer.set(NULL);
mState.renderbuffer.set(NULL);
mTexture2DZero.set(NULL);
mTextureCubeMapZero.set(NULL);
delete mVertexDataManager;
delete mIndexDataManager;
delete mBlit;
delete mLineLoopIB;
if (mMaskedClearSavedState)
{
mMaskedClearSavedState->Release();
}
mResourceManager->release();
}
void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
{
mDisplay = display;
mDevice = mDisplay->getDevice();
if (!mHasBeenCurrent)
{
mDeviceCaps = mDisplay->getDeviceCaps();
mVertexDataManager = new VertexDataManager(this, mDevice);
mIndexDataManager = new IndexDataManager(this, mDevice);
mBlit = new Blit(this);
mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
mMaximumPointSize = mDeviceCaps.MaxPointSize;
mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
mSupportsInstancing = mDisplay->getInstancingSupport();
mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
(int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
mMaxRenderbufferDimension = mMaxTextureDimension;
mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
mMaxTextureAnisotropy = mDisplay->getTextureFilterAnisotropySupport();
TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d, MaxTextureAnisotropy=%f",
mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel, mMaxTextureAnisotropy);
const D3DFORMAT renderBufferFormats[] =
{
D3DFMT_A8R8G8B8,
D3DFMT_X8R8G8B8,
D3DFMT_R5G6B5,
D3DFMT_D24S8
};
int max = 0;
for (unsigned int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
{
bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
{
if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
{
max = j;
}
}
}
mMaxSupportedSamples = max;
mSupportsEventQueries = mDisplay->getEventQuerySupport();
mSupportsOcclusionQueries = mDisplay->getOcclusionQuerySupport();
mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
mSupportsDepthTextures = mDisplay->getDepthTextureSupport();
mSupportsTextureFilterAnisotropy = mMaxTextureAnisotropy >= 2.0f;
mSupportsDerivativeInstructions = (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0;
mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
mNumCompressedTextureFormats = 0;
if (supportsDXT1Textures())
{
mNumCompressedTextureFormats += 2;
}
if (supportsDXT3Textures())
{
mNumCompressedTextureFormats += 1;
}
if (supportsDXT5Textures())
{
mNumCompressedTextureFormats += 1;
}
initExtensionString();
initRendererString();
mState.viewportX = 0;
mState.viewportY = 0;
mState.viewportWidth = surface->getWidth();
mState.viewportHeight = surface->getHeight();
mState.scissorX = 0;
mState.scissorY = 0;
mState.scissorWidth = surface->getWidth();
mState.scissorHeight = surface->getHeight();
mHasBeenCurrent = true;
}
// Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
setFramebufferZero(framebufferZero);
if (defaultRenderTarget)
{
defaultRenderTarget->Release();
}
if (depthStencil)
{
depthStencil->Release();
}
markAllStateDirty();
}
// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
void Context::markAllStateDirty()
{
for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
{
mAppliedTextureSerialPS[t] = 0;
}
for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
{
mAppliedTextureSerialVS[t] = 0;
}
mAppliedProgramBinarySerial = 0;
mAppliedRenderTargetSerial = 0;
mAppliedDepthbufferSerial = 0;
mAppliedStencilbufferSerial = 0;
mAppliedIBSerial = 0;
mDepthStencilInitialized = false;
mViewportInitialized = false;
mRenderTargetDescInitialized = false;
mVertexDeclarationCache.markStateDirty();
mClearStateDirty = true;
mCullStateDirty = true;
mDepthStateDirty = true;
mMaskStateDirty = true;
mBlendStateDirty = true;
mStencilStateDirty = true;
mPolygonOffsetStateDirty = true;
mScissorStateDirty = true;
mSampleStateDirty = true;
mDitherStateDirty = true;
mFrontFaceDirty = true;
mDxUniformsDirty = true;
}
void Context::markDxUniformsDirty()
{
mDxUniformsDirty = true;
}
void Context::markContextLost()
{
if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
mContextLost = true;
}
bool Context::isContextLost()
{
return mContextLost;
}
void Context::setClearColor(float red, float green, float blue, float alpha)
{
mState.colorClearValue.red = red;
mState.colorClearValue.green = green;
mState.colorClearValue.blue = blue;
mState.colorClearValue.alpha = alpha;
}
void Context::setClearDepth(float depth)
{
mState.depthClearValue = depth;
}
void Context::setClearStencil(int stencil)
{
mState.stencilClearValue = stencil;
}
void Context::setCullFace(bool enabled)
{
if (mState.cullFace != enabled)
{
mState.cullFace = enabled;
mCullStateDirty = true;
}
}
bool Context::isCullFaceEnabled() const
{
return mState.cullFace;
}
void Context::setCullMode(GLenum mode)
{
if (mState.cullMode != mode)
{
mState.cullMode = mode;
mCullStateDirty = true;
}
}
void Context::setFrontFace(GLenum front)
{
if (mState.frontFace != front)
{
mState.frontFace = front;
mFrontFaceDirty = true;
}
}
void Context::setDepthTest(bool enabled)
{
if (mState.depthTest != enabled)
{
mState.depthTest = enabled;
mDepthStateDirty = true;
}
}
bool Context::isDepthTestEnabled() const
{
return mState.depthTest;
}
void Context::setDepthFunc(GLenum depthFunc)
{
if (mState.depthFunc != depthFunc)
{
mState.depthFunc = depthFunc;
mDepthStateDirty = true;
}
}
void Context::setDepthRange(float zNear, float zFar)
{
mState.zNear = zNear;
mState.zFar = zFar;
}
void Context::setBlend(bool enabled)
{
if (mState.blend != enabled)
{
mState.blend = enabled;
mBlendStateDirty = true;
}
}
bool Context::isBlendEnabled() const
{
return mState.blend;
}
void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
{
if (mState.sourceBlendRGB != sourceRGB ||
mState.sourceBlendAlpha != sourceAlpha ||
mState.destBlendRGB != destRGB ||
mState.destBlendAlpha != destAlpha)
{
mState.sourceBlendRGB = sourceRGB;
mState.destBlendRGB = destRGB;
mState.sourceBlendAlpha = sourceAlpha;
mState.destBlendAlpha = destAlpha;
mBlendStateDirty = true;
}
}
void Context::setBlendColor(float red, float green, float blue, float alpha)
{
if (mState.blendColor.red != red ||
mState.blendColor.green != green ||
mState.blendColor.blue != blue ||
mState.blendColor.alpha != alpha)
{
mState.blendColor.red = red;
mState.blendColor.green = green;
mState.blendColor.blue = blue;
mState.blendColor.alpha = alpha;
mBlendStateDirty = true;
}
}
void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
{
if (mState.blendEquationRGB != rgbEquation ||
mState.blendEquationAlpha != alphaEquation)
{
mState.blendEquationRGB = rgbEquation;
mState.blendEquationAlpha = alphaEquation;
mBlendStateDirty = true;
}
}
void Context::setStencilTest(bool enabled)
{
if (mState.stencilTest != enabled)
{
mState.stencilTest = enabled;
mStencilStateDirty = true;
}
}
bool Context::isStencilTestEnabled() const
{
return mState.stencilTest;
}
void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
{
if (mState.stencilFunc != stencilFunc ||
mState.stencilRef != stencilRef ||
mState.stencilMask != stencilMask)
{
mState.stencilFunc = stencilFunc;
mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
mState.stencilMask = stencilMask;
mStencilStateDirty = true;
}
}
void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
{
if (mState.stencilBackFunc != stencilBackFunc ||
mState.stencilBackRef != stencilBackRef ||
mState.stencilBackMask != stencilBackMask)
{
mState.stencilBackFunc = stencilBackFunc;
mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
mState.stencilBackMask = stencilBackMask;
mStencilStateDirty = true;
}
}
void Context::setStencilWritemask(GLuint stencilWritemask)
{
if (mState.stencilWritemask != stencilWritemask)
{
mState.stencilWritemask = stencilWritemask;
mStencilStateDirty = true;
}
}
void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
{
if (mState.stencilBackWritemask != stencilBackWritemask)
{
mState.stencilBackWritemask = stencilBackWritemask;
mStencilStateDirty = true;
}
}
void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
{
if (mState.stencilFail != stencilFail ||
mState.stencilPassDepthFail != stencilPassDepthFail ||
mState.stencilPassDepthPass != stencilPassDepthPass)
{
mState.stencilFail = stencilFail;
mState.stencilPassDepthFail = stencilPassDepthFail;
mState.stencilPassDepthPass = stencilPassDepthPass;
mStencilStateDirty = true;
}
}
void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
{
if (mState.stencilBackFail != stencilBackFail ||
mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
{
mState.stencilBackFail = stencilBackFail;
mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
mStencilStateDirty = true;
}
}
void Context::setPolygonOffsetFill(bool enabled)
{
if (mState.polygonOffsetFill != enabled)
{
mState.polygonOffsetFill = enabled;
mPolygonOffsetStateDirty = true;
}
}
bool Context::isPolygonOffsetFillEnabled() const
{
return mState.polygonOffsetFill;
}
void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
{
if (mState.polygonOffsetFactor != factor ||
mState.polygonOffsetUnits != units)
{
mState.polygonOffsetFactor = factor;
mState.polygonOffsetUnits = units;
mPolygonOffsetStateDirty = true;
}
}
void Context::setSampleAlphaToCoverage(bool enabled)
{
if (mState.sampleAlphaToCoverage != enabled)
{
mState.sampleAlphaToCoverage = enabled;
mSampleStateDirty = true;
}
}
bool Context::isSampleAlphaToCoverageEnabled() const
{
return mState.sampleAlphaToCoverage;
}
void Context::setSampleCoverage(bool enabled)
{
if (mState.sampleCoverage != enabled)
{
mState.sampleCoverage = enabled;
mSampleStateDirty = true;
}
}
bool Context::isSampleCoverageEnabled() const
{
return mState.sampleCoverage;
}
void Context::setSampleCoverageParams(GLclampf value, bool invert)
{
if (mState.sampleCoverageValue != value ||
mState.sampleCoverageInvert != invert)
{
mState.sampleCoverageValue = value;
mState.sampleCoverageInvert = invert;
mSampleStateDirty = true;
}
}
void Context::setScissorTest(bool enabled)
{
if (mState.scissorTest != enabled)
{
mState.scissorTest = enabled;
mScissorStateDirty = true;
}
}
bool Context::isScissorTestEnabled() const
{
return mState.scissorTest;
}
void Context::setDither(bool enabled)
{
if (mState.dither != enabled)
{
mState.dither = enabled;
mDitherStateDirty = true;
}
}
bool Context::isDitherEnabled() const
{
return mState.dither;
}
void Context::setLineWidth(GLfloat width)
{
mState.lineWidth = width;
}
void Context::setGenerateMipmapHint(GLenum hint)
{
mState.generateMipmapHint = hint;
}
void Context::setFragmentShaderDerivativeHint(GLenum hint)
{
mState.fragmentShaderDerivativeHint = hint;
// TODO: Propagate the hint to shader translator so we can write
// ddx, ddx_coarse, or ddx_fine depending on the hint.
// Ignore for now. It is valid for implementations to ignore hint.
}
void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
{
mState.viewportX = x;
mState.viewportY = y;
mState.viewportWidth = width;
mState.viewportHeight = height;
}
void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
{
if (mState.scissorX != x || mState.scissorY != y ||
mState.scissorWidth != width || mState.scissorHeight != height)
{
mState.scissorX = x;
mState.scissorY = y;
mState.scissorWidth = width;
mState.scissorHeight = height;
mScissorStateDirty = true;
}
}
void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
{
if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
{
mState.colorMaskRed = red;
mState.colorMaskGreen = green;
mState.colorMaskBlue = blue;
mState.colorMaskAlpha = alpha;
mMaskStateDirty = true;
}
}
void Context::setDepthMask(bool mask)
{
if (mState.depthMask != mask)
{
mState.depthMask = mask;
mMaskStateDirty = true;
}
}
void Context::setActiveSampler(unsigned int active)
{
mState.activeSampler = active;
}
GLuint Context::getReadFramebufferHandle() const
{
return mState.readFramebuffer;
}
GLuint Context::getDrawFramebufferHandle() const
{
return mState.drawFramebuffer;
}
GLuint Context::getRenderbufferHandle() const
{
return mState.renderbuffer.id();
}
GLuint Context::getArrayBufferHandle() const
{
return mState.arrayBuffer.id();
}
GLuint Context::getActiveQuery(GLenum target) const
{
Query *queryObject = NULL;
switch (target)
{
case GL_ANY_SAMPLES_PASSED_EXT:
queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get();
break;
case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get();
break;
default:
ASSERT(false);
}
if (queryObject)
{
return queryObject->id();
}
else
{
return 0;
}
}
void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
{
mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
}
const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
{
return mState.vertexAttribute[attribNum];
}
void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
GLsizei stride, const void *pointer)
{
mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
mState.vertexAttribute[attribNum].mSize = size;
mState.vertexAttribute[attribNum].mType = type;
mState.vertexAttribute[attribNum].mNormalized = normalized;
mState.vertexAttribute[attribNum].mStride = stride;
mState.vertexAttribute[attribNum].mPointer = pointer;
}
const void *Context::getVertexAttribPointer(unsigned int attribNum) const
{
return mState.vertexAttribute[attribNum].mPointer;
}
const VertexAttributeArray &Context::getVertexAttributes()
{
return mState.vertexAttribute;
}
void Context::setPackAlignment(GLint alignment)
{
mState.packAlignment = alignment;
}
GLint Context::getPackAlignment() const
{
return mState.packAlignment;
}
void Context::setUnpackAlignment(GLint alignment)
{
mState.unpackAlignment = alignment;
}
GLint Context::getUnpackAlignment() const
{
return mState.unpackAlignment;
}
void Context::setPackReverseRowOrder(bool reverseRowOrder)
{
mState.packReverseRowOrder = reverseRowOrder;
}
bool Context::getPackReverseRowOrder() const
{
return mState.packReverseRowOrder;
}
GLuint Context::createBuffer()
{
return mResourceManager->createBuffer();
}
GLuint Context::createProgram()
{
return mResourceManager->createProgram();
}
GLuint Context::createShader(GLenum type)
{
return mResourceManager->createShader(type);
}
GLuint Context::createTexture()
{
return mResourceManager->createTexture();
}
GLuint Context::createRenderbuffer()
{
return mResourceManager->createRenderbuffer();
}
// Returns an unused framebuffer name
GLuint Context::createFramebuffer()
{
GLuint handle = mFramebufferHandleAllocator.allocate();
mFramebufferMap[handle] = NULL;
return handle;
}
GLuint Context::createFence()
{
GLuint handle = mFenceHandleAllocator.allocate();
mFenceMap[handle] = new Fence(mDisplay);
return handle;
}
// Returns an unused query name
GLuint Context::createQuery()
{
GLuint handle = mQueryHandleAllocator.allocate();
mQueryMap[handle] = NULL;
return handle;
}
void Context::deleteBuffer(GLuint buffer)
{
if (mResourceManager->getBuffer(buffer))
{
detachBuffer(buffer);
}
mResourceManager->deleteBuffer(buffer);
}
void Context::deleteShader(GLuint shader)
{
mResourceManager->deleteShader(shader);
}
void Context::deleteProgram(GLuint program)
{
mResourceManager->deleteProgram(program);
}
void Context::deleteTexture(GLuint texture)
{
if (mResourceManager->getTexture(texture))
{
detachTexture(texture);
}
mResourceManager->deleteTexture(texture);
}
void Context::deleteRenderbuffer(GLuint renderbuffer)
{
if (mResourceManager->getRenderbuffer(renderbuffer))
{
detachRenderbuffer(renderbuffer);
}
mResourceManager->deleteRenderbuffer(renderbuffer);
}
void Context::deleteFramebuffer(GLuint framebuffer)
{
FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
if (framebufferObject != mFramebufferMap.end())
{
detachFramebuffer(framebuffer);
mFramebufferHandleAllocator.release(framebufferObject->first);
delete framebufferObject->second;
mFramebufferMap.erase(framebufferObject);
}
}
void Context::deleteFence(GLuint fence)
{
FenceMap::iterator fenceObject = mFenceMap.find(fence);
if (fenceObject != mFenceMap.end())
{
mFenceHandleAllocator.release(fenceObject->first);
delete fenceObject->second;
mFenceMap.erase(fenceObject);
}
}
void Context::deleteQuery(GLuint query)
{
QueryMap::iterator queryObject = mQueryMap.find(query);
if (queryObject != mQueryMap.end())
{
mQueryHandleAllocator.release(queryObject->first);
if (queryObject->second)
{
queryObject->second->release();
}
mQueryMap.erase(queryObject);
}
}
Buffer *Context::getBuffer(GLuint handle)
{
return mResourceManager->getBuffer(handle);
}
Shader *Context::getShader(GLuint handle)
{
return mResourceManager->getShader(handle);
}
Program *Context::getProgram(GLuint handle)
{
return mResourceManager->getProgram(handle);
}
Texture *Context::getTexture(GLuint handle)
{
return mResourceManager->getTexture(handle);
}
Renderbuffer *Context::getRenderbuffer(GLuint handle)
{
return mResourceManager->getRenderbuffer(handle);
}
Framebuffer *Context::getReadFramebuffer()
{
return getFramebuffer(mState.readFramebuffer);
}
Framebuffer *Context::getDrawFramebuffer()
{
return mBoundDrawFramebuffer;
}
void Context::bindArrayBuffer(unsigned int buffer)
{
mResourceManager->checkBufferAllocation(buffer);
mState.arrayBuffer.set(getBuffer(buffer));
}
void Context::bindElementArrayBuffer(unsigned int buffer)
{
mResourceManager->checkBufferAllocation(buffer);
mState.elementArrayBuffer.set(getBuffer(buffer));
}
void Context::bindTexture2D(GLuint texture)
{
mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
}
void Context::bindTextureCubeMap(GLuint texture)
{
mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
}
void Context::bindReadFramebuffer(GLuint framebuffer)
{
if (!getFramebuffer(framebuffer))
{
mFramebufferMap[framebuffer] = new Framebuffer();
}
mState.readFramebuffer = framebuffer;
}
void Context::bindDrawFramebuffer(GLuint framebuffer)
{
if (!getFramebuffer(framebuffer))
{
mFramebufferMap[framebuffer] = new Framebuffer();
}
mState.drawFramebuffer = framebuffer;
mBoundDrawFramebuffer = getFramebuffer(framebuffer);
}
void Context::bindRenderbuffer(GLuint renderbuffer)
{
mResourceManager->checkRenderbufferAllocation(renderbuffer);
mState.renderbuffer.set(getRenderbuffer(renderbuffer));
}
void Context::useProgram(GLuint program)
{
GLuint priorProgram = mState.currentProgram;
mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
if (priorProgram != program)
{
Program *newProgram = mResourceManager->getProgram(program);
Program *oldProgram = mResourceManager->getProgram(priorProgram);
mCurrentProgramBinary.set(NULL);
mDxUniformsDirty = true;
if (newProgram)
{
newProgram->addRef();
mCurrentProgramBinary.set(newProgram->getProgramBinary());
}
if (oldProgram)
{
oldProgram->release();
}
}
}
void Context::linkProgram(GLuint program)
{
Program *programObject = mResourceManager->getProgram(program);
bool linked = programObject->link();
// if the current program was relinked successfully we
// need to install the new executables
if (linked && program == mState.currentProgram)
{
mCurrentProgramBinary.set(programObject->getProgramBinary());
mDxUniformsDirty = true;
}
}
void Context::setProgramBinary(GLuint program, const void *binary, GLint length)
{
Program *programObject = mResourceManager->getProgram(program);
bool loaded = programObject->setProgramBinary(binary, length);
// if the current program was reloaded successfully we
// need to install the new executables
if (loaded && program == mState.currentProgram)
{
mCurrentProgramBinary.set(programObject->getProgramBinary());
mDxUniformsDirty = true;
}
}
void Context::beginQuery(GLenum target, GLuint query)
{
// From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
// of zero, if the active query object name for <target> is non-zero (for the
// targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
// the active query for either target is non-zero), if <id> is the name of an
// existing query object whose type does not match <target>, or if <id> is the
// active query object name for any query type, the error INVALID_OPERATION is
// generated.
// Ensure no other queries are active
// NOTE: If other queries than occlusion are supported, we will need to check
// separately that:
// a) The query ID passed is not the current active query for any target/type
// b) There are no active queries for the requested target (and in the case
// of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
// no query may be active for either if glBeginQuery targets either.
for (int i = 0; i < QUERY_TYPE_COUNT; i++)
{
if (mState.activeQuery[i].get() != NULL)
{
return error(GL_INVALID_OPERATION);
}
}
QueryType qType;
switch (target)
{
case GL_ANY_SAMPLES_PASSED_EXT:
qType = QUERY_ANY_SAMPLES_PASSED;
break;
case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
break;
default:
ASSERT(false);
return;
}
Query *queryObject = getQuery(query, true, target);
// check that name was obtained with glGenQueries
if (!queryObject)
{
return error(GL_INVALID_OPERATION);
}
// check for type mismatch
if (queryObject->getType() != target)
{
return error(GL_INVALID_OPERATION);
}
// set query as active for specified target
mState.activeQuery[qType].set(queryObject);
// begin query
queryObject->begin();
}
void Context::endQuery(GLenum target)
{
QueryType qType;
switch (target)
{
case GL_ANY_SAMPLES_PASSED_EXT:
qType = QUERY_ANY_SAMPLES_PASSED;
break;
case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
break;
default:
ASSERT(false);
return;
}
Query *queryObject = mState.activeQuery[qType].get();
if (queryObject == NULL)
{
return error(GL_INVALID_OPERATION);
}
queryObject->end();
mState.activeQuery[qType].set(NULL);
}
void Context::setFramebufferZero(Framebuffer *buffer)
{
delete mFramebufferMap[0];
mFramebufferMap[0] = buffer;
if (mState.drawFramebuffer == 0)
{
mBoundDrawFramebuffer = buffer;
}
}
void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
{
Renderbuffer *renderbufferObject = mState.renderbuffer.get();
renderbufferObject->setStorage(renderbuffer);
}
Framebuffer *Context::getFramebuffer(unsigned int handle)
{
FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
if (framebuffer == mFramebufferMap.end())
{
return NULL;
}
else
{
return framebuffer->second;
}
}
Fence *Context::getFence(unsigned int handle)
{
FenceMap::iterator fence = mFenceMap.find(handle);
if (fence == mFenceMap.end())
{
return NULL;
}
else
{
return fence->second;
}
}
Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
{
QueryMap::iterator query = mQueryMap.find(handle);
if (query == mQueryMap.end())
{
return NULL;
}
else
{
if (!query->second && create)
{
query->second = new Query(handle, type);
query->second->addRef();
}
return query->second;
}
}
Buffer *Context::getArrayBuffer()
{
return mState.arrayBuffer.get();
}
Buffer *Context::getElementArrayBuffer()
{
return mState.elementArrayBuffer.get();
}
ProgramBinary *Context::getCurrentProgramBinary()
{
return mCurrentProgramBinary.get();
}
Texture2D *Context::getTexture2D()
{
return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
}
TextureCubeMap *Context::getTextureCubeMap()
{
return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
}
Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
{
GLuint texid = mState.samplerTexture[type][sampler].id();
if (texid == 0) // Special case: 0 refers to different initial textures based on the target
{
switch (type)
{
default: UNREACHABLE();
case TEXTURE_2D: return mTexture2DZero.get();
case TEXTURE_CUBE: return mTextureCubeMapZero.get();
}
}
return mState.samplerTexture[type][sampler].get();
}
bool Context::getBooleanv(GLenum pname, GLboolean *params)
{
switch (pname)
{
case GL_SHADER_COMPILER: *params = GL_TRUE; break;
case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
case GL_COLOR_WRITEMASK:
params[0] = mState.colorMaskRed;
params[1] = mState.colorMaskGreen;
params[2] = mState.colorMaskBlue;
params[3] = mState.colorMaskAlpha;
break;
case GL_CULL_FACE: *params = mState.cullFace; break;
case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
case GL_STENCIL_TEST: *params = mState.stencilTest; break;
case GL_DEPTH_TEST: *params = mState.depthTest; break;
case GL_BLEND: *params = mState.blend; break;
case GL_DITHER: *params = mState.dither; break;
case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
default:
return false;
}
return true;
}
bool Context::getFloatv(GLenum pname, GLfloat *params)
{
// Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
// because it is stored as a float, despite the fact that the GL ES 2.0 spec names
// GetIntegerv as its native query function. As it would require conversion in any
// case, this should make no difference to the calling application.
switch (pname)
{
case GL_LINE_WIDTH: *params = mState.lineWidth; break;
case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
case GL_ALIASED_LINE_WIDTH_RANGE:
params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
break;
case GL_ALIASED_POINT_SIZE_RANGE:
params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
params[1] = getMaximumPointSize();
break;
case GL_DEPTH_RANGE:
params[0] = mState.zNear;
params[1] = mState.zFar;
break;
case GL_COLOR_CLEAR_VALUE:
params[0] = mState.colorClearValue.red;
params[1] = mState.colorClearValue.green;
params[2] = mState.colorClearValue.blue;
params[3] = mState.colorClearValue.alpha;
break;
case GL_BLEND_COLOR:
params[0] = mState.blendColor.red;
params[1] = mState.blendColor.green;
params[2] = mState.blendColor.blue;
params[3] = mState.blendColor.alpha;
break;
case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
if (!supportsTextureFilterAnisotropy())
{
return false;
}
*params = mMaxTextureAnisotropy;
break;
default:
return false;
}
return true;
}
bool Context::getIntegerv(GLenum pname, GLint *params)
{
// Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
// because it is stored as a float, despite the fact that the GL ES 2.0 spec names
// GetIntegerv as its native query function. As it would require conversion in any
// case, this should make no difference to the calling application. You may find it in
// Context::getFloatv.
switch (pname)
{
case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
//case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
case GL_STENCIL_REF: *params = mState.stencilRef; break;
case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
case GL_SUBPIXEL_BITS: *params = 4; break;
case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
params[0] = mNumCompressedTextureFormats;
break;
case GL_MAX_SAMPLES_ANGLE:
{
GLsizei maxSamples = getMaxSupportedSamples();
if (maxSamples != 0)
{
*params = maxSamples;
}
else
{
return false;
}
break;
}
case GL_SAMPLE_BUFFERS:
case GL_SAMPLES:
{
gl::Framebuffer *framebuffer = getDrawFramebuffer();
if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
{
switch (pname)
{
case GL_SAMPLE_BUFFERS:
if (framebuffer->getSamples() != 0)
{
*params = 1;
}
else
{
*params = 0;
}
break;
case GL_SAMPLES:
*params = framebuffer->getSamples();
break;
}
}
else
{
*params = 0;
}
}
break;
case GL_IMPLEMENTATION_COLOR_READ_TYPE:
case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
{
GLenum format, type;
if (getCurrentReadFormatType(&format, &type))
{
if (pname == GL_IMPLEMENTATION_COLOR_READ_FORMAT)
*params = format;
else
*params = type;
}
}
break;
case GL_MAX_VIEWPORT_DIMS:
{
int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
params[0] = maxDimension;
params[1] = maxDimension;
}
break;
case GL_COMPRESSED_TEXTURE_FORMATS:
{
if (supportsDXT1Textures())
{
*params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
*params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
}
if (supportsDXT3Textures())
{
*params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
}
if (supportsDXT5Textures())
{
*params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
}
}
break;
case GL_VIEWPORT:
params[0] = mState.viewportX;
params[1] = mState.viewportY;
params[2] = mState.viewportWidth;
params[3] = mState.viewportHeight;
break;
case GL_SCISSOR_BOX:
params[0] = mState.scissorX;
params[1] = mState.scissorY;
params[2] = mState.scissorWidth;
params[3] = mState.scissorHeight;
break;
case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
case GL_FRONT_FACE: *params = mState.frontFace; break;
case GL_RED_BITS:
case GL_GREEN_BITS:
case GL_BLUE_BITS:
case GL_ALPHA_BITS:
{
gl::Framebuffer *framebuffer = getDrawFramebuffer();
gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
if (colorbuffer)
{
switch (pname)
{
case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
}
}
else
{
*params = 0;
}
}
break;
case GL_DEPTH_BITS:
{
gl::Framebuffer *framebuffer = getDrawFramebuffer();
gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
if (depthbuffer)
{
*params = depthbuffer->getDepthSize();
}
else
{
*params = 0;
}
}
break;
case GL_STENCIL_BITS:
{
gl::Framebuffer *framebuffer = getDrawFramebuffer();
gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
if (stencilbuffer)
{
*params = stencilbuffer->getStencilSize();
}
else
{
*params = 0;
}
}
break;
case GL_TEXTURE_BINDING_2D:
{
if (mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
{
error(GL_INVALID_OPERATION);
return false;
}
*params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
}
break;
case GL_TEXTURE_BINDING_CUBE_MAP:
{
if (mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
{
error(GL_INVALID_OPERATION);
return false;
}
*params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
}
break;
case GL_RESET_NOTIFICATION_STRATEGY_EXT:
*params = mResetStrategy;
break;
case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
*params = 1;
break;
case GL_PROGRAM_BINARY_FORMATS_OES:
*params = GL_PROGRAM_BINARY_ANGLE;
break;
default:
return false;
}
return true;
}
bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
{
// Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
// is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
// to the fact that it is stored internally as a float, and so would require conversion
// if returned from Context::getIntegerv. Since this conversion is already implemented
// in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
// place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
// application.
switch (pname)
{
case GL_COMPRESSED_TEXTURE_FORMATS:
{
*type = GL_INT;
*numParams = mNumCompressedTextureFormats;
}
break;
case GL_SHADER_BINARY_FORMATS:
{
*type = GL_INT;
*numParams = 0;
}
break;
case GL_MAX_VERTEX_ATTRIBS:
case GL_MAX_VERTEX_UNIFORM_VECTORS:
case GL_MAX_VARYING_VECTORS:
case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
case GL_MAX_TEXTURE_IMAGE_UNITS:
case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
case GL_MAX_RENDERBUFFER_SIZE:
case GL_NUM_SHADER_BINARY_FORMATS:
case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
case GL_ARRAY_BUFFER_BINDING:
case GL_FRAMEBUFFER_BINDING:
case GL_RENDERBUFFER_BINDING:
case GL_CURRENT_PROGRAM:
case GL_PACK_ALIGNMENT:
case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
case GL_UNPACK_ALIGNMENT:
case GL_GENERATE_MIPMAP_HINT:
case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
case GL_RED_BITS:
case GL_GREEN_BITS:
case GL_BLUE_BITS:
case GL_ALPHA_BITS:
case GL_DEPTH_BITS:
case GL_STENCIL_BITS:
case GL_ELEMENT_ARRAY_BUFFER_BINDING:
case GL_CULL_FACE_MODE:
case GL_FRONT_FACE:
case GL_ACTIVE_TEXTURE:
case GL_STENCIL_FUNC:
case GL_STENCIL_VALUE_MASK:
case GL_STENCIL_REF:
case GL_STENCIL_FAIL:
case GL_STENCIL_PASS_DEPTH_FAIL:
case GL_STENCIL_PASS_DEPTH_PASS:
case GL_STENCIL_BACK_FUNC:
case GL_STENCIL_BACK_VALUE_MASK:
case GL_STENCIL_BACK_REF:
case GL_STENCIL_BACK_FAIL:
case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
case GL_STENCIL_BACK_PASS_DEPTH_PASS:
case GL_DEPTH_FUNC:
case GL_BLEND_SRC_RGB:
case GL_BLEND_SRC_ALPHA:
case GL_BLEND_DST_RGB:
case GL_BLEND_DST_ALPHA:
case GL_BLEND_EQUATION_RGB:
case GL_BLEND_EQUATION_ALPHA:
case GL_STENCIL_WRITEMASK:
case GL_STENCIL_BACK_WRITEMASK:
case GL_STENCIL_CLEAR_VALUE:
case GL_SUBPIXEL_BITS:
case GL_MAX_TEXTURE_SIZE:
case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
case GL_SAMPLE_BUFFERS:
case GL_SAMPLES:
case GL_IMPLEMENTATION_COLOR_READ_TYPE:
case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
case GL_TEXTURE_BINDING_2D:
case GL_TEXTURE_BINDING_CUBE_MAP:
case GL_RESET_NOTIFICATION_STRATEGY_EXT:
case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
case GL_PROGRAM_BINARY_FORMATS_OES:
{
*type = GL_INT;
*numParams = 1;
}
break;
case GL_MAX_SAMPLES_ANGLE:
{
if (getMaxSupportedSamples() != 0)
{
*type = GL_INT;
*numParams = 1;
}
else
{
return false;
}
}
break;
case GL_MAX_VIEWPORT_DIMS:
{
*type = GL_INT;
*numParams = 2;
}
break;
case GL_VIEWPORT:
case GL_SCISSOR_BOX:
{
*type = GL_INT;
*numParams = 4;
}
break;
case GL_SHADER_COMPILER:
case GL_SAMPLE_COVERAGE_INVERT:
case GL_DEPTH_WRITEMASK:
case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
case GL_SAMPLE_COVERAGE:
case GL_SCISSOR_TEST:
case GL_STENCIL_TEST:
case GL_DEPTH_TEST:
case GL_BLEND:
case GL_DITHER:
case GL_CONTEXT_ROBUST_ACCESS_EXT:
{
*type = GL_BOOL;
*numParams = 1;
}
break;
case GL_COLOR_WRITEMASK:
{
*type = GL_BOOL;
*numParams = 4;
}
break;
case GL_POLYGON_OFFSET_FACTOR:
case GL_POLYGON_OFFSET_UNITS:
case GL_SAMPLE_COVERAGE_VALUE:
case GL_DEPTH_CLEAR_VALUE:
case GL_LINE_WIDTH:
{
*type = GL_FLOAT;
*numParams = 1;
}
break;
case GL_ALIASED_LINE_WIDTH_RANGE:
case GL_ALIASED_POINT_SIZE_RANGE:
case GL_DEPTH_RANGE:
{
*type = GL_FLOAT;
*numParams = 2;
}
break;
case GL_COLOR_CLEAR_VALUE:
case GL_BLEND_COLOR:
{
*type = GL_FLOAT;
*numParams = 4;
}
break;
case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
if (!supportsTextureFilterAnisotropy())
{
return false;
}
*type = GL_FLOAT;
*numParams = 1;
break;
default:
return false;
}
return true;
}
// Applies the render target surface, depth stencil surface, viewport rectangle and
// scissor rectangle to the Direct3D 9 device
bool Context::applyRenderTarget(bool ignoreViewport)
{
Framebuffer *framebufferObject = getDrawFramebuffer();
if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
{
return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
}
// if there is no color attachment we must synthesize a NULL colorattachment
// to keep the D3D runtime happy. This should only be possible if depth texturing.
Renderbuffer *renderbufferObject = NULL;
if (framebufferObject->getColorbufferType() != GL_NONE)
{
renderbufferObject = framebufferObject->getColorbuffer();
}
else
{
renderbufferObject = framebufferObject->getNullColorbuffer();
}
if (!renderbufferObject)
{
ERR("unable to locate renderbuffer for FBO.");
return false;
}
bool renderTargetChanged = false;
unsigned int renderTargetSerial = renderbufferObject->getSerial();
if (renderTargetSerial != mAppliedRenderTargetSerial)
{
IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
if (!renderTarget)
{
ERR("render target pointer unexpectedly null.");
return false; // Context must be lost
}
mDevice->SetRenderTarget(0, renderTarget);
mAppliedRenderTargetSerial = renderTargetSerial;
mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
renderTargetChanged = true;
renderTarget->Release();
}
IDirect3DSurface9 *depthStencil = NULL;
unsigned int depthbufferSerial = 0;
unsigned int stencilbufferSerial = 0;
if (framebufferObject->getDepthbufferType() != GL_NONE)
{
Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
depthStencil = depthbuffer->getDepthStencil();
if (!depthStencil)
{
ERR("Depth stencil pointer unexpectedly null.");
return false;
}
depthbufferSerial = depthbuffer->getSerial();
}
else if (framebufferObject->getStencilbufferType() != GL_NONE)
{
Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
depthStencil = stencilbuffer->getDepthStencil();
if (!depthStencil)
{
ERR("Depth stencil pointer unexpectedly null.");
return false;
}
stencilbufferSerial = stencilbuffer->getSerial();
}
if (depthbufferSerial != mAppliedDepthbufferSerial ||
stencilbufferSerial != mAppliedStencilbufferSerial ||
!mDepthStencilInitialized)
{
mDevice->SetDepthStencilSurface(depthStencil);
mAppliedDepthbufferSerial = depthbufferSerial;
mAppliedStencilbufferSerial = stencilbufferSerial;
mDepthStencilInitialized = true;
}
if (depthStencil)
{
depthStencil->Release();
}
if (!mRenderTargetDescInitialized || renderTargetChanged)
{
IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
if (!renderTarget)
{
return false; // Context must be lost
}
renderTarget->GetDesc(&mRenderTargetDesc);
mRenderTargetDescInitialized = true;
renderTarget->Release();
}
D3DVIEWPORT9 viewport;
float zNear = clamp01(mState.zNear);
float zFar = clamp01(mState.zFar);
if (ignoreViewport)
{
viewport.X = 0;
viewport.Y = 0;
viewport.Width = mRenderTargetDesc.Width;
viewport.Height = mRenderTargetDesc.Height;
viewport.MinZ = 0.0f;
viewport.MaxZ = 1.0f;
}
else
{
viewport.X = clamp(mState.viewportX, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
viewport.Y = clamp(mState.viewportY, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
viewport.Width = clamp(mState.viewportWidth, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
viewport.Height = clamp(mState.viewportHeight, 0L, static_cast<LONG>(mRenderTargetDesc.Height) - static_cast<LONG>(viewport.Y));
viewport.MinZ = zNear;
viewport.MaxZ = zFar;
}
if (viewport.Width <= 0 || viewport.Height <= 0)
{
return false; // Nothing to render
}
if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
{
mDevice->SetViewport(&viewport);
mSetViewport = viewport;
mViewportInitialized = true;
mDxUniformsDirty = true;
}
if (mScissorStateDirty)
{
if (mState.scissorTest)
{
RECT rect;
rect.left = clamp(mState.scissorX, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
rect.top = clamp(mState.scissorY, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
rect.right = clamp(mState.scissorX + mState.scissorWidth, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
rect.bottom = clamp(mState.scissorY + mState.scissorHeight, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
mDevice->SetScissorRect(&rect);
mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
}
else
{
mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
}
mScissorStateDirty = false;
}
if (mState.currentProgram && mDxUniformsDirty)
{
ProgramBinary *programBinary = getCurrentProgramBinary();
GLint halfPixelSize = programBinary->getDxHalfPixelSizeLocation();
GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
programBinary->setUniform2fv(halfPixelSize, 1, xy);
// These values are used for computing gl_FragCoord in Program::linkVaryings().
GLint coord = programBinary->getDxCoordLocation();
GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
(float)mState.viewportX + mState.viewportWidth / 2.0f,
(float)mState.viewportY + mState.viewportHeight / 2.0f};
programBinary->setUniform4fv(coord, 1, whxy);
GLint depth = programBinary->getDxDepthLocation();
GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
programBinary->setUniform2fv(depth, 1, dz);
GLint depthRange = programBinary->getDxDepthRangeLocation();
GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
programBinary->setUniform3fv(depthRange, 1, nearFarDiff);
mDxUniformsDirty = false;
}
return true;
}
// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
void Context::applyState(GLenum drawMode)
{
ProgramBinary *programBinary = getCurrentProgramBinary();
Framebuffer *framebufferObject = getDrawFramebuffer();
GLint frontCCW = programBinary->getDxFrontCCWLocation();
GLint ccw = (mState.frontFace == GL_CCW);
programBinary->setUniform1iv(frontCCW, 1, &ccw);
GLint pointsOrLines = programBinary->getDxPointsOrLinesLocation();
GLint alwaysFront = !isTriangleMode(drawMode);
programBinary->setUniform1iv(pointsOrLines, 1, &alwaysFront);
D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
// Apparently some ATI cards have a bug where a draw with a zero color
// write mask can cause later draws to have incorrect results. Instead,
// set a nonzero color write mask but modify the blend state so that no
// drawing is done.
// http://code.google.com/p/angleproject/issues/detail?id=169
if (mCullStateDirty || mFrontFaceDirty)
{
if (mState.cullFace)
{
mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
}
else
{
mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
}
mCullStateDirty = false;
}
if (mDepthStateDirty)
{
if (mState.depthTest)
{
mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
}
else
{
mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
}
mDepthStateDirty = false;
}
if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
{
mBlendStateDirty = true;
mMaskStateDirty = true;
}
if (mBlendStateDirty)
{
if (mState.blend)
{
mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
{
mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
}
else
{
mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
unorm<8>(mState.blendColor.alpha),
unorm<8>(mState.blendColor.alpha),
unorm<8>(mState.blendColor.alpha)));
}
mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
mState.destBlendRGB != mState.destBlendAlpha ||
mState.blendEquationRGB != mState.blendEquationAlpha)
{
mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
}
else
{
mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
}
}
else
{
mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
}
mBlendStateDirty = false;
}
if (mStencilStateDirty || mFrontFaceDirty)
{
if (mState.stencilTest && framebufferObject->hasStencil())
{
mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
// FIXME: Unsupported by D3D9
const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
if (mState.stencilWritemask != mState.stencilBackWritemask ||
mState.stencilRef != mState.stencilBackRef ||
mState.stencilMask != mState.stencilBackMask)
{
ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
return error(GL_INVALID_OPERATION);
}
// get the maximum size of the stencil ref
gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
es2dx::ConvertComparison(mState.stencilFunc));
mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
es2dx::ConvertStencilOp(mState.stencilFail));
mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
es2dx::ConvertComparison(mState.stencilBackFunc));
mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
es2dx::ConvertStencilOp(mState.stencilBackFail));
mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
}
else
{
mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
}
mStencilStateDirty = false;
mFrontFaceDirty = false;
}
if (mMaskStateDirty)
{
int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
mState.colorMaskBlue, mState.colorMaskAlpha);
if (colorMask == 0 && !zeroColorMaskAllowed)
{
// Enable green channel, but set blending so nothing will be drawn.
mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
}
else
{
mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
}
mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
mMaskStateDirty = false;
}
if (mPolygonOffsetStateDirty)
{
if (mState.polygonOffsetFill)
{
gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
if (depthbuffer)
{
mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
}
}
else
{
mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
}
mPolygonOffsetStateDirty = false;
}
if (mSampleStateDirty)
{
if (mState.sampleAlphaToCoverage)
{
FIXME("Sample alpha to coverage is unimplemented.");
}
mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
if (mState.sampleCoverage)
{
unsigned int mask = 0;
if (mState.sampleCoverageValue != 0)
{
float threshold = 0.5f;
for (int i = 0; i < framebufferObject->getSamples(); ++i)
{
mask <<= 1;
if ((i + 1) * mState.sampleCoverageValue >= threshold)
{
threshold += 1.0f;
mask |= 1;
}
}
}
if (mState.sampleCoverageInvert)
{
mask = ~mask;
}
mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
}
else
{
mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
}
mSampleStateDirty = false;
}
if (mDitherStateDirty)
{
mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
mDitherStateDirty = false;
}
}
GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
{
TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instances);
if (err != GL_NO_ERROR)
{
return err;
}
ProgramBinary *programBinary = getCurrentProgramBinary();
return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, repeatDraw);
}
// Applies the indices and element array bindings to the Direct3D 9 device
GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
{
GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
if (err == GL_NO_ERROR)
{
if (indexInfo->serial != mAppliedIBSerial)
{
mDevice->SetIndices(indexInfo->indexBuffer);
mAppliedIBSerial = indexInfo->serial;
}
}
return err;
}
// Applies the shaders and shader constants to the Direct3D 9 device
void Context::applyShaders()
{
ProgramBinary *programBinary = getCurrentProgramBinary();
if (programBinary->getSerial() != mAppliedProgramBinarySerial)
{
IDirect3DVertexShader9 *vertexShader = programBinary->getVertexShader();
IDirect3DPixelShader9 *pixelShader = programBinary->getPixelShader();
mDevice->SetPixelShader(pixelShader);
mDevice->SetVertexShader(vertexShader);
programBinary->dirtyAllUniforms();
mAppliedProgramBinarySerial = programBinary->getSerial();
}
programBinary->applyUniforms();
}
// Applies the textures and sampler states to the Direct3D 9 device
void Context::applyTextures()
{
applyTextures(SAMPLER_PIXEL);
if (mSupportsVertexTexture)
{
applyTextures(SAMPLER_VERTEX);
}
}
// For each Direct3D 9 sampler of either the pixel or vertex stage,
// looks up the corresponding OpenGL texture image unit and texture type,
// and sets the texture and its addressing/filtering state (or NULL when inactive).
void Context::applyTextures(SamplerType type)
{
ProgramBinary *programBinary = getCurrentProgramBinary();
int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
int samplerRange = programBinary->getUsedSamplerRange(type);
for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
{
int textureUnit = programBinary->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
int d3dSampler = samplerIndex + d3dSamplerOffset;
if (textureUnit != -1)
{
TextureType textureType = programBinary->getSamplerTextureType(type, samplerIndex);
Texture *texture = getSamplerTexture(textureUnit, textureType);
if (!texture->isSamplerComplete())
{
texture = getIncompleteTexture(textureType);
}
unsigned int texSerial = texture->getTextureSerial();
if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
{
IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
{
GLenum wrapS = texture->getWrapS();
GLenum wrapT = texture->getWrapT();
GLenum minFilter = texture->getMinFilter();
GLenum magFilter = texture->getMagFilter();
float maxAnisotropy = texture->getMaxAnisotropy();
mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter, maxAnisotropy));
D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter, maxAnisotropy);
mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, texture->getLodOffset());
if (supportsTextureFilterAnisotropy())
{
mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)maxAnisotropy);
}
}
if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
{
mDevice->SetTexture(d3dSampler, d3dTexture);
}
appliedTextureSerial[samplerIndex] = texSerial;
texture->resetDirty();
}
}
else
{
if (appliedTextureSerial[samplerIndex] != 0)
{
mDevice->SetTexture(d3dSampler, NULL);
appliedTextureSerial[samplerIndex] = 0;
}
}
}
for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
{
if (appliedTextureSerial[samplerIndex] != 0)
{
mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
appliedTextureSerial[samplerIndex] = 0;
}
}
}
void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
{
Framebuffer *framebuffer = getReadFramebuffer();
if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
{
return error(GL_INVALID_FRAMEBUFFER_OPERATION);
}
if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
{
return error(GL_INVALID_OPERATION);
}
GLsizei outputPitch = ComputePitch(width, ConvertSizedInternalFormat(format, type), mState.packAlignment);
// sized query sanity check
if (bufSize)
{
int requiredSize = outputPitch * height;
if (requiredSize > *bufSize)
{
return error(GL_INVALID_OPERATION);
}
}
IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
if (!renderTarget)
{
return; // Context must be lost, return silently
}
D3DSURFACE_DESC desc;
renderTarget->GetDesc(&desc);
if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
{
UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
renderTarget->Release();
return error(GL_OUT_OF_MEMORY);
}
HRESULT result;
IDirect3DSurface9 *systemSurface = NULL;
bool directToPixels = !getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
if (directToPixels)
{
// Use the pixels ptr as a shared handle to write directly into client's memory
result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
if (FAILED(result))
{
// Try again without the shared handle
directToPixels = false;
}
}
if (!directToPixels)
{
result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
renderTarget->Release();
return error(GL_OUT_OF_MEMORY);
}
}
result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
renderTarget->Release();
renderTarget = NULL;
if (FAILED(result))
{
systemSurface->Release();
// It turns out that D3D will sometimes produce more error
// codes than those documented.
if (checkDeviceLost(result))
return error(GL_OUT_OF_MEMORY);
else
{
UNREACHABLE();
return;
}
}
if (directToPixels)
{
systemSurface->Release();
return;
}
RECT rect;
rect.left = clamp(x, 0L, static_cast<LONG>(desc.Width));
rect.top = clamp(y, 0L, static_cast<LONG>(desc.Height));
rect.right = clamp(x + width, 0L, static_cast<LONG>(desc.Width));
rect.bottom = clamp(y + height, 0L, static_cast<LONG>(desc.Height));
D3DLOCKED_RECT lock;
result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
if (FAILED(result))
{
UNREACHABLE();
systemSurface->Release();
return; // No sensible error to generate
}
unsigned char *dest = (unsigned char*)pixels;
unsigned short *dest16 = (unsigned short*)pixels;
unsigned char *source;
int inputPitch;
if (getPackReverseRowOrder())
{
source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
inputPitch = -lock.Pitch;
}
else
{
source = (unsigned char*)lock.pBits;
inputPitch = lock.Pitch;
}
unsigned int fastPixelSize = 0;
if (desc.Format == D3DFMT_A8R8G8B8 &&
format == GL_BGRA_EXT &&
type == GL_UNSIGNED_BYTE)
{
fastPixelSize = 4;
}
else if ((desc.Format == D3DFMT_A4R4G4B4 &&
format == GL_BGRA_EXT &&
type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) ||
(desc.Format == D3DFMT_A1R5G5B5 &&
format == GL_BGRA_EXT &&
type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT))
{
fastPixelSize = 2;
}
else if (desc.Format == D3DFMT_A16B16G16R16F &&
format == GL_RGBA &&
type == GL_HALF_FLOAT_OES)
{
fastPixelSize = 8;
}
else if (desc.Format == D3DFMT_A32B32G32R32F &&
format == GL_RGBA &&
type == GL_FLOAT)
{
fastPixelSize = 16;
}
for (int j = 0; j < rect.bottom - rect.top; j++)
{
if (fastPixelSize != 0)
{
// Fast path for formats which require no translation:
// D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE
// D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT
// D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT
// D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES
// D3DFMT_A32B32G32R32F to RGBA/FLOAT
//
// Note that buffers with no alpha go through the slow path below.
memcpy(dest + j * outputPitch,
source + j * inputPitch,
(rect.right - rect.left) * fastPixelSize);
continue;
}
else if (desc.Format == D3DFMT_A8R8G8B8 &&
format == GL_RGBA &&
type == GL_UNSIGNED_BYTE)
{
// Fast path for swapping red with blue
for (int i = 0; i < rect.right - rect.left; i++)
{
unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
*(unsigned int*)(dest + 4 * i + j * outputPitch) =
(argb & 0xFF00FF00) | // Keep alpha and green
(argb & 0x00FF0000) >> 16 | // Move red to blue
(argb & 0x000000FF) << 16; // Move blue to red
}
continue;
}
for (int i = 0; i < rect.right - rect.left; i++)
{
float r;
float g;