1 #include "precompiled.h"
3 // Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
8 // State.cpp: Implements the State class, encapsulating raw GL state.
10 #include "libGLESv2/State.h"
12 #include "libGLESv2/Context.h"
13 #include "libGLESv2/VertexArray.h"
14 #include "libGLESv2/Query.h"
15 #include "libGLESv2/Framebuffer.h"
16 #include "libGLESv2/FramebufferAttachment.h"
17 #include "libGLESv2/renderer/RenderTarget.h"
18 #include "libGLESv2/formatutils.h"
26 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
28 mDepthClearValue = 1.0f;
29 mStencilClearValue = 0;
31 mRasterizer.rasterizerDiscard = false;
32 mRasterizer.cullFace = false;
33 mRasterizer.cullMode = GL_BACK;
34 mRasterizer.frontFace = GL_CCW;
35 mRasterizer.polygonOffsetFill = false;
36 mRasterizer.polygonOffsetFactor = 0.0f;
37 mRasterizer.polygonOffsetUnits = 0.0f;
38 mRasterizer.pointDrawMode = false;
39 mRasterizer.multiSample = false;
47 mBlend.sourceBlendRGB = GL_ONE;
48 mBlend.sourceBlendAlpha = GL_ONE;
49 mBlend.destBlendRGB = GL_ZERO;
50 mBlend.destBlendAlpha = GL_ZERO;
51 mBlend.blendEquationRGB = GL_FUNC_ADD;
52 mBlend.blendEquationAlpha = GL_FUNC_ADD;
53 mBlend.sampleAlphaToCoverage = false;
57 mBlendColor.green = 0;
59 mBlendColor.alpha = 0;
61 mDepthStencil.depthTest = false;
62 mDepthStencil.depthFunc = GL_LESS;
63 mDepthStencil.depthMask = true;
64 mDepthStencil.stencilTest = false;
65 mDepthStencil.stencilFunc = GL_ALWAYS;
66 mDepthStencil.stencilMask = -1;
67 mDepthStencil.stencilWritemask = -1;
68 mDepthStencil.stencilBackFunc = GL_ALWAYS;
69 mDepthStencil.stencilBackMask = - 1;
70 mDepthStencil.stencilBackWritemask = -1;
71 mDepthStencil.stencilFail = GL_KEEP;
72 mDepthStencil.stencilPassDepthFail = GL_KEEP;
73 mDepthStencil.stencilPassDepthPass = GL_KEEP;
74 mDepthStencil.stencilBackFail = GL_KEEP;
75 mDepthStencil.stencilBackPassDepthFail = GL_KEEP;
76 mDepthStencil.stencilBackPassDepthPass = GL_KEEP;
81 mSampleCoverage = false;
82 mSampleCoverageValue = 1.0f;
83 mSampleCoverageInvert = false;
84 mGenerateMipmapHint = GL_DONT_CARE;
85 mFragmentShaderDerivativeHint = GL_DONT_CARE;
96 mBlend.colorMaskRed = true;
97 mBlend.colorMaskGreen = true;
98 mBlend.colorMaskBlue = true;
99 mBlend.colorMaskAlpha = true;
101 const GLfloat defaultFloatValues[] = { 0.0f, 0.0f, 0.0f, 1.0f };
102 for (int attribIndex = 0; attribIndex < MAX_VERTEX_ATTRIBS; attribIndex++)
104 mVertexAttribCurrentValues[attribIndex].setFloatValues(defaultFloatValues);
107 for (unsigned int textureUnit = 0; textureUnit < ArraySize(mSamplers); textureUnit++)
109 mSamplers[textureUnit].set(NULL);
114 mActiveQueries[GL_ANY_SAMPLES_PASSED].set(NULL);
115 mActiveQueries[GL_ANY_SAMPLES_PASSED_CONSERVATIVE].set(NULL);
116 mActiveQueries[GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN].set(NULL);
118 mCurrentProgramId = 0;
119 mCurrentProgramBinary.set(NULL);
121 mReadFramebuffer = NULL;
122 mDrawFramebuffer = NULL;
127 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
129 for (int sampler = 0; sampler < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; sampler++)
131 mSamplerTexture[type][sampler].set(NULL);
135 const GLfloat defaultFloatValues[] = { 0.0f, 0.0f, 0.0f, 1.0f };
136 for (int attribIndex = 0; attribIndex < MAX_VERTEX_ATTRIBS; attribIndex++)
138 mVertexAttribCurrentValues[attribIndex].setFloatValues(defaultFloatValues);
141 mArrayBuffer.set(NULL);
142 mRenderbuffer.set(NULL);
144 mTransformFeedback.set(NULL);
146 for (State::ActiveQueryMap::iterator i = mActiveQueries.begin(); i != mActiveQueries.end(); i++)
151 mGenericUniformBuffer.set(NULL);
152 for (int i = 0; i < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; i++)
154 mUniformBuffers[i].set(NULL);
157 mGenericTransformFeedbackBuffer.set(NULL);
158 for (int i = 0; i < IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
160 mTransformFeedbackBuffers[i].set(NULL);
163 mCopyReadBuffer.set(NULL);
164 mCopyWriteBuffer.set(NULL);
166 mPack.pixelBuffer.set(NULL);
167 mUnpack.pixelBuffer.set(NULL);
170 const RasterizerState &State::getRasterizerState() const
175 const BlendState &State::getBlendState() const
180 const DepthStencilState &State::getDepthStencilState() const
182 return mDepthStencil;
185 void State::setClearColor(float red, float green, float blue, float alpha)
187 mColorClearValue.red = red;
188 mColorClearValue.green = green;
189 mColorClearValue.blue = blue;
190 mColorClearValue.alpha = alpha;
193 void State::setClearDepth(float depth)
195 mDepthClearValue = depth;
198 void State::setClearStencil(int stencil)
200 mStencilClearValue = stencil;
203 ClearParameters State::getClearParameters(GLbitfield mask) const
205 ClearParameters clearParams = { 0 };
206 for (unsigned int i = 0; i < ArraySize(clearParams.clearColor); i++)
208 clearParams.clearColor[i] = false;
210 clearParams.colorFClearValue = mColorClearValue;
211 clearParams.colorClearType = GL_FLOAT;
212 clearParams.colorMaskRed = mBlend.colorMaskRed;
213 clearParams.colorMaskGreen = mBlend.colorMaskGreen;
214 clearParams.colorMaskBlue = mBlend.colorMaskBlue;
215 clearParams.colorMaskAlpha = mBlend.colorMaskAlpha;
216 clearParams.clearDepth = false;
217 clearParams.depthClearValue = mDepthClearValue;
218 clearParams.clearStencil = false;
219 clearParams.stencilClearValue = mStencilClearValue;
220 clearParams.stencilWriteMask = mDepthStencil.stencilWritemask;
221 clearParams.scissorEnabled = mScissorTest;
222 clearParams.scissor = mScissor;
224 const Framebuffer *framebufferObject = getDrawFramebuffer();
225 if (mask & GL_COLOR_BUFFER_BIT)
227 if (framebufferObject->hasEnabledColorAttachment())
229 for (unsigned int i = 0; i < ArraySize(clearParams.clearColor); i++)
231 clearParams.clearColor[i] = true;
236 if (mask & GL_DEPTH_BUFFER_BIT)
238 if (mDepthStencil.depthMask && framebufferObject->getDepthbuffer() != NULL)
240 clearParams.clearDepth = true;
244 if (mask & GL_STENCIL_BUFFER_BIT)
246 if (framebufferObject->getStencilbuffer() != NULL)
248 rx::RenderTarget *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
251 ERR("Depth stencil pointer unexpectedly null.");
252 ClearParameters nullClearParam = { 0 };
253 return nullClearParam;
256 if (GetInternalFormatInfo(depthStencil->getActualFormat()).stencilBits > 0)
258 clearParams.clearStencil = true;
266 void State::setColorMask(bool red, bool green, bool blue, bool alpha)
268 mBlend.colorMaskRed = red;
269 mBlend.colorMaskGreen = green;
270 mBlend.colorMaskBlue = blue;
271 mBlend.colorMaskAlpha = alpha;
274 void State::setDepthMask(bool mask)
276 mDepthStencil.depthMask = mask;
279 bool State::isRasterizerDiscardEnabled() const
281 return mRasterizer.rasterizerDiscard;
284 void State::setRasterizerDiscard(bool enabled)
286 mRasterizer.rasterizerDiscard = enabled;
289 bool State::isCullFaceEnabled() const
291 return mRasterizer.cullFace;
294 void State::setCullFace(bool enabled)
296 mRasterizer.cullFace = enabled;
299 void State::setCullMode(GLenum mode)
301 mRasterizer.cullMode = mode;
304 void State::setFrontFace(GLenum front)
306 mRasterizer.frontFace = front;
309 bool State::isDepthTestEnabled() const
311 return mDepthStencil.depthTest;
314 void State::setDepthTest(bool enabled)
316 mDepthStencil.depthTest = enabled;
319 void State::setDepthFunc(GLenum depthFunc)
321 mDepthStencil.depthFunc = depthFunc;
324 void State::setDepthRange(float zNear, float zFar)
330 void State::getDepthRange(float *zNear, float *zFar) const
336 bool State::isBlendEnabled() const
341 void State::setBlend(bool enabled)
343 mBlend.blend = enabled;
346 void State::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
348 mBlend.sourceBlendRGB = sourceRGB;
349 mBlend.destBlendRGB = destRGB;
350 mBlend.sourceBlendAlpha = sourceAlpha;
351 mBlend.destBlendAlpha = destAlpha;
354 void State::setBlendColor(float red, float green, float blue, float alpha)
356 mBlendColor.red = red;
357 mBlendColor.green = green;
358 mBlendColor.blue = blue;
359 mBlendColor.alpha = alpha;
362 void State::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
364 mBlend.blendEquationRGB = rgbEquation;
365 mBlend.blendEquationAlpha = alphaEquation;
368 const ColorF &State::getBlendColor() const
373 bool State::isStencilTestEnabled() const
375 return mDepthStencil.stencilTest;
378 void State::setStencilTest(bool enabled)
380 mDepthStencil.stencilTest = enabled;
383 void State::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
385 mDepthStencil.stencilFunc = stencilFunc;
386 mStencilRef = (stencilRef > 0) ? stencilRef : 0;
387 mDepthStencil.stencilMask = stencilMask;
390 void State::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
392 mDepthStencil.stencilBackFunc = stencilBackFunc;
393 mStencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
394 mDepthStencil.stencilBackMask = stencilBackMask;
397 void State::setStencilWritemask(GLuint stencilWritemask)
399 mDepthStencil.stencilWritemask = stencilWritemask;
402 void State::setStencilBackWritemask(GLuint stencilBackWritemask)
404 mDepthStencil.stencilBackWritemask = stencilBackWritemask;
407 void State::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
409 mDepthStencil.stencilFail = stencilFail;
410 mDepthStencil.stencilPassDepthFail = stencilPassDepthFail;
411 mDepthStencil.stencilPassDepthPass = stencilPassDepthPass;
414 void State::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
416 mDepthStencil.stencilBackFail = stencilBackFail;
417 mDepthStencil.stencilBackPassDepthFail = stencilBackPassDepthFail;
418 mDepthStencil.stencilBackPassDepthPass = stencilBackPassDepthPass;
421 GLint State::getStencilRef() const
426 GLint State::getStencilBackRef() const
428 return mStencilBackRef;
431 bool State::isPolygonOffsetFillEnabled() const
433 return mRasterizer.polygonOffsetFill;
436 void State::setPolygonOffsetFill(bool enabled)
438 mRasterizer.polygonOffsetFill = enabled;
441 void State::setPolygonOffsetParams(GLfloat factor, GLfloat units)
443 // An application can pass NaN values here, so handle this gracefully
444 mRasterizer.polygonOffsetFactor = factor != factor ? 0.0f : factor;
445 mRasterizer.polygonOffsetUnits = units != units ? 0.0f : units;
448 bool State::isSampleAlphaToCoverageEnabled() const
450 return mBlend.sampleAlphaToCoverage;
453 void State::setSampleAlphaToCoverage(bool enabled)
455 mBlend.sampleAlphaToCoverage = enabled;
458 bool State::isSampleCoverageEnabled() const
460 return mSampleCoverage;
463 void State::setSampleCoverage(bool enabled)
465 mSampleCoverage = enabled;
468 void State::setSampleCoverageParams(GLclampf value, bool invert)
470 mSampleCoverageValue = value;
471 mSampleCoverageInvert = invert;
474 void State::getSampleCoverageParams(GLclampf *value, bool *invert)
476 ASSERT(value != NULL && invert != NULL);
478 *value = mSampleCoverageValue;
479 *invert = mSampleCoverageInvert;
482 bool State::isScissorTestEnabled() const
487 void State::setScissorTest(bool enabled)
489 mScissorTest = enabled;
492 void State::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
496 mScissor.width = width;
497 mScissor.height = height;
500 const Rectangle &State::getScissor() const
505 bool State::isDitherEnabled() const
507 return mBlend.dither;
510 void State::setDither(bool enabled)
512 mBlend.dither = enabled;
515 void State::setEnableFeature(GLenum feature, bool enabled)
519 case GL_CULL_FACE: setCullFace(enabled); break;
520 case GL_POLYGON_OFFSET_FILL: setPolygonOffsetFill(enabled); break;
521 case GL_SAMPLE_ALPHA_TO_COVERAGE: setSampleAlphaToCoverage(enabled); break;
522 case GL_SAMPLE_COVERAGE: setSampleCoverage(enabled); break;
523 case GL_SCISSOR_TEST: setScissorTest(enabled); break;
524 case GL_STENCIL_TEST: setStencilTest(enabled); break;
525 case GL_DEPTH_TEST: setDepthTest(enabled); break;
526 case GL_BLEND: setBlend(enabled); break;
527 case GL_DITHER: setDither(enabled); break;
528 case GL_PRIMITIVE_RESTART_FIXED_INDEX: UNIMPLEMENTED(); break;
529 case GL_RASTERIZER_DISCARD: setRasterizerDiscard(enabled); break;
530 default: UNREACHABLE();
534 bool State::getEnableFeature(GLenum feature)
538 case GL_CULL_FACE: return isCullFaceEnabled();
539 case GL_POLYGON_OFFSET_FILL: return isPolygonOffsetFillEnabled();
540 case GL_SAMPLE_ALPHA_TO_COVERAGE: return isSampleAlphaToCoverageEnabled();
541 case GL_SAMPLE_COVERAGE: return isSampleCoverageEnabled();
542 case GL_SCISSOR_TEST: return isScissorTestEnabled();
543 case GL_STENCIL_TEST: return isStencilTestEnabled();
544 case GL_DEPTH_TEST: return isDepthTestEnabled();
545 case GL_BLEND: return isBlendEnabled();
546 case GL_DITHER: return isDitherEnabled();
547 case GL_PRIMITIVE_RESTART_FIXED_INDEX: UNIMPLEMENTED(); return false;
548 case GL_RASTERIZER_DISCARD: return isRasterizerDiscardEnabled();
549 default: UNREACHABLE(); return false;
553 void State::setLineWidth(GLfloat width)
558 void State::setGenerateMipmapHint(GLenum hint)
560 mGenerateMipmapHint = hint;
563 void State::setFragmentShaderDerivativeHint(GLenum hint)
565 mFragmentShaderDerivativeHint = hint;
566 // TODO: Propagate the hint to shader translator so we can write
567 // ddx, ddx_coarse, or ddx_fine depending on the hint.
568 // Ignore for now. It is valid for implementations to ignore hint.
571 void State::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
575 mViewport.width = width;
576 mViewport.height = height;
579 const Rectangle &State::getViewport() const
584 void State::setActiveSampler(unsigned int active)
586 mActiveSampler = active;
589 unsigned int State::getActiveSampler() const
591 return mActiveSampler;
594 void State::setSamplerTexture(TextureType type, Texture *texture)
596 mSamplerTexture[type][mActiveSampler].set(texture);
599 Texture *State::getSamplerTexture(unsigned int sampler, TextureType type) const
601 GLuint texid = mSamplerTexture[type][sampler].id();
603 if (texid == 0) // Special case: 0 refers to default textures held by Context
608 return mSamplerTexture[type][sampler].get();
611 GLuint State::getSamplerTextureId(unsigned int sampler, TextureType type) const
613 return mSamplerTexture[type][sampler].id();
616 void State::detachTexture(GLuint texture)
618 // Textures have a detach method on State rather than a simple
619 // removeBinding, because the zero/null texture objects are managed
620 // separately, and don't have to go through the Context's maps or
621 // the ResourceManager.
623 // [OpenGL ES 2.0.24] section 3.8 page 84:
624 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
625 // rebound to texture object zero
627 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
629 for (int sampler = 0; sampler < IMPLEMENTATION_MAX_COMBINED_TEXTURE_IMAGE_UNITS; sampler++)
631 if (mSamplerTexture[type][sampler].id() == texture)
633 mSamplerTexture[type][sampler].set(NULL);
638 // [OpenGL ES 2.0.24] section 4.4 page 112:
639 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
640 // as if Texture2DAttachment had been called, with a texture of 0, for each attachment point to which this
641 // image was attached in the currently bound framebuffer.
643 if (mReadFramebuffer)
645 mReadFramebuffer->detachTexture(texture);
648 if (mDrawFramebuffer)
650 mDrawFramebuffer->detachTexture(texture);
654 void State::setSamplerBinding(GLuint textureUnit, Sampler *sampler)
656 mSamplers[textureUnit].set(sampler);
659 GLuint State::getSamplerId(GLuint textureUnit) const
661 ASSERT(textureUnit < ArraySize(mSamplers));
662 return mSamplers[textureUnit].id();
665 Sampler *State::getSampler(GLuint textureUnit) const
667 return mSamplers[textureUnit].get();
670 void State::detachSampler(GLuint sampler)
672 // [OpenGL ES 3.0.2] section 3.8.2 pages 123-124:
673 // If a sampler object that is currently bound to one or more texture units is
674 // deleted, it is as though BindSampler is called once for each texture unit to
675 // which the sampler is bound, with unit set to the texture unit and sampler set to zero.
676 for (unsigned int textureUnit = 0; textureUnit < ArraySize(mSamplers); textureUnit++)
678 if (mSamplers[textureUnit].id() == sampler)
680 mSamplers[textureUnit].set(NULL);
685 void State::setRenderbufferBinding(Renderbuffer *renderbuffer)
687 mRenderbuffer.set(renderbuffer);
690 GLuint State::getRenderbufferId() const
692 return mRenderbuffer.id();
695 Renderbuffer *State::getCurrentRenderbuffer()
697 return mRenderbuffer.get();
700 void State::detachRenderbuffer(GLuint renderbuffer)
702 // [OpenGL ES 2.0.24] section 4.4 page 109:
703 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
704 // had been executed with the target RENDERBUFFER and name of zero.
706 if (mRenderbuffer.id() == renderbuffer)
708 mRenderbuffer.set(NULL);
711 // [OpenGL ES 2.0.24] section 4.4 page 111:
712 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
713 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
714 // point to which this image was attached in the currently bound framebuffer.
716 Framebuffer *readFramebuffer = mReadFramebuffer;
717 Framebuffer *drawFramebuffer = mDrawFramebuffer;
721 readFramebuffer->detachRenderbuffer(renderbuffer);
724 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
726 drawFramebuffer->detachRenderbuffer(renderbuffer);
731 void State::setReadFramebufferBinding(Framebuffer *framebuffer)
733 mReadFramebuffer = framebuffer;
736 void State::setDrawFramebufferBinding(Framebuffer *framebuffer)
738 mDrawFramebuffer = framebuffer;
741 Framebuffer *State::getTargetFramebuffer(GLenum target) const
745 case GL_READ_FRAMEBUFFER_ANGLE: return mReadFramebuffer;
746 case GL_DRAW_FRAMEBUFFER_ANGLE:
747 case GL_FRAMEBUFFER: return mDrawFramebuffer;
748 default: UNREACHABLE(); return NULL;
752 Framebuffer *State::getReadFramebuffer()
754 return mReadFramebuffer;
757 Framebuffer *State::getDrawFramebuffer()
759 return mDrawFramebuffer;
762 const Framebuffer *State::getReadFramebuffer() const
764 return mReadFramebuffer;
767 const Framebuffer *State::getDrawFramebuffer() const
769 return mDrawFramebuffer;
772 bool State::removeReadFramebufferBinding(GLuint framebuffer)
774 if (mReadFramebuffer->id() == framebuffer)
776 mReadFramebuffer = NULL;
783 bool State::removeDrawFramebufferBinding(GLuint framebuffer)
785 if (mDrawFramebuffer->id() == framebuffer)
787 mDrawFramebuffer = NULL;
794 void State::setVertexArrayBinding(VertexArray *vertexArray)
796 mVertexArray = vertexArray;
799 GLuint State::getVertexArrayId() const
801 ASSERT(mVertexArray != NULL);
802 return mVertexArray->id();
805 VertexArray *State::getVertexArray() const
807 ASSERT(mVertexArray != NULL);
811 bool State::removeVertexArrayBinding(GLuint vertexArray)
813 if (mVertexArray->id() == vertexArray)
822 void State::setCurrentProgram(GLuint programId, Program *newProgram)
824 mCurrentProgramId = programId; // set new ID before trying to delete program binary; otherwise it will only be flagged for deletion
825 mCurrentProgramBinary.set(NULL);
829 newProgram->addRef();
830 mCurrentProgramBinary.set(newProgram->getProgramBinary());
834 void State::setCurrentProgramBinary(ProgramBinary *binary)
836 mCurrentProgramBinary.set(binary);
839 GLuint State::getCurrentProgramId() const
841 return mCurrentProgramId;
844 ProgramBinary *State::getCurrentProgramBinary() const
846 return mCurrentProgramBinary.get();
849 void State::setTransformFeedbackBinding(TransformFeedback *transformFeedback)
851 mTransformFeedback.set(transformFeedback);
854 TransformFeedback *State::getCurrentTransformFeedback() const
856 return mTransformFeedback.get();
859 void State::detachTransformFeedback(GLuint transformFeedback)
861 if (mTransformFeedback.id() == transformFeedback)
863 mTransformFeedback.set(NULL);
867 bool State::isQueryActive() const
869 for (State::ActiveQueryMap::const_iterator i = mActiveQueries.begin();
870 i != mActiveQueries.end(); i++)
872 if (i->second.get() != NULL)
881 void State::setActiveQuery(GLenum target, Query *query)
883 mActiveQueries[target].set(query);
886 GLuint State::getActiveQueryId(GLenum target) const
888 const Query *query = getActiveQuery(target);
889 return (query ? query->id() : 0u);
892 Query *State::getActiveQuery(GLenum target) const
894 // All query types should already exist in the activeQueries map
895 ASSERT(mActiveQueries.find(target) != mActiveQueries.end());
897 return mActiveQueries.at(target).get();
900 void State::setArrayBufferBinding(Buffer *buffer)
902 mArrayBuffer.set(buffer);
905 GLuint State::getArrayBufferId() const
907 return mArrayBuffer.id();
910 bool State::removeArrayBufferBinding(GLuint buffer)
912 if (mArrayBuffer.id() == buffer)
914 mArrayBuffer.set(NULL);
921 void State::setGenericUniformBufferBinding(Buffer *buffer)
923 mGenericUniformBuffer.set(buffer);
926 void State::setIndexedUniformBufferBinding(GLuint index, Buffer *buffer, GLintptr offset, GLsizeiptr size)
928 mUniformBuffers[index].set(buffer, offset, size);
931 GLuint State::getIndexedUniformBufferId(GLuint index) const
933 ASSERT(index < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS);
935 return mUniformBuffers[index].id();
938 Buffer *State::getIndexedUniformBuffer(GLuint index) const
940 ASSERT(index < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS);
942 return mUniformBuffers[index].get();
945 void State::setGenericTransformFeedbackBufferBinding(Buffer *buffer)
947 mGenericTransformFeedbackBuffer.set(buffer);
950 void State::setIndexedTransformFeedbackBufferBinding(GLuint index, Buffer *buffer, GLintptr offset, GLsizeiptr size)
952 mTransformFeedbackBuffers[index].set(buffer, offset, size);
955 GLuint State::getIndexedTransformFeedbackBufferId(GLuint index) const
957 ASSERT(index < IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS);
959 return mTransformFeedbackBuffers[index].id();
962 Buffer *State::getIndexedTransformFeedbackBuffer(GLuint index) const
964 ASSERT(index < IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS);
966 return mTransformFeedbackBuffers[index].get();
969 GLuint State::getIndexedTransformFeedbackBufferOffset(GLuint index) const
971 ASSERT(index < IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS);
973 return mTransformFeedbackBuffers[index].getOffset();
976 void State::setCopyReadBufferBinding(Buffer *buffer)
978 mCopyReadBuffer.set(buffer);
981 void State::setCopyWriteBufferBinding(Buffer *buffer)
983 mCopyWriteBuffer.set(buffer);
986 void State::setPixelPackBufferBinding(Buffer *buffer)
988 mPack.pixelBuffer.set(buffer);
991 void State::setPixelUnpackBufferBinding(Buffer *buffer)
993 mUnpack.pixelBuffer.set(buffer);
996 Buffer *State::getTargetBuffer(GLenum target) const
1000 case GL_ARRAY_BUFFER: return mArrayBuffer.get();
1001 case GL_COPY_READ_BUFFER: return mCopyReadBuffer.get();
1002 case GL_COPY_WRITE_BUFFER: return mCopyWriteBuffer.get();
1003 case GL_ELEMENT_ARRAY_BUFFER: return getVertexArray()->getElementArrayBuffer();
1004 case GL_PIXEL_PACK_BUFFER: return mPack.pixelBuffer.get();
1005 case GL_PIXEL_UNPACK_BUFFER: return mUnpack.pixelBuffer.get();
1006 case GL_TRANSFORM_FEEDBACK_BUFFER: return mGenericTransformFeedbackBuffer.get();
1007 case GL_UNIFORM_BUFFER: return mGenericUniformBuffer.get();
1008 default: UNREACHABLE(); return NULL;
1012 void State::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
1014 getVertexArray()->enableAttribute(attribNum, enabled);
1017 void State::setVertexAttribf(GLuint index, const GLfloat values[4])
1019 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
1020 mVertexAttribCurrentValues[index].setFloatValues(values);
1023 void State::setVertexAttribu(GLuint index, const GLuint values[4])
1025 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
1026 mVertexAttribCurrentValues[index].setUnsignedIntValues(values);
1029 void State::setVertexAttribi(GLuint index, const GLint values[4])
1031 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
1032 mVertexAttribCurrentValues[index].setIntValues(values);
1035 void State::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
1036 bool pureInteger, GLsizei stride, const void *pointer)
1038 getVertexArray()->setAttributeState(attribNum, boundBuffer, size, type, normalized, pureInteger, stride, pointer);
1041 const VertexAttribute &State::getVertexAttribState(unsigned int attribNum) const
1043 return getVertexArray()->getVertexAttribute(attribNum);
1046 const VertexAttribCurrentValueData &State::getVertexAttribCurrentValue(unsigned int attribNum) const
1048 ASSERT(attribNum < MAX_VERTEX_ATTRIBS);
1049 return mVertexAttribCurrentValues[attribNum];
1052 const VertexAttribCurrentValueData *State::getVertexAttribCurrentValues() const
1054 return mVertexAttribCurrentValues;
1057 const void *State::getVertexAttribPointer(unsigned int attribNum) const
1059 return getVertexArray()->getVertexAttribute(attribNum).pointer;
1062 void State::setPackAlignment(GLint alignment)
1064 mPack.alignment = alignment;
1067 GLint State::getPackAlignment() const
1069 return mPack.alignment;
1072 void State::setPackReverseRowOrder(bool reverseRowOrder)
1074 mPack.reverseRowOrder = reverseRowOrder;
1077 bool State::getPackReverseRowOrder() const
1079 return mPack.reverseRowOrder;
1082 const PixelPackState &State::getPackState() const
1087 void State::setUnpackAlignment(GLint alignment)
1089 mUnpack.alignment = alignment;
1092 GLint State::getUnpackAlignment() const
1094 return mUnpack.alignment;
1097 const PixelUnpackState &State::getUnpackState() const
1102 void State::getBooleanv(GLenum pname, GLboolean *params)
1106 case GL_SAMPLE_COVERAGE_INVERT: *params = mSampleCoverageInvert; break;
1107 case GL_DEPTH_WRITEMASK: *params = mDepthStencil.depthMask; break;
1108 case GL_COLOR_WRITEMASK:
1109 params[0] = mBlend.colorMaskRed;
1110 params[1] = mBlend.colorMaskGreen;
1111 params[2] = mBlend.colorMaskBlue;
1112 params[3] = mBlend.colorMaskAlpha;
1114 case GL_CULL_FACE: *params = mRasterizer.cullFace; break;
1115 case GL_POLYGON_OFFSET_FILL: *params = mRasterizer.polygonOffsetFill; break;
1116 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mBlend.sampleAlphaToCoverage; break;
1117 case GL_SAMPLE_COVERAGE: *params = mSampleCoverage; break;
1118 case GL_SCISSOR_TEST: *params = mScissorTest; break;
1119 case GL_STENCIL_TEST: *params = mDepthStencil.stencilTest; break;
1120 case GL_DEPTH_TEST: *params = mDepthStencil.depthTest; break;
1121 case GL_BLEND: *params = mBlend.blend; break;
1122 case GL_DITHER: *params = mBlend.dither; break;
1123 case GL_TRANSFORM_FEEDBACK_ACTIVE: *params = getCurrentTransformFeedback()->isStarted(); break;
1124 case GL_TRANSFORM_FEEDBACK_PAUSED: *params = getCurrentTransformFeedback()->isPaused(); break;
1131 void State::getFloatv(GLenum pname, GLfloat *params)
1133 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1134 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1135 // GetIntegerv as its native query function. As it would require conversion in any
1136 // case, this should make no difference to the calling application.
1139 case GL_LINE_WIDTH: *params = mLineWidth; break;
1140 case GL_SAMPLE_COVERAGE_VALUE: *params = mSampleCoverageValue; break;
1141 case GL_DEPTH_CLEAR_VALUE: *params = mDepthClearValue; break;
1142 case GL_POLYGON_OFFSET_FACTOR: *params = mRasterizer.polygonOffsetFactor; break;
1143 case GL_POLYGON_OFFSET_UNITS: *params = mRasterizer.polygonOffsetUnits; break;
1144 case GL_DEPTH_RANGE:
1148 case GL_COLOR_CLEAR_VALUE:
1149 params[0] = mColorClearValue.red;
1150 params[1] = mColorClearValue.green;
1151 params[2] = mColorClearValue.blue;
1152 params[3] = mColorClearValue.alpha;
1154 case GL_BLEND_COLOR:
1155 params[0] = mBlendColor.red;
1156 params[1] = mBlendColor.green;
1157 params[2] = mBlendColor.blue;
1158 params[3] = mBlendColor.alpha;
1166 void State::getIntegerv(GLenum pname, GLint *params)
1168 if (pname >= GL_DRAW_BUFFER0_EXT && pname <= GL_DRAW_BUFFER15_EXT)
1170 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0_EXT);
1171 ASSERT(colorAttachment < mContext->getCaps().maxDrawBuffers);
1172 Framebuffer *framebuffer = mDrawFramebuffer;
1173 *params = framebuffer->getDrawBufferState(colorAttachment);
1177 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1178 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1179 // GetIntegerv as its native query function. As it would require conversion in any
1180 // case, this should make no difference to the calling application. You may find it in
1181 // State::getFloatv.
1184 case GL_ARRAY_BUFFER_BINDING: *params = mArrayBuffer.id(); break;
1185 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = getVertexArray()->getElementArrayBufferId(); break;
1186 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1187 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mDrawFramebuffer->id(); break;
1188 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mReadFramebuffer->id(); break;
1189 case GL_RENDERBUFFER_BINDING: *params = mRenderbuffer.id(); break;
1190 case GL_VERTEX_ARRAY_BINDING: *params = mVertexArray->id(); break;
1191 case GL_CURRENT_PROGRAM: *params = mCurrentProgramId; break;
1192 case GL_PACK_ALIGNMENT: *params = mPack.alignment; break;
1193 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mPack.reverseRowOrder; break;
1194 case GL_UNPACK_ALIGNMENT: *params = mUnpack.alignment; break;
1195 case GL_GENERATE_MIPMAP_HINT: *params = mGenerateMipmapHint; break;
1196 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mFragmentShaderDerivativeHint; break;
1197 case GL_ACTIVE_TEXTURE: *params = (mActiveSampler + GL_TEXTURE0); break;
1198 case GL_STENCIL_FUNC: *params = mDepthStencil.stencilFunc; break;
1199 case GL_STENCIL_REF: *params = mStencilRef; break;
1200 case GL_STENCIL_VALUE_MASK: *params = clampToInt(mDepthStencil.stencilMask); break;
1201 case GL_STENCIL_BACK_FUNC: *params = mDepthStencil.stencilBackFunc; break;
1202 case GL_STENCIL_BACK_REF: *params = mStencilBackRef; break;
1203 case GL_STENCIL_BACK_VALUE_MASK: *params = clampToInt(mDepthStencil.stencilBackMask); break;
1204 case GL_STENCIL_FAIL: *params = mDepthStencil.stencilFail; break;
1205 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilPassDepthFail; break;
1206 case GL_STENCIL_PASS_DEPTH_PASS: *params = mDepthStencil.stencilPassDepthPass; break;
1207 case GL_STENCIL_BACK_FAIL: *params = mDepthStencil.stencilBackFail; break;
1208 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilBackPassDepthFail; break;
1209 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mDepthStencil.stencilBackPassDepthPass; break;
1210 case GL_DEPTH_FUNC: *params = mDepthStencil.depthFunc; break;
1211 case GL_BLEND_SRC_RGB: *params = mBlend.sourceBlendRGB; break;
1212 case GL_BLEND_SRC_ALPHA: *params = mBlend.sourceBlendAlpha; break;
1213 case GL_BLEND_DST_RGB: *params = mBlend.destBlendRGB; break;
1214 case GL_BLEND_DST_ALPHA: *params = mBlend.destBlendAlpha; break;
1215 case GL_BLEND_EQUATION_RGB: *params = mBlend.blendEquationRGB; break;
1216 case GL_BLEND_EQUATION_ALPHA: *params = mBlend.blendEquationAlpha; break;
1217 case GL_STENCIL_WRITEMASK: *params = clampToInt(mDepthStencil.stencilWritemask); break;
1218 case GL_STENCIL_BACK_WRITEMASK: *params = clampToInt(mDepthStencil.stencilBackWritemask); break;
1219 case GL_STENCIL_CLEAR_VALUE: *params = mStencilClearValue; break;
1220 case GL_SAMPLE_BUFFERS:
1223 gl::Framebuffer *framebuffer = mDrawFramebuffer;
1224 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1228 case GL_SAMPLE_BUFFERS:
1229 if (framebuffer->getSamples() != 0)
1239 *params = framebuffer->getSamples();
1250 params[0] = mViewport.x;
1251 params[1] = mViewport.y;
1252 params[2] = mViewport.width;
1253 params[3] = mViewport.height;
1255 case GL_SCISSOR_BOX:
1256 params[0] = mScissor.x;
1257 params[1] = mScissor.y;
1258 params[2] = mScissor.width;
1259 params[3] = mScissor.height;
1261 case GL_CULL_FACE_MODE: *params = mRasterizer.cullMode; break;
1262 case GL_FRONT_FACE: *params = mRasterizer.frontFace; break;
1268 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1269 gl::FramebufferAttachment *colorbuffer = framebuffer->getFirstColorbuffer();
1275 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1276 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1277 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1278 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1289 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1290 gl::FramebufferAttachment *depthbuffer = framebuffer->getDepthbuffer();
1294 *params = depthbuffer->getDepthSize();
1302 case GL_STENCIL_BITS:
1304 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1305 gl::FramebufferAttachment *stencilbuffer = framebuffer->getStencilbuffer();
1309 *params = stencilbuffer->getStencilSize();
1317 case GL_TEXTURE_BINDING_2D:
1318 ASSERT(mActiveSampler < mContext->getMaximumCombinedTextureImageUnits());
1319 *params = mSamplerTexture[TEXTURE_2D][mActiveSampler].id();
1321 case GL_TEXTURE_BINDING_CUBE_MAP:
1322 ASSERT(mActiveSampler < mContext->getMaximumCombinedTextureImageUnits());
1323 *params = mSamplerTexture[TEXTURE_CUBE][mActiveSampler].id();
1325 case GL_TEXTURE_BINDING_3D:
1326 ASSERT(mActiveSampler < mContext->getMaximumCombinedTextureImageUnits());
1327 *params = mSamplerTexture[TEXTURE_3D][mActiveSampler].id();
1329 case GL_TEXTURE_BINDING_2D_ARRAY:
1330 ASSERT(mActiveSampler < mContext->getMaximumCombinedTextureImageUnits());
1331 *params = mSamplerTexture[TEXTURE_2D_ARRAY][mActiveSampler].id();
1333 case GL_UNIFORM_BUFFER_BINDING:
1334 *params = mGenericUniformBuffer.id();
1336 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
1337 *params = mGenericTransformFeedbackBuffer.id();
1339 case GL_COPY_READ_BUFFER_BINDING:
1340 *params = mCopyReadBuffer.id();
1342 case GL_COPY_WRITE_BUFFER_BINDING:
1343 *params = mCopyWriteBuffer.id();
1345 case GL_PIXEL_PACK_BUFFER_BINDING:
1346 *params = mPack.pixelBuffer.id();
1348 case GL_PIXEL_UNPACK_BUFFER_BINDING:
1349 *params = mUnpack.pixelBuffer.id();
1357 bool State::getIndexedIntegerv(GLenum target, GLuint index, GLint *data)
1361 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
1362 if (index < IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS)
1364 *data = mTransformFeedbackBuffers[index].id();
1367 case GL_UNIFORM_BUFFER_BINDING:
1368 if (index < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS)
1370 *data = mUniformBuffers[index].id();
1380 bool State::getIndexedInteger64v(GLenum target, GLuint index, GLint64 *data)
1384 case GL_TRANSFORM_FEEDBACK_BUFFER_START:
1385 if (index < IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS)
1387 *data = mTransformFeedbackBuffers[index].getOffset();
1390 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE:
1391 if (index < IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS)
1393 *data = mTransformFeedbackBuffers[index].getSize();
1396 case GL_UNIFORM_BUFFER_START:
1397 if (index < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS)
1399 *data = mUniformBuffers[index].getOffset();
1402 case GL_UNIFORM_BUFFER_SIZE:
1403 if (index < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS)
1405 *data = mUniformBuffers[index].getSize();
1415 bool State::hasMappedBuffer(GLenum target) const
1417 if (target == GL_ARRAY_BUFFER)
1419 for (unsigned int attribIndex = 0; attribIndex < gl::MAX_VERTEX_ATTRIBS; attribIndex++)
1421 const gl::VertexAttribute &vertexAttrib = getVertexAttribState(attribIndex);
1422 gl::Buffer *boundBuffer = vertexAttrib.buffer.get();
1423 if (vertexAttrib.enabled && boundBuffer && boundBuffer->isMapped())
1433 Buffer *buffer = getTargetBuffer(target);
1434 return (buffer && buffer->isMapped());