1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // Context.cpp: Implements the es2::Context class, managing all GL state and performing
16 // rendering operations. It is the GLES2 specific implementation of EGLContext.
22 #include "utilities.h"
23 #include "ResourceManager.h"
26 #include "Framebuffer.h"
29 #include "Renderbuffer.h"
33 #include "TransformFeedback.h"
34 #include "VertexArray.h"
35 #include "VertexDataManager.h"
36 #include "IndexDataManager.h"
37 #include "libEGL/Display.h"
38 #include "libEGL/Surface.h"
39 #include "Common/Half.hpp"
41 #include <EGL/eglext.h>
45 Context::Context(egl::Display *display, const Context *shareContext, EGLint clientVersion)
46 : egl::Context(display), clientVersion(clientVersion)
48 sw::Context *context = new sw::Context();
49 device = new es2::Device(context);
51 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
53 mState.depthClearValue = 1.0f;
54 mState.stencilClearValue = 0;
56 mState.cullFaceEnabled = false;
57 mState.cullMode = GL_BACK;
58 mState.frontFace = GL_CCW;
59 mState.depthTestEnabled = false;
60 mState.depthFunc = GL_LESS;
61 mState.blendEnabled = false;
62 mState.sourceBlendRGB = GL_ONE;
63 mState.sourceBlendAlpha = GL_ONE;
64 mState.destBlendRGB = GL_ZERO;
65 mState.destBlendAlpha = GL_ZERO;
66 mState.blendEquationRGB = GL_FUNC_ADD;
67 mState.blendEquationAlpha = GL_FUNC_ADD;
68 mState.blendColor.red = 0;
69 mState.blendColor.green = 0;
70 mState.blendColor.blue = 0;
71 mState.blendColor.alpha = 0;
72 mState.stencilTestEnabled = false;
73 mState.stencilFunc = GL_ALWAYS;
74 mState.stencilRef = 0;
75 mState.stencilMask = -1;
76 mState.stencilWritemask = -1;
77 mState.stencilBackFunc = GL_ALWAYS;
78 mState.stencilBackRef = 0;
79 mState.stencilBackMask = - 1;
80 mState.stencilBackWritemask = -1;
81 mState.stencilFail = GL_KEEP;
82 mState.stencilPassDepthFail = GL_KEEP;
83 mState.stencilPassDepthPass = GL_KEEP;
84 mState.stencilBackFail = GL_KEEP;
85 mState.stencilBackPassDepthFail = GL_KEEP;
86 mState.stencilBackPassDepthPass = GL_KEEP;
87 mState.polygonOffsetFillEnabled = false;
88 mState.polygonOffsetFactor = 0.0f;
89 mState.polygonOffsetUnits = 0.0f;
90 mState.sampleAlphaToCoverageEnabled = false;
91 mState.sampleCoverageEnabled = false;
92 mState.sampleCoverageValue = 1.0f;
93 mState.sampleCoverageInvert = false;
94 mState.scissorTestEnabled = false;
95 mState.ditherEnabled = true;
96 mState.primitiveRestartFixedIndexEnabled = false;
97 mState.rasterizerDiscardEnabled = false;
98 mState.generateMipmapHint = GL_DONT_CARE;
99 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
101 mState.lineWidth = 1.0f;
103 mState.viewportX = 0;
104 mState.viewportY = 0;
105 mState.viewportWidth = 0;
106 mState.viewportHeight = 0;
112 mState.scissorWidth = 0;
113 mState.scissorHeight = 0;
115 mState.colorMaskRed = true;
116 mState.colorMaskGreen = true;
117 mState.colorMaskBlue = true;
118 mState.colorMaskAlpha = true;
119 mState.depthMask = true;
123 mResourceManager = shareContext->mResourceManager;
124 mResourceManager->addRef();
128 mResourceManager = new ResourceManager();
131 // [OpenGL ES 2.0.24] section 3.7 page 83:
132 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
133 // and cube map texture state vectors respectively associated with them.
134 // In order that access to these initial textures not be lost, they are treated as texture
135 // objects all of whose names are 0.
137 mTexture2DZero = new Texture2D(0);
138 mTexture3DZero = new Texture3D(0);
139 mTexture2DArrayZero = new Texture2DArray(0);
140 mTextureCubeMapZero = new TextureCubeMap(0);
141 mTextureExternalZero = new TextureExternal(0);
143 mState.activeSampler = 0;
146 bindElementArrayBuffer(0);
147 bindTextureCubeMap(0);
149 bindReadFramebuffer(0);
150 bindDrawFramebuffer(0);
152 bindGenericUniformBuffer(0);
153 bindTransformFeedback(0);
155 mState.currentProgram = 0;
157 mState.packAlignment = 4;
158 mState.unpackInfo.alignment = 4;
159 mState.packRowLength = 0;
160 mState.packImageHeight = 0;
161 mState.packSkipPixels = 0;
162 mState.packSkipRows = 0;
163 mState.packSkipImages = 0;
164 mState.unpackInfo.rowLength = 0;
165 mState.unpackInfo.imageHeight = 0;
166 mState.unpackInfo.skipPixels = 0;
167 mState.unpackInfo.skipRows = 0;
168 mState.unpackInfo.skipImages = 0;
170 mVertexDataManager = nullptr;
171 mIndexDataManager = nullptr;
173 mInvalidEnum = false;
174 mInvalidValue = false;
175 mInvalidOperation = false;
176 mOutOfMemory = false;
177 mInvalidFramebufferOperation = false;
179 mHasBeenCurrent = false;
186 if(mState.currentProgram != 0)
188 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
191 programObject->release();
193 mState.currentProgram = 0;
196 while(!mFramebufferNameSpace.empty())
198 deleteFramebuffer(mFramebufferNameSpace.firstName());
201 while(!mFenceNameSpace.empty())
203 deleteFence(mFenceNameSpace.firstName());
206 while(!mQueryNameSpace.empty())
208 deleteQuery(mQueryNameSpace.firstName());
211 while(!mVertexArrayNameSpace.empty())
213 deleteVertexArray(mVertexArrayNameSpace.firstName());
216 while(!mTransformFeedbackNameSpace.empty())
218 deleteTransformFeedback(mTransformFeedbackNameSpace.firstName());
221 for(int type = 0; type < TEXTURE_TYPE_COUNT; type++)
223 for(int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS; sampler++)
225 mState.samplerTexture[type][sampler] = nullptr;
229 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
231 mState.vertexAttribute[i].mBoundBuffer = nullptr;
234 for(int i = 0; i < QUERY_TYPE_COUNT; i++)
236 mState.activeQuery[i] = nullptr;
239 mState.arrayBuffer = nullptr;
240 mState.copyReadBuffer = nullptr;
241 mState.copyWriteBuffer = nullptr;
242 mState.pixelPackBuffer = nullptr;
243 mState.pixelUnpackBuffer = nullptr;
244 mState.genericUniformBuffer = nullptr;
245 mState.renderbuffer = nullptr;
247 for(int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i)
249 mState.sampler[i] = nullptr;
252 mTexture2DZero = nullptr;
253 mTexture3DZero = nullptr;
254 mTexture2DArrayZero = nullptr;
255 mTextureCubeMapZero = nullptr;
256 mTextureExternalZero = nullptr;
258 delete mVertexDataManager;
259 delete mIndexDataManager;
261 mResourceManager->release();
265 void Context::makeCurrent(egl::Surface *surface)
269 mVertexDataManager = new VertexDataManager(this);
270 mIndexDataManager = new IndexDataManager();
272 mState.viewportX = 0;
273 mState.viewportY = 0;
274 mState.viewportWidth = surface->getWidth();
275 mState.viewportHeight = surface->getHeight();
279 mState.scissorWidth = surface->getWidth();
280 mState.scissorHeight = surface->getHeight();
282 mHasBeenCurrent = true;
285 // Wrap the existing resources into GL objects and assign them to the '0' names
286 egl::Image *defaultRenderTarget = surface->getRenderTarget();
287 egl::Image *depthStencil = surface->getDepthStencil();
289 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
290 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
291 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
293 setFramebufferZero(framebufferZero);
295 if(defaultRenderTarget)
297 defaultRenderTarget->release();
302 depthStencil->release();
308 EGLint Context::getClientVersion() const
310 return clientVersion;
313 // This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
314 void Context::markAllStateDirty()
316 mAppliedProgramSerial = 0;
318 mDepthStateDirty = true;
319 mMaskStateDirty = true;
320 mBlendStateDirty = true;
321 mStencilStateDirty = true;
322 mPolygonOffsetStateDirty = true;
323 mSampleStateDirty = true;
324 mDitherStateDirty = true;
325 mFrontFaceDirty = true;
328 void Context::setClearColor(float red, float green, float blue, float alpha)
330 mState.colorClearValue.red = red;
331 mState.colorClearValue.green = green;
332 mState.colorClearValue.blue = blue;
333 mState.colorClearValue.alpha = alpha;
336 void Context::setClearDepth(float depth)
338 mState.depthClearValue = depth;
341 void Context::setClearStencil(int stencil)
343 mState.stencilClearValue = stencil;
346 void Context::setCullFaceEnabled(bool enabled)
348 mState.cullFaceEnabled = enabled;
351 bool Context::isCullFaceEnabled() const
353 return mState.cullFaceEnabled;
356 void Context::setCullMode(GLenum mode)
358 mState.cullMode = mode;
361 void Context::setFrontFace(GLenum front)
363 if(mState.frontFace != front)
365 mState.frontFace = front;
366 mFrontFaceDirty = true;
370 void Context::setDepthTestEnabled(bool enabled)
372 if(mState.depthTestEnabled != enabled)
374 mState.depthTestEnabled = enabled;
375 mDepthStateDirty = true;
379 bool Context::isDepthTestEnabled() const
381 return mState.depthTestEnabled;
384 void Context::setDepthFunc(GLenum depthFunc)
386 if(mState.depthFunc != depthFunc)
388 mState.depthFunc = depthFunc;
389 mDepthStateDirty = true;
393 void Context::setDepthRange(float zNear, float zFar)
395 mState.zNear = zNear;
399 void Context::setBlendEnabled(bool enabled)
401 if(mState.blendEnabled != enabled)
403 mState.blendEnabled = enabled;
404 mBlendStateDirty = true;
408 bool Context::isBlendEnabled() const
410 return mState.blendEnabled;
413 void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
415 if(mState.sourceBlendRGB != sourceRGB ||
416 mState.sourceBlendAlpha != sourceAlpha ||
417 mState.destBlendRGB != destRGB ||
418 mState.destBlendAlpha != destAlpha)
420 mState.sourceBlendRGB = sourceRGB;
421 mState.destBlendRGB = destRGB;
422 mState.sourceBlendAlpha = sourceAlpha;
423 mState.destBlendAlpha = destAlpha;
424 mBlendStateDirty = true;
428 void Context::setBlendColor(float red, float green, float blue, float alpha)
430 if(mState.blendColor.red != red ||
431 mState.blendColor.green != green ||
432 mState.blendColor.blue != blue ||
433 mState.blendColor.alpha != alpha)
435 mState.blendColor.red = red;
436 mState.blendColor.green = green;
437 mState.blendColor.blue = blue;
438 mState.blendColor.alpha = alpha;
439 mBlendStateDirty = true;
443 void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
445 if(mState.blendEquationRGB != rgbEquation ||
446 mState.blendEquationAlpha != alphaEquation)
448 mState.blendEquationRGB = rgbEquation;
449 mState.blendEquationAlpha = alphaEquation;
450 mBlendStateDirty = true;
454 void Context::setStencilTestEnabled(bool enabled)
456 if(mState.stencilTestEnabled != enabled)
458 mState.stencilTestEnabled = enabled;
459 mStencilStateDirty = true;
463 bool Context::isStencilTestEnabled() const
465 return mState.stencilTestEnabled;
468 void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
470 if(mState.stencilFunc != stencilFunc ||
471 mState.stencilRef != stencilRef ||
472 mState.stencilMask != stencilMask)
474 mState.stencilFunc = stencilFunc;
475 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
476 mState.stencilMask = stencilMask;
477 mStencilStateDirty = true;
481 void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
483 if(mState.stencilBackFunc != stencilBackFunc ||
484 mState.stencilBackRef != stencilBackRef ||
485 mState.stencilBackMask != stencilBackMask)
487 mState.stencilBackFunc = stencilBackFunc;
488 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
489 mState.stencilBackMask = stencilBackMask;
490 mStencilStateDirty = true;
494 void Context::setStencilWritemask(GLuint stencilWritemask)
496 if(mState.stencilWritemask != stencilWritemask)
498 mState.stencilWritemask = stencilWritemask;
499 mStencilStateDirty = true;
503 void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
505 if(mState.stencilBackWritemask != stencilBackWritemask)
507 mState.stencilBackWritemask = stencilBackWritemask;
508 mStencilStateDirty = true;
512 void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
514 if(mState.stencilFail != stencilFail ||
515 mState.stencilPassDepthFail != stencilPassDepthFail ||
516 mState.stencilPassDepthPass != stencilPassDepthPass)
518 mState.stencilFail = stencilFail;
519 mState.stencilPassDepthFail = stencilPassDepthFail;
520 mState.stencilPassDepthPass = stencilPassDepthPass;
521 mStencilStateDirty = true;
525 void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
527 if(mState.stencilBackFail != stencilBackFail ||
528 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
529 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
531 mState.stencilBackFail = stencilBackFail;
532 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
533 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
534 mStencilStateDirty = true;
538 void Context::setPolygonOffsetFillEnabled(bool enabled)
540 if(mState.polygonOffsetFillEnabled != enabled)
542 mState.polygonOffsetFillEnabled = enabled;
543 mPolygonOffsetStateDirty = true;
547 bool Context::isPolygonOffsetFillEnabled() const
549 return mState.polygonOffsetFillEnabled;
552 void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
554 if(mState.polygonOffsetFactor != factor ||
555 mState.polygonOffsetUnits != units)
557 mState.polygonOffsetFactor = factor;
558 mState.polygonOffsetUnits = units;
559 mPolygonOffsetStateDirty = true;
563 void Context::setSampleAlphaToCoverageEnabled(bool enabled)
565 if(mState.sampleAlphaToCoverageEnabled != enabled)
567 mState.sampleAlphaToCoverageEnabled = enabled;
568 mSampleStateDirty = true;
572 bool Context::isSampleAlphaToCoverageEnabled() const
574 return mState.sampleAlphaToCoverageEnabled;
577 void Context::setSampleCoverageEnabled(bool enabled)
579 if(mState.sampleCoverageEnabled != enabled)
581 mState.sampleCoverageEnabled = enabled;
582 mSampleStateDirty = true;
586 bool Context::isSampleCoverageEnabled() const
588 return mState.sampleCoverageEnabled;
591 void Context::setSampleCoverageParams(GLclampf value, bool invert)
593 if(mState.sampleCoverageValue != value ||
594 mState.sampleCoverageInvert != invert)
596 mState.sampleCoverageValue = value;
597 mState.sampleCoverageInvert = invert;
598 mSampleStateDirty = true;
602 void Context::setScissorTestEnabled(bool enabled)
604 mState.scissorTestEnabled = enabled;
607 bool Context::isScissorTestEnabled() const
609 return mState.scissorTestEnabled;
612 void Context::setDitherEnabled(bool enabled)
614 if(mState.ditherEnabled != enabled)
616 mState.ditherEnabled = enabled;
617 mDitherStateDirty = true;
621 bool Context::isDitherEnabled() const
623 return mState.ditherEnabled;
626 void Context::setPrimitiveRestartFixedIndexEnabled(bool enabled)
629 mState.primitiveRestartFixedIndexEnabled = enabled;
632 bool Context::isPrimitiveRestartFixedIndexEnabled() const
634 return mState.primitiveRestartFixedIndexEnabled;
637 void Context::setRasterizerDiscardEnabled(bool enabled)
639 mState.rasterizerDiscardEnabled = enabled;
642 bool Context::isRasterizerDiscardEnabled() const
644 return mState.rasterizerDiscardEnabled;
647 void Context::setLineWidth(GLfloat width)
649 mState.lineWidth = width;
650 device->setLineWidth(clamp(width, ALIASED_LINE_WIDTH_RANGE_MIN, ALIASED_LINE_WIDTH_RANGE_MAX));
653 void Context::setGenerateMipmapHint(GLenum hint)
655 mState.generateMipmapHint = hint;
658 void Context::setFragmentShaderDerivativeHint(GLenum hint)
660 mState.fragmentShaderDerivativeHint = hint;
661 // TODO: Propagate the hint to shader translator so we can write
662 // ddx, ddx_coarse, or ddx_fine depending on the hint.
663 // Ignore for now. It is valid for implementations to ignore hint.
666 void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
668 mState.viewportX = x;
669 mState.viewportY = y;
670 mState.viewportWidth = width;
671 mState.viewportHeight = height;
674 void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
678 mState.scissorWidth = width;
679 mState.scissorHeight = height;
682 void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
684 if(mState.colorMaskRed != red || mState.colorMaskGreen != green ||
685 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
687 mState.colorMaskRed = red;
688 mState.colorMaskGreen = green;
689 mState.colorMaskBlue = blue;
690 mState.colorMaskAlpha = alpha;
691 mMaskStateDirty = true;
695 unsigned int Context::getColorMask() const
697 return (mState.colorMaskRed ? 0x1 : 0) |
698 (mState.colorMaskGreen ? 0x2 : 0) |
699 (mState.colorMaskBlue ? 0x4 : 0) |
700 (mState.colorMaskAlpha ? 0x8 : 0);
703 void Context::setDepthMask(bool mask)
705 if(mState.depthMask != mask)
707 mState.depthMask = mask;
708 mMaskStateDirty = true;
712 void Context::setActiveSampler(unsigned int active)
714 mState.activeSampler = active;
717 GLuint Context::getReadFramebufferName() const
719 return mState.readFramebuffer;
722 GLuint Context::getDrawFramebufferName() const
724 return mState.drawFramebuffer;
727 GLuint Context::getRenderbufferName() const
729 return mState.renderbuffer.name();
732 void Context::setFramebufferReadBuffer(GLuint buf)
734 getReadFramebuffer()->setReadBuffer(buf);
737 void Context::setFramebufferDrawBuffers(GLsizei n, const GLenum *bufs)
739 Framebuffer *drawFramebuffer = getDrawFramebuffer();
741 for(int i = 0; i < MAX_COLOR_ATTACHMENTS; i++)
743 drawFramebuffer->setDrawBuffer(i, (i < n) ? bufs[i] : GL_NONE);
747 GLuint Context::getReadFramebufferColorIndex() const
749 GLenum buf = getReadFramebuffer()->getReadBuffer();
755 return GL_INVALID_INDEX;
757 return buf - GL_COLOR_ATTACHMENT0;
761 GLuint Context::getArrayBufferName() const
763 return mState.arrayBuffer.name();
766 GLuint Context::getElementArrayBufferName() const
768 Buffer* elementArrayBuffer = getCurrentVertexArray()->getElementArrayBuffer();
769 return elementArrayBuffer ? elementArrayBuffer->name : 0;
772 GLuint Context::getActiveQuery(GLenum target) const
774 Query *queryObject = nullptr;
778 case GL_ANY_SAMPLES_PASSED_EXT:
779 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED];
781 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
782 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE];
784 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
785 queryObject = mState.activeQuery[QUERY_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN];
793 return queryObject->name;
799 void Context::setVertexAttribArrayEnabled(unsigned int attribNum, bool enabled)
801 getCurrentVertexArray()->enableAttribute(attribNum, enabled);
804 void Context::setVertexAttribDivisor(unsigned int attribNum, GLuint divisor)
806 getCurrentVertexArray()->setVertexAttribDivisor(attribNum, divisor);
809 const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum) const
811 return getCurrentVertexArray()->getVertexAttribute(attribNum);
814 void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
815 GLsizei stride, const void *pointer)
817 getCurrentVertexArray()->setAttributeState(attribNum, boundBuffer, size, type, normalized, stride, pointer);
820 const void *Context::getVertexAttribPointer(unsigned int attribNum) const
822 return getCurrentVertexArray()->getVertexAttribute(attribNum).mPointer;
825 const VertexAttributeArray &Context::getVertexArrayAttributes()
827 return getCurrentVertexArray()->getVertexAttributes();
830 const VertexAttributeArray &Context::getCurrentVertexAttributes()
832 return mState.vertexAttribute;
835 void Context::setPackAlignment(GLint alignment)
837 mState.packAlignment = alignment;
840 void Context::setUnpackAlignment(GLint alignment)
842 mState.unpackInfo.alignment = alignment;
845 const egl::Image::UnpackInfo& Context::getUnpackInfo() const
847 return mState.unpackInfo;
850 void Context::setPackRowLength(GLint rowLength)
852 mState.packRowLength = rowLength;
855 void Context::setPackImageHeight(GLint imageHeight)
857 mState.packImageHeight = imageHeight;
860 void Context::setPackSkipPixels(GLint skipPixels)
862 mState.packSkipPixels = skipPixels;
865 void Context::setPackSkipRows(GLint skipRows)
867 mState.packSkipRows = skipRows;
870 void Context::setPackSkipImages(GLint skipImages)
872 mState.packSkipImages = skipImages;
875 void Context::setUnpackRowLength(GLint rowLength)
877 mState.unpackInfo.rowLength = rowLength;
880 void Context::setUnpackImageHeight(GLint imageHeight)
882 mState.unpackInfo.imageHeight = imageHeight;
885 void Context::setUnpackSkipPixels(GLint skipPixels)
887 mState.unpackInfo.skipPixels = skipPixels;
890 void Context::setUnpackSkipRows(GLint skipRows)
892 mState.unpackInfo.skipRows = skipRows;
895 void Context::setUnpackSkipImages(GLint skipImages)
897 mState.unpackInfo.skipImages = skipImages;
900 GLuint Context::createBuffer()
902 return mResourceManager->createBuffer();
905 GLuint Context::createProgram()
907 return mResourceManager->createProgram();
910 GLuint Context::createShader(GLenum type)
912 return mResourceManager->createShader(type);
915 GLuint Context::createTexture()
917 return mResourceManager->createTexture();
920 GLuint Context::createRenderbuffer()
922 return mResourceManager->createRenderbuffer();
925 // Returns an unused framebuffer name
926 GLuint Context::createFramebuffer()
928 return mFramebufferNameSpace.allocate();
931 GLuint Context::createFence()
933 return mFenceNameSpace.allocate(new Fence());
936 // Returns an unused query name
937 GLuint Context::createQuery()
939 return mQueryNameSpace.allocate();
942 // Returns an unused vertex array name
943 GLuint Context::createVertexArray()
945 return mVertexArrayNameSpace.allocate();
948 GLsync Context::createFenceSync(GLenum condition, GLbitfield flags)
950 GLuint handle = mResourceManager->createFenceSync(condition, flags);
952 return reinterpret_cast<GLsync>(static_cast<uintptr_t>(handle));
955 // Returns an unused transform feedback name
956 GLuint Context::createTransformFeedback()
958 return mTransformFeedbackNameSpace.allocate();
961 // Returns an unused sampler name
962 GLuint Context::createSampler()
964 return mResourceManager->createSampler();
967 void Context::deleteBuffer(GLuint buffer)
969 detachBuffer(buffer);
971 mResourceManager->deleteBuffer(buffer);
974 void Context::deleteShader(GLuint shader)
976 mResourceManager->deleteShader(shader);
979 void Context::deleteProgram(GLuint program)
981 mResourceManager->deleteProgram(program);
984 void Context::deleteTexture(GLuint texture)
986 detachTexture(texture);
988 mResourceManager->deleteTexture(texture);
991 void Context::deleteRenderbuffer(GLuint renderbuffer)
993 if(mResourceManager->getRenderbuffer(renderbuffer))
995 detachRenderbuffer(renderbuffer);
998 mResourceManager->deleteRenderbuffer(renderbuffer);
1001 void Context::deleteFramebuffer(GLuint framebuffer)
1003 detachFramebuffer(framebuffer);
1005 Framebuffer *framebufferObject = mFramebufferNameSpace.remove(framebuffer);
1007 if(framebufferObject)
1009 delete framebufferObject;
1013 void Context::deleteFence(GLuint fence)
1015 Fence *fenceObject = mFenceNameSpace.remove(fence);
1023 void Context::deleteQuery(GLuint query)
1025 Query *queryObject = mQueryNameSpace.remove(query);
1029 queryObject->release();
1033 void Context::deleteVertexArray(GLuint vertexArray)
1035 // [OpenGL ES 3.0.2] section 2.10 page 43:
1036 // If a vertex array object that is currently bound is deleted, the binding
1037 // for that object reverts to zero and the default vertex array becomes current.
1038 if(getCurrentVertexArray()->name == vertexArray)
1043 VertexArray *vertexArrayObject = mVertexArrayNameSpace.remove(vertexArray);
1045 if(vertexArrayObject)
1047 delete vertexArrayObject;
1051 void Context::deleteFenceSync(GLsync fenceSync)
1053 // The spec specifies the underlying Fence object is not deleted until all current
1054 // wait commands finish. However, since the name becomes invalid, we cannot query the fence,
1055 // and since our API is currently designed for being called from a single thread, we can delete
1056 // the fence immediately.
1057 mResourceManager->deleteFenceSync(static_cast<GLuint>(reinterpret_cast<uintptr_t>(fenceSync)));
1060 void Context::deleteTransformFeedback(GLuint transformFeedback)
1062 TransformFeedback *transformFeedbackObject = mTransformFeedbackNameSpace.remove(transformFeedback);
1064 if(transformFeedbackObject)
1066 delete transformFeedbackObject;
1070 void Context::deleteSampler(GLuint sampler)
1072 detachSampler(sampler);
1074 mResourceManager->deleteSampler(sampler);
1077 Buffer *Context::getBuffer(GLuint handle) const
1079 return mResourceManager->getBuffer(handle);
1082 Shader *Context::getShader(GLuint handle) const
1084 return mResourceManager->getShader(handle);
1087 Program *Context::getProgram(GLuint handle) const
1089 return mResourceManager->getProgram(handle);
1092 Texture *Context::getTexture(GLuint handle) const
1094 return mResourceManager->getTexture(handle);
1097 Renderbuffer *Context::getRenderbuffer(GLuint handle) const
1099 return mResourceManager->getRenderbuffer(handle);
1102 Framebuffer *Context::getReadFramebuffer() const
1104 return getFramebuffer(mState.readFramebuffer);
1107 Framebuffer *Context::getDrawFramebuffer() const
1109 return getFramebuffer(mState.drawFramebuffer);
1112 void Context::bindArrayBuffer(unsigned int buffer)
1114 mResourceManager->checkBufferAllocation(buffer);
1116 mState.arrayBuffer = getBuffer(buffer);
1119 void Context::bindElementArrayBuffer(unsigned int buffer)
1121 mResourceManager->checkBufferAllocation(buffer);
1123 getCurrentVertexArray()->setElementArrayBuffer(getBuffer(buffer));
1126 void Context::bindCopyReadBuffer(GLuint buffer)
1128 mResourceManager->checkBufferAllocation(buffer);
1130 mState.copyReadBuffer = getBuffer(buffer);
1133 void Context::bindCopyWriteBuffer(GLuint buffer)
1135 mResourceManager->checkBufferAllocation(buffer);
1137 mState.copyWriteBuffer = getBuffer(buffer);
1140 void Context::bindPixelPackBuffer(GLuint buffer)
1142 mResourceManager->checkBufferAllocation(buffer);
1144 mState.pixelPackBuffer = getBuffer(buffer);
1147 void Context::bindPixelUnpackBuffer(GLuint buffer)
1149 mResourceManager->checkBufferAllocation(buffer);
1151 mState.pixelUnpackBuffer = getBuffer(buffer);
1154 void Context::bindTransformFeedbackBuffer(GLuint buffer)
1156 mResourceManager->checkBufferAllocation(buffer);
1158 TransformFeedback* transformFeedback = getTransformFeedback(mState.transformFeedback);
1160 if(transformFeedback)
1162 transformFeedback->setGenericBuffer(getBuffer(buffer));
1166 void Context::bindTexture2D(GLuint texture)
1168 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
1170 mState.samplerTexture[TEXTURE_2D][mState.activeSampler] = getTexture(texture);
1173 void Context::bindTextureCubeMap(GLuint texture)
1175 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
1177 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler] = getTexture(texture);
1180 void Context::bindTextureExternal(GLuint texture)
1182 mResourceManager->checkTextureAllocation(texture, TEXTURE_EXTERNAL);
1184 mState.samplerTexture[TEXTURE_EXTERNAL][mState.activeSampler] = getTexture(texture);
1187 void Context::bindTexture3D(GLuint texture)
1189 mResourceManager->checkTextureAllocation(texture, TEXTURE_3D);
1191 mState.samplerTexture[TEXTURE_3D][mState.activeSampler] = getTexture(texture);
1194 void Context::bindTexture2DArray(GLuint texture)
1196 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D_ARRAY);
1198 mState.samplerTexture[TEXTURE_2D_ARRAY][mState.activeSampler] = getTexture(texture);
1201 void Context::bindReadFramebuffer(GLuint framebuffer)
1203 if(!getFramebuffer(framebuffer))
1205 mFramebufferNameSpace.insert(framebuffer, new Framebuffer());
1208 mState.readFramebuffer = framebuffer;
1211 void Context::bindDrawFramebuffer(GLuint framebuffer)
1213 if(!getFramebuffer(framebuffer))
1215 mFramebufferNameSpace.insert(framebuffer, new Framebuffer());
1218 mState.drawFramebuffer = framebuffer;
1221 void Context::bindRenderbuffer(GLuint renderbuffer)
1223 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1225 mState.renderbuffer = getRenderbuffer(renderbuffer);
1228 void Context::bindVertexArray(GLuint array)
1230 VertexArray *vertexArray = getVertexArray(array);
1234 vertexArray = new VertexArray(array);
1235 mVertexArrayNameSpace.insert(array, vertexArray);
1238 mState.vertexArray = array;
1241 void Context::bindGenericUniformBuffer(GLuint buffer)
1243 mResourceManager->checkBufferAllocation(buffer);
1245 mState.genericUniformBuffer = getBuffer(buffer);
1248 void Context::bindIndexedUniformBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size)
1250 mResourceManager->checkBufferAllocation(buffer);
1252 Buffer* bufferObject = getBuffer(buffer);
1253 mState.uniformBuffers[index].set(bufferObject, offset, size);
1256 void Context::bindGenericTransformFeedbackBuffer(GLuint buffer)
1258 mResourceManager->checkBufferAllocation(buffer);
1260 getTransformFeedback()->setGenericBuffer(getBuffer(buffer));
1263 void Context::bindIndexedTransformFeedbackBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size)
1265 mResourceManager->checkBufferAllocation(buffer);
1267 Buffer* bufferObject = getBuffer(buffer);
1268 getTransformFeedback()->setBuffer(index, bufferObject, offset, size);
1271 void Context::bindTransformFeedback(GLuint id)
1273 if(!getTransformFeedback(id))
1275 mTransformFeedbackNameSpace.insert(id, new TransformFeedback(id));
1278 mState.transformFeedback = id;
1281 bool Context::bindSampler(GLuint unit, GLuint sampler)
1283 mResourceManager->checkSamplerAllocation(sampler);
1285 Sampler* samplerObject = getSampler(sampler);
1289 mState.sampler[unit] = samplerObject;
1292 return !!samplerObject;
1295 void Context::useProgram(GLuint program)
1297 GLuint priorProgram = mState.currentProgram;
1298 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
1300 if(priorProgram != program)
1302 Program *newProgram = mResourceManager->getProgram(program);
1303 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1307 newProgram->addRef();
1312 oldProgram->release();
1317 void Context::beginQuery(GLenum target, GLuint query)
1319 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1320 // of zero, if the active query object name for <target> is non-zero (for the
1321 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1322 // the active query for either target is non-zero), if <id> is the name of an
1323 // existing query object whose type does not match <target>, or if <id> is the
1324 // active query object name for any query type, the error INVALID_OPERATION is
1327 // Ensure no other queries are active
1328 // NOTE: If other queries than occlusion are supported, we will need to check
1330 // a) The query ID passed is not the current active query for any target/type
1331 // b) There are no active queries for the requested target (and in the case
1332 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1333 // no query may be active for either if glBeginQuery targets either.
1334 for(int i = 0; i < QUERY_TYPE_COUNT; i++)
1336 if(mState.activeQuery[i])
1338 return error(GL_INVALID_OPERATION);
1345 case GL_ANY_SAMPLES_PASSED_EXT:
1346 qType = QUERY_ANY_SAMPLES_PASSED;
1348 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1349 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1351 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
1352 qType = QUERY_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN;
1358 Query *queryObject = createQuery(query, target);
1360 // Check that name was obtained with glGenQueries
1363 return error(GL_INVALID_OPERATION);
1366 // Check for type mismatch
1367 if(queryObject->getType() != target)
1369 return error(GL_INVALID_OPERATION);
1372 // Set query as active for specified target
1373 mState.activeQuery[qType] = queryObject;
1376 queryObject->begin();
1379 void Context::endQuery(GLenum target)
1385 case GL_ANY_SAMPLES_PASSED_EXT: qType = QUERY_ANY_SAMPLES_PASSED; break;
1386 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT: qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE; break;
1387 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN: qType = QUERY_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN; break;
1388 default: UNREACHABLE(target); return;
1391 Query *queryObject = mState.activeQuery[qType];
1395 return error(GL_INVALID_OPERATION);
1400 mState.activeQuery[qType] = nullptr;
1403 void Context::setFramebufferZero(Framebuffer *buffer)
1405 delete mFramebufferNameSpace.remove(0);
1406 mFramebufferNameSpace.insert(0, buffer);
1409 void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
1411 Renderbuffer *renderbufferObject = mState.renderbuffer;
1412 renderbufferObject->setStorage(renderbuffer);
1415 Framebuffer *Context::getFramebuffer(unsigned int handle) const
1417 return mFramebufferNameSpace.find(handle);
1420 Fence *Context::getFence(unsigned int handle) const
1422 return mFenceNameSpace.find(handle);
1425 FenceSync *Context::getFenceSync(GLsync handle) const
1427 return mResourceManager->getFenceSync(static_cast<GLuint>(reinterpret_cast<uintptr_t>(handle)));
1430 Query *Context::getQuery(unsigned int handle) const
1432 return mQueryNameSpace.find(handle);
1435 Query *Context::createQuery(unsigned int handle, GLenum type)
1437 if(!mQueryNameSpace.isReserved(handle))
1443 Query *query = mQueryNameSpace.find(handle);
1446 query = new Query(handle, type);
1448 mQueryNameSpace.insert(handle, query);
1455 VertexArray *Context::getVertexArray(GLuint array) const
1457 return mVertexArrayNameSpace.find(array);
1460 VertexArray *Context::getCurrentVertexArray() const
1462 return getVertexArray(mState.vertexArray);
1465 bool Context::isVertexArray(GLuint array) const
1467 return mVertexArrayNameSpace.isReserved(array);
1470 bool Context::hasZeroDivisor() const
1472 // Verify there is at least one active attribute with a divisor of zero
1473 es2::Program *programObject = getCurrentProgram();
1474 for(int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
1476 bool active = (programObject->getAttributeStream(attributeIndex) != -1);
1477 if(active && getCurrentVertexArray()->getVertexAttribute(attributeIndex).mDivisor == 0)
1486 TransformFeedback *Context::getTransformFeedback(GLuint transformFeedback) const
1488 return mTransformFeedbackNameSpace.find(transformFeedback);
1491 Sampler *Context::getSampler(GLuint sampler) const
1493 return mResourceManager->getSampler(sampler);
1496 bool Context::isSampler(GLuint sampler) const
1498 return mResourceManager->isSampler(sampler);
1501 Buffer *Context::getArrayBuffer() const
1503 return mState.arrayBuffer;
1506 Buffer *Context::getElementArrayBuffer() const
1508 return getCurrentVertexArray()->getElementArrayBuffer();
1511 Buffer *Context::getCopyReadBuffer() const
1513 return mState.copyReadBuffer;
1516 Buffer *Context::getCopyWriteBuffer() const
1518 return mState.copyWriteBuffer;
1521 Buffer *Context::getPixelPackBuffer() const
1523 return mState.pixelPackBuffer;
1526 Buffer *Context::getPixelUnpackBuffer() const
1528 return mState.pixelUnpackBuffer;
1531 Buffer *Context::getGenericUniformBuffer() const
1533 return mState.genericUniformBuffer;
1536 const GLvoid* Context::getPixels(const GLvoid* data) const
1538 es2::Buffer* unpackBuffer = getPixelUnpackBuffer();
1539 const unsigned char* unpackBufferData = unpackBuffer ? static_cast<const unsigned char*>(unpackBuffer->data()) : nullptr;
1540 return unpackBufferData ? unpackBufferData + (ptrdiff_t)(data) : data;
1543 bool Context::getBuffer(GLenum target, es2::Buffer **buffer) const
1547 case GL_ARRAY_BUFFER:
1548 *buffer = getArrayBuffer();
1550 case GL_ELEMENT_ARRAY_BUFFER:
1551 *buffer = getElementArrayBuffer();
1553 case GL_COPY_READ_BUFFER:
1554 if(clientVersion >= 3)
1556 *buffer = getCopyReadBuffer();
1560 case GL_COPY_WRITE_BUFFER:
1561 if(clientVersion >= 3)
1563 *buffer = getCopyWriteBuffer();
1567 case GL_PIXEL_PACK_BUFFER:
1568 if(clientVersion >= 3)
1570 *buffer = getPixelPackBuffer();
1574 case GL_PIXEL_UNPACK_BUFFER:
1575 if(clientVersion >= 3)
1577 *buffer = getPixelUnpackBuffer();
1581 case GL_TRANSFORM_FEEDBACK_BUFFER:
1582 if(clientVersion >= 3)
1584 TransformFeedback* transformFeedback = getTransformFeedback();
1585 *buffer = transformFeedback ? static_cast<es2::Buffer*>(transformFeedback->getGenericBuffer()) : nullptr;
1589 case GL_UNIFORM_BUFFER:
1590 if(clientVersion >= 3)
1592 *buffer = getGenericUniformBuffer();
1602 TransformFeedback *Context::getTransformFeedback() const
1604 return getTransformFeedback(mState.transformFeedback);
1607 Program *Context::getCurrentProgram() const
1609 return mResourceManager->getProgram(mState.currentProgram);
1612 Texture2D *Context::getTexture2D() const
1614 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
1617 Texture3D *Context::getTexture3D() const
1619 return static_cast<Texture3D*>(getSamplerTexture(mState.activeSampler, TEXTURE_3D));
1622 Texture2DArray *Context::getTexture2DArray() const
1624 return static_cast<Texture2DArray*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D_ARRAY));
1627 TextureCubeMap *Context::getTextureCubeMap() const
1629 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
1632 TextureExternal *Context::getTextureExternal() const
1634 return static_cast<TextureExternal*>(getSamplerTexture(mState.activeSampler, TEXTURE_EXTERNAL));
1637 Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type) const
1639 GLuint texid = mState.samplerTexture[type][sampler].name();
1641 if(texid == 0) // Special case: 0 refers to different initial textures based on the target
1645 case TEXTURE_2D: return mTexture2DZero;
1646 case TEXTURE_3D: return mTexture3DZero;
1647 case TEXTURE_2D_ARRAY: return mTexture2DArrayZero;
1648 case TEXTURE_CUBE: return mTextureCubeMapZero;
1649 case TEXTURE_EXTERNAL: return mTextureExternalZero;
1650 default: UNREACHABLE(type);
1654 return mState.samplerTexture[type][sampler];
1657 void Context::samplerParameteri(GLuint sampler, GLenum pname, GLint param)
1659 mResourceManager->checkSamplerAllocation(sampler);
1661 Sampler *samplerObject = getSampler(sampler);
1662 ASSERT(samplerObject);
1666 case GL_TEXTURE_MIN_FILTER: samplerObject->setMinFilter(static_cast<GLenum>(param)); break;
1667 case GL_TEXTURE_MAG_FILTER: samplerObject->setMagFilter(static_cast<GLenum>(param)); break;
1668 case GL_TEXTURE_WRAP_S: samplerObject->setWrapS(static_cast<GLenum>(param)); break;
1669 case GL_TEXTURE_WRAP_T: samplerObject->setWrapT(static_cast<GLenum>(param)); break;
1670 case GL_TEXTURE_WRAP_R: samplerObject->setWrapR(static_cast<GLenum>(param)); break;
1671 case GL_TEXTURE_MIN_LOD: samplerObject->setMinLod(static_cast<GLfloat>(param)); break;
1672 case GL_TEXTURE_MAX_LOD: samplerObject->setMaxLod(static_cast<GLfloat>(param)); break;
1673 case GL_TEXTURE_COMPARE_MODE: samplerObject->setComparisonMode(static_cast<GLenum>(param)); break;
1674 case GL_TEXTURE_COMPARE_FUNC: samplerObject->setComparisonFunc(static_cast<GLenum>(param)); break;
1675 default: UNREACHABLE(pname); break;
1679 void Context::samplerParameterf(GLuint sampler, GLenum pname, GLfloat param)
1681 mResourceManager->checkSamplerAllocation(sampler);
1683 Sampler *samplerObject = getSampler(sampler);
1684 ASSERT(samplerObject);
1688 case GL_TEXTURE_MIN_FILTER: samplerObject->setMinFilter(static_cast<GLenum>(roundf(param))); break;
1689 case GL_TEXTURE_MAG_FILTER: samplerObject->setMagFilter(static_cast<GLenum>(roundf(param))); break;
1690 case GL_TEXTURE_WRAP_S: samplerObject->setWrapS(static_cast<GLenum>(roundf(param))); break;
1691 case GL_TEXTURE_WRAP_T: samplerObject->setWrapT(static_cast<GLenum>(roundf(param))); break;
1692 case GL_TEXTURE_WRAP_R: samplerObject->setWrapR(static_cast<GLenum>(roundf(param))); break;
1693 case GL_TEXTURE_MIN_LOD: samplerObject->setMinLod(param); break;
1694 case GL_TEXTURE_MAX_LOD: samplerObject->setMaxLod(param); break;
1695 case GL_TEXTURE_COMPARE_MODE: samplerObject->setComparisonMode(static_cast<GLenum>(roundf(param))); break;
1696 case GL_TEXTURE_COMPARE_FUNC: samplerObject->setComparisonFunc(static_cast<GLenum>(roundf(param))); break;
1697 default: UNREACHABLE(pname); break;
1701 GLint Context::getSamplerParameteri(GLuint sampler, GLenum pname)
1703 mResourceManager->checkSamplerAllocation(sampler);
1705 Sampler *samplerObject = getSampler(sampler);
1706 ASSERT(samplerObject);
1710 case GL_TEXTURE_MIN_FILTER: return static_cast<GLint>(samplerObject->getMinFilter());
1711 case GL_TEXTURE_MAG_FILTER: return static_cast<GLint>(samplerObject->getMagFilter());
1712 case GL_TEXTURE_WRAP_S: return static_cast<GLint>(samplerObject->getWrapS());
1713 case GL_TEXTURE_WRAP_T: return static_cast<GLint>(samplerObject->getWrapT());
1714 case GL_TEXTURE_WRAP_R: return static_cast<GLint>(samplerObject->getWrapR());
1715 case GL_TEXTURE_MIN_LOD: return static_cast<GLint>(roundf(samplerObject->getMinLod()));
1716 case GL_TEXTURE_MAX_LOD: return static_cast<GLint>(roundf(samplerObject->getMaxLod()));
1717 case GL_TEXTURE_COMPARE_MODE: return static_cast<GLint>(samplerObject->getComparisonMode());
1718 case GL_TEXTURE_COMPARE_FUNC: return static_cast<GLint>(samplerObject->getComparisonFunc());
1719 default: UNREACHABLE(pname); return 0;
1723 GLfloat Context::getSamplerParameterf(GLuint sampler, GLenum pname)
1725 mResourceManager->checkSamplerAllocation(sampler);
1727 Sampler *samplerObject = getSampler(sampler);
1728 ASSERT(samplerObject);
1732 case GL_TEXTURE_MIN_FILTER: return static_cast<GLfloat>(samplerObject->getMinFilter());
1733 case GL_TEXTURE_MAG_FILTER: return static_cast<GLfloat>(samplerObject->getMagFilter());
1734 case GL_TEXTURE_WRAP_S: return static_cast<GLfloat>(samplerObject->getWrapS());
1735 case GL_TEXTURE_WRAP_T: return static_cast<GLfloat>(samplerObject->getWrapT());
1736 case GL_TEXTURE_WRAP_R: return static_cast<GLfloat>(samplerObject->getWrapR());
1737 case GL_TEXTURE_MIN_LOD: return samplerObject->getMinLod();
1738 case GL_TEXTURE_MAX_LOD: return samplerObject->getMaxLod();
1739 case GL_TEXTURE_COMPARE_MODE: return static_cast<GLfloat>(samplerObject->getComparisonMode());
1740 case GL_TEXTURE_COMPARE_FUNC: return static_cast<GLfloat>(samplerObject->getComparisonFunc());
1741 default: UNREACHABLE(pname); return 0;
1745 bool Context::getBooleanv(GLenum pname, GLboolean *params) const
1749 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1750 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1751 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
1752 case GL_COLOR_WRITEMASK:
1753 params[0] = mState.colorMaskRed;
1754 params[1] = mState.colorMaskGreen;
1755 params[2] = mState.colorMaskBlue;
1756 params[3] = mState.colorMaskAlpha;
1758 case GL_CULL_FACE: *params = mState.cullFaceEnabled; break;
1759 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFillEnabled; break;
1760 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverageEnabled; break;
1761 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverageEnabled; break;
1762 case GL_SCISSOR_TEST: *params = mState.scissorTestEnabled; break;
1763 case GL_STENCIL_TEST: *params = mState.stencilTestEnabled; break;
1764 case GL_DEPTH_TEST: *params = mState.depthTestEnabled; break;
1765 case GL_BLEND: *params = mState.blendEnabled; break;
1766 case GL_DITHER: *params = mState.ditherEnabled; break;
1767 case GL_PRIMITIVE_RESTART_FIXED_INDEX: *params = mState.primitiveRestartFixedIndexEnabled; break;
1768 case GL_RASTERIZER_DISCARD: *params = mState.rasterizerDiscardEnabled; break;
1769 case GL_TRANSFORM_FEEDBACK_ACTIVE:
1771 TransformFeedback* transformFeedback = getTransformFeedback(mState.transformFeedback);
1772 if(transformFeedback)
1774 *params = transformFeedback->isActive();
1779 case GL_TRANSFORM_FEEDBACK_PAUSED:
1781 TransformFeedback* transformFeedback = getTransformFeedback(mState.transformFeedback);
1782 if(transformFeedback)
1784 *params = transformFeedback->isPaused();
1796 bool Context::getFloatv(GLenum pname, GLfloat *params) const
1798 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1799 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1800 // GetIntegerv as its native query function. As it would require conversion in any
1801 // case, this should make no difference to the calling application.
1804 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1805 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1806 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1807 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1808 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
1809 case GL_ALIASED_LINE_WIDTH_RANGE:
1810 params[0] = ALIASED_LINE_WIDTH_RANGE_MIN;
1811 params[1] = ALIASED_LINE_WIDTH_RANGE_MAX;
1813 case GL_ALIASED_POINT_SIZE_RANGE:
1814 params[0] = ALIASED_POINT_SIZE_RANGE_MIN;
1815 params[1] = ALIASED_POINT_SIZE_RANGE_MAX;
1817 case GL_DEPTH_RANGE:
1818 params[0] = mState.zNear;
1819 params[1] = mState.zFar;
1821 case GL_COLOR_CLEAR_VALUE:
1822 params[0] = mState.colorClearValue.red;
1823 params[1] = mState.colorClearValue.green;
1824 params[2] = mState.colorClearValue.blue;
1825 params[3] = mState.colorClearValue.alpha;
1827 case GL_BLEND_COLOR:
1828 params[0] = mState.blendColor.red;
1829 params[1] = mState.blendColor.green;
1830 params[2] = mState.blendColor.blue;
1831 params[3] = mState.blendColor.alpha;
1833 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1834 *params = MAX_TEXTURE_MAX_ANISOTROPY;
1843 template bool Context::getIntegerv<GLint>(GLenum pname, GLint *params) const;
1844 template bool Context::getIntegerv<GLint64>(GLenum pname, GLint64 *params) const;
1846 template<typename T> bool Context::getIntegerv(GLenum pname, T *params) const
1848 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1849 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1850 // GetIntegerv as its native query function. As it would require conversion in any
1851 // case, this should make no difference to the calling application. You may find it in
1852 // Context::getFloatv.
1855 case GL_MAX_VERTEX_ATTRIBS: *params = MAX_VERTEX_ATTRIBS; break;
1856 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = MAX_VERTEX_UNIFORM_VECTORS; break;
1857 case GL_MAX_VARYING_VECTORS: *params = MAX_VARYING_VECTORS; break;
1858 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1859 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1860 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = MAX_TEXTURE_IMAGE_UNITS; break;
1861 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = MAX_FRAGMENT_UNIFORM_VECTORS; break;
1862 case GL_MAX_RENDERBUFFER_SIZE: *params = IMPLEMENTATION_MAX_RENDERBUFFER_SIZE; break;
1863 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1864 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
1865 case GL_ARRAY_BUFFER_BINDING: *params = getArrayBufferName(); break;
1866 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = getElementArrayBufferName(); break;
1867 // case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1868 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1869 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
1870 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.name(); break;
1871 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1872 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1873 case GL_UNPACK_ALIGNMENT: *params = mState.unpackInfo.alignment; break;
1874 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1875 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
1876 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1877 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1878 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1879 case GL_STENCIL_VALUE_MASK: *params = sw::clampToSignedInt(mState.stencilMask); break;
1880 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1881 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1882 case GL_STENCIL_BACK_VALUE_MASK: *params = sw::clampToSignedInt(mState.stencilBackMask); break;
1883 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1884 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1885 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1886 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1887 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1888 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1889 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1890 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1891 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1892 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1893 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1894 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1895 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1896 case GL_STENCIL_WRITEMASK: *params = sw::clampToSignedInt(mState.stencilWritemask); break;
1897 case GL_STENCIL_BACK_WRITEMASK: *params = sw::clampToSignedInt(mState.stencilBackWritemask); break;
1898 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1899 case GL_SUBPIXEL_BITS: *params = 4; break;
1900 case GL_MAX_TEXTURE_SIZE: *params = IMPLEMENTATION_MAX_TEXTURE_SIZE; break;
1901 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE; break;
1902 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = NUM_COMPRESSED_TEXTURE_FORMATS; break;
1903 case GL_MAX_SAMPLES_ANGLE: *params = IMPLEMENTATION_MAX_SAMPLES; break;
1904 case GL_SAMPLE_BUFFERS:
1907 Framebuffer *framebuffer = getDrawFramebuffer();
1908 int width, height, samples;
1910 if(framebuffer->completeness(width, height, samples) == GL_FRAMEBUFFER_COMPLETE)
1914 case GL_SAMPLE_BUFFERS:
1935 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1937 Framebuffer *framebuffer = getReadFramebuffer();
1938 *params = framebuffer->getImplementationColorReadType();
1941 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1943 Framebuffer *framebuffer = getReadFramebuffer();
1944 *params = framebuffer->getImplementationColorReadFormat();
1947 case GL_MAX_VIEWPORT_DIMS:
1949 int maxDimension = IMPLEMENTATION_MAX_RENDERBUFFER_SIZE;
1950 params[0] = maxDimension;
1951 params[1] = maxDimension;
1954 case GL_COMPRESSED_TEXTURE_FORMATS:
1956 for(int i = 0; i < NUM_COMPRESSED_TEXTURE_FORMATS; i++)
1958 params[i] = compressedTextureFormats[i];
1963 params[0] = mState.viewportX;
1964 params[1] = mState.viewportY;
1965 params[2] = mState.viewportWidth;
1966 params[3] = mState.viewportHeight;
1968 case GL_SCISSOR_BOX:
1969 params[0] = mState.scissorX;
1970 params[1] = mState.scissorY;
1971 params[2] = mState.scissorWidth;
1972 params[3] = mState.scissorHeight;
1974 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1975 case GL_FRONT_FACE: *params = mState.frontFace; break;
1981 Framebuffer *framebuffer = getDrawFramebuffer();
1982 Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0);
1988 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1989 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1990 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1991 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
2002 Framebuffer *framebuffer = getDrawFramebuffer();
2003 Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
2007 *params = depthbuffer->getDepthSize();
2015 case GL_STENCIL_BITS:
2017 Framebuffer *framebuffer = getDrawFramebuffer();
2018 Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
2022 *params = stencilbuffer->getStencilSize();
2030 case GL_TEXTURE_BINDING_2D:
2031 if(mState.activeSampler > MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1)
2033 error(GL_INVALID_OPERATION);
2037 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].name();
2039 case GL_TEXTURE_BINDING_CUBE_MAP:
2040 if(mState.activeSampler > MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1)
2042 error(GL_INVALID_OPERATION);
2046 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].name();
2048 case GL_TEXTURE_BINDING_EXTERNAL_OES:
2049 if(mState.activeSampler > MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1)
2051 error(GL_INVALID_OPERATION);
2055 *params = mState.samplerTexture[TEXTURE_EXTERNAL][mState.activeSampler].name();
2057 case GL_TEXTURE_BINDING_3D_OES:
2058 if(mState.activeSampler > MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1)
2060 error(GL_INVALID_OPERATION);
2064 *params = mState.samplerTexture[TEXTURE_3D][mState.activeSampler].name();
2066 case GL_TEXTURE_BINDING_2D_ARRAY: // GLES 3.0
2067 if(clientVersion < 3)
2071 else if(mState.activeSampler > MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1)
2073 error(GL_INVALID_OPERATION);
2077 *params = mState.samplerTexture[TEXTURE_2D_ARRAY][mState.activeSampler].name();
2079 case GL_COPY_READ_BUFFER_BINDING: // name, initially 0
2080 if(clientVersion >= 3)
2082 *params = mState.copyReadBuffer.name();
2089 case GL_COPY_WRITE_BUFFER_BINDING: // name, initially 0
2090 if(clientVersion >= 3)
2092 *params = mState.copyWriteBuffer.name();
2099 case GL_DRAW_BUFFER0:
2100 case GL_DRAW_BUFFER1:
2101 case GL_DRAW_BUFFER2:
2102 case GL_DRAW_BUFFER3:
2103 case GL_DRAW_BUFFER4:
2104 case GL_DRAW_BUFFER5:
2105 case GL_DRAW_BUFFER6:
2106 case GL_DRAW_BUFFER7:
2107 case GL_DRAW_BUFFER8:
2108 case GL_DRAW_BUFFER9:
2109 case GL_DRAW_BUFFER10:
2110 case GL_DRAW_BUFFER11:
2111 case GL_DRAW_BUFFER12:
2112 case GL_DRAW_BUFFER13:
2113 case GL_DRAW_BUFFER14:
2114 case GL_DRAW_BUFFER15:
2115 *params = getDrawFramebuffer()->getDrawBuffer(pname - GL_DRAW_BUFFER0);
2117 case GL_MAJOR_VERSION:
2118 if(clientVersion >= 3)
2120 *params = clientVersion;
2127 case GL_MAX_3D_TEXTURE_SIZE: // GLint, at least 2048
2128 *params = IMPLEMENTATION_MAX_TEXTURE_SIZE;
2130 case GL_MAX_ARRAY_TEXTURE_LAYERS: // GLint, at least 2048
2131 *params = IMPLEMENTATION_MAX_TEXTURE_SIZE;
2133 case GL_MAX_COLOR_ATTACHMENTS:
2134 *params = MAX_COLOR_ATTACHMENTS;
2136 case GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS: // integer, at least 50048
2137 *params = MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS;
2139 case GL_MAX_COMBINED_UNIFORM_BLOCKS: // integer, at least 70
2143 case GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS: // integer, at least 50176
2144 *params = MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS;
2146 case GL_MAX_DRAW_BUFFERS:
2147 *params = MAX_DRAW_BUFFERS;
2149 case GL_MAX_ELEMENT_INDEX:
2150 *params = MAX_ELEMENT_INDEX;
2152 case GL_MAX_ELEMENTS_INDICES:
2153 *params = MAX_ELEMENTS_INDICES;
2155 case GL_MAX_ELEMENTS_VERTICES:
2156 *params = MAX_ELEMENTS_VERTICES;
2158 case GL_MAX_FRAGMENT_INPUT_COMPONENTS: // integer, at least 128
2162 case GL_MAX_FRAGMENT_UNIFORM_BLOCKS: // integer, at least 12
2163 *params = MAX_FRAGMENT_UNIFORM_BLOCKS;
2165 case GL_MAX_FRAGMENT_UNIFORM_COMPONENTS: // integer, at least 896
2166 *params = MAX_FRAGMENT_UNIFORM_COMPONENTS;
2168 case GL_MAX_PROGRAM_TEXEL_OFFSET: // integer, minimum is 7
2172 case GL_MAX_SERVER_WAIT_TIMEOUT: // integer
2176 case GL_MAX_TEXTURE_LOD_BIAS: // integer, at least 2.0
2180 case GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS: // integer, at least 64
2181 *params = sw::MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS;
2183 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS: // integer, at least 4
2184 *params = MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS;
2186 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS: // integer, at least 4
2187 *params = sw::MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS;
2189 case GL_MAX_UNIFORM_BLOCK_SIZE: // integer, at least 16384
2190 *params = MAX_UNIFORM_BLOCK_SIZE;
2192 case GL_MAX_UNIFORM_BUFFER_BINDINGS: // integer, at least 24
2193 *params = MAX_UNIFORM_BUFFER_BINDINGS;
2195 case GL_MAX_VARYING_COMPONENTS: // integer, at least 60
2199 case GL_MAX_VERTEX_OUTPUT_COMPONENTS: // integer, at least 64
2203 case GL_MAX_VERTEX_UNIFORM_BLOCKS: // integer, at least 12
2204 *params = MAX_VERTEX_UNIFORM_BLOCKS;
2206 case GL_MAX_VERTEX_UNIFORM_COMPONENTS: // integer, at least 1024
2207 *params = MAX_VERTEX_UNIFORM_COMPONENTS;
2209 case GL_MIN_PROGRAM_TEXEL_OFFSET: // integer, maximum is -8
2213 case GL_MINOR_VERSION: // integer
2217 case GL_NUM_EXTENSIONS: // integer
2218 GLuint numExtensions;
2219 getExtensions(0, &numExtensions);
2220 *params = numExtensions;
2222 case GL_NUM_PROGRAM_BINARY_FORMATS: // integer, at least 0
2226 case GL_PACK_ROW_LENGTH: // integer, initially 0
2227 *params = mState.packRowLength;
2229 case GL_PACK_SKIP_PIXELS: // integer, initially 0
2230 *params = mState.packSkipPixels;
2232 case GL_PACK_SKIP_ROWS: // integer, initially 0
2233 *params = mState.packSkipRows;
2235 case GL_PIXEL_PACK_BUFFER_BINDING: // integer, initially 0
2236 if(clientVersion >= 3)
2238 *params = mState.pixelPackBuffer.name();
2245 case GL_PIXEL_UNPACK_BUFFER_BINDING: // integer, initially 0
2246 if(clientVersion >= 3)
2248 *params = mState.pixelUnpackBuffer.name();
2255 case GL_PROGRAM_BINARY_FORMATS: // integer[GL_NUM_PROGRAM_BINARY_FORMATS​]
2259 case GL_READ_BUFFER: // symbolic constant, initial value is GL_BACK​
2260 *params = getReadFramebuffer()->getReadBuffer();
2262 case GL_SAMPLER_BINDING: // GLint, default 0
2263 *params = mState.sampler[mState.activeSampler].name();
2265 case GL_UNIFORM_BUFFER_BINDING: // name, initially 0
2266 if(clientVersion >= 3)
2268 *params = mState.genericUniformBuffer.name();
2275 case GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT: // integer, defaults to 1
2276 *params = UNIFORM_BUFFER_OFFSET_ALIGNMENT;
2278 case GL_UNIFORM_BUFFER_SIZE: // indexed[n] 64-bit integer, initially 0
2279 if(clientVersion >= 3)
2281 *params = mState.genericUniformBuffer->size();
2288 case GL_UNIFORM_BUFFER_START: // indexed[n] 64-bit integer, initially 0
2289 if(clientVersion >= 3)
2291 *params = mState.genericUniformBuffer->offset();
2299 case GL_UNPACK_IMAGE_HEIGHT: // integer, initially 0
2300 *params = mState.unpackInfo.imageHeight;
2302 case GL_UNPACK_ROW_LENGTH: // integer, initially 0
2303 *params = mState.unpackInfo.rowLength;
2305 case GL_UNPACK_SKIP_IMAGES: // integer, initially 0
2306 *params = mState.unpackInfo.skipImages;
2308 case GL_UNPACK_SKIP_PIXELS: // integer, initially 0
2309 *params = mState.unpackInfo.skipPixels;
2311 case GL_UNPACK_SKIP_ROWS: // integer, initially 0
2312 *params = mState.unpackInfo.skipRows;
2314 case GL_VERTEX_ARRAY_BINDING: // GLint, initially 0
2315 *params = getCurrentVertexArray()->name;
2317 case GL_TRANSFORM_FEEDBACK_BINDING:
2319 TransformFeedback* transformFeedback = getTransformFeedback(mState.transformFeedback);
2320 if(transformFeedback)
2322 *params = transformFeedback->name;
2330 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
2332 TransformFeedback* transformFeedback = getTransformFeedback(mState.transformFeedback);
2333 if(transformFeedback)
2335 *params = transformFeedback->getGenericBufferName();
2350 template bool Context::getTransformFeedbackiv<GLint>(GLuint index, GLenum pname, GLint *param) const;
2351 template bool Context::getTransformFeedbackiv<GLint64>(GLuint index, GLenum pname, GLint64 *param) const;
2353 template<typename T> bool Context::getTransformFeedbackiv(GLuint index, GLenum pname, T *param) const
2355 TransformFeedback* transformFeedback = getTransformFeedback(mState.transformFeedback);
2356 if(!transformFeedback)
2363 case GL_TRANSFORM_FEEDBACK_BINDING: // GLint, initially 0
2364 *param = transformFeedback->name;
2366 case GL_TRANSFORM_FEEDBACK_ACTIVE: // boolean, initially GL_FALSE
2367 *param = transformFeedback->isActive();
2369 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING: // name, initially 0
2370 *param = transformFeedback->getBufferName(index);
2372 case GL_TRANSFORM_FEEDBACK_PAUSED: // boolean, initially GL_FALSE
2373 *param = transformFeedback->isPaused();
2375 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE: // indexed[n] 64-bit integer, initially 0
2376 if(transformFeedback->getBuffer(index))
2378 *param = transformFeedback->getSize(index);
2382 case GL_TRANSFORM_FEEDBACK_BUFFER_START: // indexed[n] 64-bit integer, initially 0
2383 if(transformFeedback->getBuffer(index))
2385 *param = transformFeedback->getOffset(index);
2396 template bool Context::getUniformBufferiv<GLint>(GLuint index, GLenum pname, GLint *param) const;
2397 template bool Context::getUniformBufferiv<GLint64>(GLuint index, GLenum pname, GLint64 *param) const;
2399 template<typename T> bool Context::getUniformBufferiv(GLuint index, GLenum pname, T *param) const
2401 const BufferBinding& uniformBuffer = mState.uniformBuffers[index];
2405 case GL_UNIFORM_BUFFER_BINDING: // name, initially 0
2406 *param = uniformBuffer.get().name();
2408 case GL_UNIFORM_BUFFER_SIZE: // indexed[n] 64-bit integer, initially 0
2409 *param = uniformBuffer.getSize();
2411 case GL_UNIFORM_BUFFER_START: // indexed[n] 64-bit integer, initially 0
2412 *param = uniformBuffer.getOffset();
2421 bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams) const
2423 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
2424 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
2425 // to the fact that it is stored internally as a float, and so would require conversion
2426 // if returned from Context::getIntegerv. Since this conversion is already implemented
2427 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
2428 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
2432 case GL_COMPRESSED_TEXTURE_FORMATS:
2435 *numParams = NUM_COMPRESSED_TEXTURE_FORMATS;
2438 case GL_SHADER_BINARY_FORMATS:
2444 case GL_MAX_VERTEX_ATTRIBS:
2445 case GL_MAX_VERTEX_UNIFORM_VECTORS:
2446 case GL_MAX_VARYING_VECTORS:
2447 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
2448 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
2449 case GL_MAX_TEXTURE_IMAGE_UNITS:
2450 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
2451 case GL_MAX_RENDERBUFFER_SIZE:
2452 case GL_NUM_SHADER_BINARY_FORMATS:
2453 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
2454 case GL_ARRAY_BUFFER_BINDING:
2455 case GL_FRAMEBUFFER_BINDING: // Same as GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
2456 case GL_READ_FRAMEBUFFER_BINDING_ANGLE:
2457 case GL_RENDERBUFFER_BINDING:
2458 case GL_CURRENT_PROGRAM:
2459 case GL_PACK_ALIGNMENT:
2460 case GL_UNPACK_ALIGNMENT:
2461 case GL_GENERATE_MIPMAP_HINT:
2462 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
2468 case GL_STENCIL_BITS:
2469 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
2470 case GL_CULL_FACE_MODE:
2472 case GL_ACTIVE_TEXTURE:
2473 case GL_STENCIL_FUNC:
2474 case GL_STENCIL_VALUE_MASK:
2475 case GL_STENCIL_REF:
2476 case GL_STENCIL_FAIL:
2477 case GL_STENCIL_PASS_DEPTH_FAIL:
2478 case GL_STENCIL_PASS_DEPTH_PASS:
2479 case GL_STENCIL_BACK_FUNC:
2480 case GL_STENCIL_BACK_VALUE_MASK:
2481 case GL_STENCIL_BACK_REF:
2482 case GL_STENCIL_BACK_FAIL:
2483 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
2484 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
2486 case GL_BLEND_SRC_RGB:
2487 case GL_BLEND_SRC_ALPHA:
2488 case GL_BLEND_DST_RGB:
2489 case GL_BLEND_DST_ALPHA:
2490 case GL_BLEND_EQUATION_RGB:
2491 case GL_BLEND_EQUATION_ALPHA:
2492 case GL_STENCIL_WRITEMASK:
2493 case GL_STENCIL_BACK_WRITEMASK:
2494 case GL_STENCIL_CLEAR_VALUE:
2495 case GL_SUBPIXEL_BITS:
2496 case GL_MAX_TEXTURE_SIZE:
2497 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
2498 case GL_SAMPLE_BUFFERS:
2500 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
2501 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
2502 case GL_TEXTURE_BINDING_2D:
2503 case GL_TEXTURE_BINDING_CUBE_MAP:
2504 case GL_TEXTURE_BINDING_EXTERNAL_OES:
2505 case GL_TEXTURE_BINDING_3D_OES:
2506 case GL_COPY_READ_BUFFER_BINDING:
2507 case GL_COPY_WRITE_BUFFER_BINDING:
2508 case GL_DRAW_BUFFER0:
2509 case GL_DRAW_BUFFER1:
2510 case GL_DRAW_BUFFER2:
2511 case GL_DRAW_BUFFER3:
2512 case GL_DRAW_BUFFER4:
2513 case GL_DRAW_BUFFER5:
2514 case GL_DRAW_BUFFER6:
2515 case GL_DRAW_BUFFER7:
2516 case GL_DRAW_BUFFER8:
2517 case GL_DRAW_BUFFER9:
2518 case GL_DRAW_BUFFER10:
2519 case GL_DRAW_BUFFER11:
2520 case GL_DRAW_BUFFER12:
2521 case GL_DRAW_BUFFER13:
2522 case GL_DRAW_BUFFER14:
2523 case GL_DRAW_BUFFER15:
2524 case GL_MAJOR_VERSION:
2525 case GL_MAX_3D_TEXTURE_SIZE:
2526 case GL_MAX_ARRAY_TEXTURE_LAYERS:
2527 case GL_MAX_COLOR_ATTACHMENTS:
2528 case GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS:
2529 case GL_MAX_COMBINED_UNIFORM_BLOCKS:
2530 case GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS:
2531 case GL_MAX_DRAW_BUFFERS:
2532 case GL_MAX_ELEMENT_INDEX:
2533 case GL_MAX_ELEMENTS_INDICES:
2534 case GL_MAX_ELEMENTS_VERTICES:
2535 case GL_MAX_FRAGMENT_INPUT_COMPONENTS:
2536 case GL_MAX_FRAGMENT_UNIFORM_BLOCKS:
2537 case GL_MAX_FRAGMENT_UNIFORM_COMPONENTS:
2538 case GL_MAX_PROGRAM_TEXEL_OFFSET:
2539 case GL_MAX_SERVER_WAIT_TIMEOUT:
2540 case GL_MAX_TEXTURE_LOD_BIAS:
2541 case GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS:
2542 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS:
2543 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS:
2544 case GL_MAX_UNIFORM_BLOCK_SIZE:
2545 case GL_MAX_UNIFORM_BUFFER_BINDINGS:
2546 case GL_MAX_VARYING_COMPONENTS:
2547 case GL_MAX_VERTEX_OUTPUT_COMPONENTS:
2548 case GL_MAX_VERTEX_UNIFORM_BLOCKS:
2549 case GL_MAX_VERTEX_UNIFORM_COMPONENTS:
2550 case GL_MIN_PROGRAM_TEXEL_OFFSET:
2551 case GL_MINOR_VERSION:
2552 case GL_NUM_EXTENSIONS:
2553 case GL_NUM_PROGRAM_BINARY_FORMATS:
2554 case GL_PACK_ROW_LENGTH:
2555 case GL_PACK_SKIP_PIXELS:
2556 case GL_PACK_SKIP_ROWS:
2557 case GL_PIXEL_PACK_BUFFER_BINDING:
2558 case GL_PIXEL_UNPACK_BUFFER_BINDING:
2559 case GL_PROGRAM_BINARY_FORMATS:
2560 case GL_READ_BUFFER:
2561 case GL_SAMPLER_BINDING:
2562 case GL_TEXTURE_BINDING_2D_ARRAY:
2563 case GL_UNIFORM_BUFFER_BINDING:
2564 case GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT:
2565 case GL_UNIFORM_BUFFER_SIZE:
2566 case GL_UNIFORM_BUFFER_START:
2567 case GL_UNPACK_IMAGE_HEIGHT:
2568 case GL_UNPACK_ROW_LENGTH:
2569 case GL_UNPACK_SKIP_IMAGES:
2570 case GL_UNPACK_SKIP_PIXELS:
2571 case GL_UNPACK_SKIP_ROWS:
2572 case GL_VERTEX_ARRAY_BINDING:
2573 case GL_TRANSFORM_FEEDBACK_BINDING:
2574 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
2580 case GL_MAX_SAMPLES_ANGLE:
2586 case GL_MAX_VIEWPORT_DIMS:
2593 case GL_SCISSOR_BOX:
2599 case GL_SHADER_COMPILER:
2600 case GL_SAMPLE_COVERAGE_INVERT:
2601 case GL_DEPTH_WRITEMASK:
2602 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
2603 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
2604 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
2605 case GL_SAMPLE_COVERAGE:
2606 case GL_SCISSOR_TEST:
2607 case GL_STENCIL_TEST:
2611 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
2612 case GL_RASTERIZER_DISCARD:
2613 case GL_TRANSFORM_FEEDBACK_ACTIVE:
2614 case GL_TRANSFORM_FEEDBACK_PAUSED:
2620 case GL_COLOR_WRITEMASK:
2626 case GL_POLYGON_OFFSET_FACTOR:
2627 case GL_POLYGON_OFFSET_UNITS:
2628 case GL_SAMPLE_COVERAGE_VALUE:
2629 case GL_DEPTH_CLEAR_VALUE:
2636 case GL_ALIASED_LINE_WIDTH_RANGE:
2637 case GL_ALIASED_POINT_SIZE_RANGE:
2638 case GL_DEPTH_RANGE:
2644 case GL_COLOR_CLEAR_VALUE:
2645 case GL_BLEND_COLOR:
2651 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
2662 void Context::applyScissor(int width, int height)
2664 if(mState.scissorTestEnabled)
2666 sw::Rect scissor = { mState.scissorX, mState.scissorY, mState.scissorX + mState.scissorWidth, mState.scissorY + mState.scissorHeight };
2667 scissor.clip(0, 0, width, height);
2669 device->setScissorRect(scissor);
2670 device->setScissorEnable(true);
2674 device->setScissorEnable(false);
2678 // Applies the render target surface, depth stencil surface, viewport rectangle and scissor rectangle
2679 bool Context::applyRenderTarget()
2681 Framebuffer *framebuffer = getDrawFramebuffer();
2682 int width, height, samples;
2684 if(!framebuffer || framebuffer->completeness(width, height, samples) != GL_FRAMEBUFFER_COMPLETE)
2686 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
2689 for(int i = 0; i < MAX_DRAW_BUFFERS; i++)
2691 if(framebuffer->getDrawBuffer(i) != GL_NONE)
2693 egl::Image *renderTarget = framebuffer->getRenderTarget(i);
2694 device->setRenderTarget(i, renderTarget);
2695 if(renderTarget) renderTarget->release();
2699 device->setRenderTarget(i, nullptr);
2703 egl::Image *depthBuffer = framebuffer->getDepthBuffer();
2704 device->setDepthBuffer(depthBuffer);
2705 if(depthBuffer) depthBuffer->release();
2707 egl::Image *stencilBuffer = framebuffer->getStencilBuffer();
2708 device->setStencilBuffer(stencilBuffer);
2709 if(stencilBuffer) stencilBuffer->release();
2712 float zNear = clamp01(mState.zNear);
2713 float zFar = clamp01(mState.zFar);
2715 viewport.x0 = mState.viewportX;
2716 viewport.y0 = mState.viewportY;
2717 viewport.width = mState.viewportWidth;
2718 viewport.height = mState.viewportHeight;
2719 viewport.minZ = zNear;
2720 viewport.maxZ = zFar;
2722 device->setViewport(viewport);
2724 applyScissor(width, height);
2726 Program *program = getCurrentProgram();
2730 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
2731 program->setUniform1fv(program->getUniformLocation("gl_DepthRange.near"), 1, &nearFarDiff[0]);
2732 program->setUniform1fv(program->getUniformLocation("gl_DepthRange.far"), 1, &nearFarDiff[1]);
2733 program->setUniform1fv(program->getUniformLocation("gl_DepthRange.diff"), 1, &nearFarDiff[2]);
2739 // Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc)
2740 void Context::applyState(GLenum drawMode)
2742 Framebuffer *framebuffer = getDrawFramebuffer();
2744 if(mState.cullFaceEnabled)
2746 device->setCullMode(es2sw::ConvertCullMode(mState.cullMode, mState.frontFace));
2750 device->setCullMode(sw::CULL_NONE);
2753 if(mDepthStateDirty)
2755 if(mState.depthTestEnabled)
2757 device->setDepthBufferEnable(true);
2758 device->setDepthCompare(es2sw::ConvertDepthComparison(mState.depthFunc));
2762 device->setDepthBufferEnable(false);
2765 mDepthStateDirty = false;
2768 if(mBlendStateDirty)
2770 if(mState.blendEnabled)
2772 device->setAlphaBlendEnable(true);
2773 device->setSeparateAlphaBlendEnable(true);
2775 device->setBlendConstant(es2sw::ConvertColor(mState.blendColor));
2777 device->setSourceBlendFactor(es2sw::ConvertBlendFunc(mState.sourceBlendRGB));
2778 device->setDestBlendFactor(es2sw::ConvertBlendFunc(mState.destBlendRGB));
2779 device->setBlendOperation(es2sw::ConvertBlendOp(mState.blendEquationRGB));
2781 device->setSourceBlendFactorAlpha(es2sw::ConvertBlendFunc(mState.sourceBlendAlpha));
2782 device->setDestBlendFactorAlpha(es2sw::ConvertBlendFunc(mState.destBlendAlpha));
2783 device->setBlendOperationAlpha(es2sw::ConvertBlendOp(mState.blendEquationAlpha));
2787 device->setAlphaBlendEnable(false);
2790 mBlendStateDirty = false;
2793 if(mStencilStateDirty || mFrontFaceDirty)
2795 if(mState.stencilTestEnabled && framebuffer->hasStencil())
2797 device->setStencilEnable(true);
2798 device->setTwoSidedStencil(true);
2800 // get the maximum size of the stencil ref
2801 Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
2802 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2804 if(mState.frontFace == GL_CCW)
2806 device->setStencilWriteMask(mState.stencilWritemask);
2807 device->setStencilCompare(es2sw::ConvertStencilComparison(mState.stencilFunc));
2809 device->setStencilReference((mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2810 device->setStencilMask(mState.stencilMask);
2812 device->setStencilFailOperation(es2sw::ConvertStencilOp(mState.stencilFail));
2813 device->setStencilZFailOperation(es2sw::ConvertStencilOp(mState.stencilPassDepthFail));
2814 device->setStencilPassOperation(es2sw::ConvertStencilOp(mState.stencilPassDepthPass));
2816 device->setStencilWriteMaskCCW(mState.stencilBackWritemask);
2817 device->setStencilCompareCCW(es2sw::ConvertStencilComparison(mState.stencilBackFunc));
2819 device->setStencilReferenceCCW((mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2820 device->setStencilMaskCCW(mState.stencilBackMask);
2822 device->setStencilFailOperationCCW(es2sw::ConvertStencilOp(mState.stencilBackFail));
2823 device->setStencilZFailOperationCCW(es2sw::ConvertStencilOp(mState.stencilBackPassDepthFail));
2824 device->setStencilPassOperationCCW(es2sw::ConvertStencilOp(mState.stencilBackPassDepthPass));
2828 device->setStencilWriteMaskCCW(mState.stencilWritemask);
2829 device->setStencilCompareCCW(es2sw::ConvertStencilComparison(mState.stencilFunc));
2831 device->setStencilReferenceCCW((mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2832 device->setStencilMaskCCW(mState.stencilMask);
2834 device->setStencilFailOperationCCW(es2sw::ConvertStencilOp(mState.stencilFail));
2835 device->setStencilZFailOperationCCW(es2sw::ConvertStencilOp(mState.stencilPassDepthFail));
2836 device->setStencilPassOperationCCW(es2sw::ConvertStencilOp(mState.stencilPassDepthPass));
2838 device->setStencilWriteMask(mState.stencilBackWritemask);
2839 device->setStencilCompare(es2sw::ConvertStencilComparison(mState.stencilBackFunc));
2841 device->setStencilReference((mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2842 device->setStencilMask(mState.stencilBackMask);
2844 device->setStencilFailOperation(es2sw::ConvertStencilOp(mState.stencilBackFail));
2845 device->setStencilZFailOperation(es2sw::ConvertStencilOp(mState.stencilBackPassDepthFail));
2846 device->setStencilPassOperation(es2sw::ConvertStencilOp(mState.stencilBackPassDepthPass));
2851 device->setStencilEnable(false);
2854 mStencilStateDirty = false;
2855 mFrontFaceDirty = false;
2860 for(int i = 0; i < MAX_DRAW_BUFFERS; i++)
2862 device->setColorWriteMask(i, es2sw::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
2865 device->setDepthWriteEnable(mState.depthMask);
2867 mMaskStateDirty = false;
2870 if(mPolygonOffsetStateDirty)
2872 if(mState.polygonOffsetFillEnabled)
2874 Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
2877 device->setSlopeDepthBias(mState.polygonOffsetFactor);
2878 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
2879 device->setDepthBias(depthBias);
2884 device->setSlopeDepthBias(0);
2885 device->setDepthBias(0);
2888 mPolygonOffsetStateDirty = false;
2891 if(mSampleStateDirty)
2893 if(mState.sampleAlphaToCoverageEnabled)
2895 device->setTransparencyAntialiasing(sw::TRANSPARENCY_ALPHA_TO_COVERAGE);
2899 device->setTransparencyAntialiasing(sw::TRANSPARENCY_NONE);
2902 if(mState.sampleCoverageEnabled)
2904 unsigned int mask = 0;
2905 if(mState.sampleCoverageValue != 0)
2907 int width, height, samples;
2908 framebuffer->completeness(width, height, samples);
2910 float threshold = 0.5f;
2912 for(int i = 0; i < samples; i++)
2916 if((i + 1) * mState.sampleCoverageValue >= threshold)
2924 if(mState.sampleCoverageInvert)
2929 device->setMultiSampleMask(mask);
2933 device->setMultiSampleMask(0xFFFFFFFF);
2936 mSampleStateDirty = false;
2939 if(mDitherStateDirty)
2941 // UNIMPLEMENTED(); // FIXME
2943 mDitherStateDirty = false;
2946 device->setRasterizerDiscard(mState.rasterizerDiscardEnabled);
2949 GLenum Context::applyVertexBuffer(GLint base, GLint first, GLsizei count, GLsizei instanceId)
2951 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
2953 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instanceId);
2954 if(err != GL_NO_ERROR)
2959 Program *program = getCurrentProgram();
2961 device->resetInputStreams(false);
2963 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2965 if(program->getAttributeStream(i) == -1)
2970 sw::Resource *resource = attributes[i].vertexBuffer;
2971 const void *buffer = (char*)resource->data() + attributes[i].offset;
2973 int stride = attributes[i].stride;
2975 buffer = (char*)buffer + stride * base;
2977 sw::Stream attribute(resource, buffer, stride);
2979 attribute.type = attributes[i].type;
2980 attribute.count = attributes[i].count;
2981 attribute.normalized = attributes[i].normalized;
2983 int stream = program->getAttributeStream(i);
2984 device->setInputStream(stream, attribute);
2990 // Applies the indices and element array bindings
2991 GLenum Context::applyIndexBuffer(const void *indices, GLuint start, GLuint end, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
2993 GLenum err = mIndexDataManager->prepareIndexData(type, start, end, count, getCurrentVertexArray()->getElementArrayBuffer(), indices, indexInfo);
2995 if(err == GL_NO_ERROR)
2997 device->setIndexBuffer(indexInfo->indexBuffer);
3003 // Applies the shaders and shader constants
3004 void Context::applyShaders()
3006 Program *programObject = getCurrentProgram();
3007 sw::VertexShader *vertexShader = programObject->getVertexShader();
3008 sw::PixelShader *pixelShader = programObject->getPixelShader();
3010 device->setVertexShader(vertexShader);
3011 device->setPixelShader(pixelShader);
3013 if(programObject->getSerial() != mAppliedProgramSerial)
3015 programObject->dirtyAllUniforms();
3016 mAppliedProgramSerial = programObject->getSerial();
3019 programObject->applyTransformFeedback(getTransformFeedback());
3020 programObject->applyUniformBuffers(mState.uniformBuffers);
3021 programObject->applyUniforms();
3024 void Context::applyTextures()
3026 applyTextures(sw::SAMPLER_PIXEL);
3027 applyTextures(sw::SAMPLER_VERTEX);
3030 void Context::applyTextures(sw::SamplerType samplerType)
3032 Program *programObject = getCurrentProgram();
3034 int samplerCount = (samplerType == sw::SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS; // Range of samplers of given sampler type
3036 for(int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
3038 int textureUnit = programObject->getSamplerMapping(samplerType, samplerIndex); // OpenGL texture image unit index
3040 if(textureUnit != -1)
3042 TextureType textureType = programObject->getSamplerTextureType(samplerType, samplerIndex);
3044 Texture *texture = getSamplerTexture(textureUnit, textureType);
3046 if(texture->isSamplerComplete())
3048 GLenum wrapS, wrapT, wrapR, minFilter, magFilter;
3049 GLfloat minLOD, maxLOD;
3051 Sampler *samplerObject = mState.sampler[textureUnit];
3054 wrapS = samplerObject->getWrapS();
3055 wrapT = samplerObject->getWrapT();
3056 wrapR = samplerObject->getWrapR();
3057 minFilter = samplerObject->getMinFilter();
3058 magFilter = samplerObject->getMagFilter();
3059 minLOD = samplerObject->getMinLod();
3060 maxLOD = samplerObject->getMaxLod();
3064 wrapS = texture->getWrapS();
3065 wrapT = texture->getWrapT();
3066 wrapR = texture->getWrapR();
3067 minFilter = texture->getMinFilter();
3068 magFilter = texture->getMagFilter();
3069 minLOD = texture->getMinLOD();
3070 maxLOD = texture->getMaxLOD();
3072 GLfloat maxAnisotropy = texture->getMaxAnisotropy();
3074 GLint baseLevel = texture->getBaseLevel();
3075 GLint maxLevel = texture->getMaxLevel();
3076 GLenum swizzleR = texture->getSwizzleR();
3077 GLenum swizzleG = texture->getSwizzleG();
3078 GLenum swizzleB = texture->getSwizzleB();
3079 GLenum swizzleA = texture->getSwizzleA();
3081 device->setAddressingModeU(samplerType, samplerIndex, es2sw::ConvertTextureWrap(wrapS));
3082 device->setAddressingModeV(samplerType, samplerIndex, es2sw::ConvertTextureWrap(wrapT));
3083 device->setAddressingModeW(samplerType, samplerIndex, es2sw::ConvertTextureWrap(wrapR));
3084 device->setSwizzleR(samplerType, samplerIndex, es2sw::ConvertSwizzleType(swizzleR));
3085 device->setSwizzleG(samplerType, samplerIndex, es2sw::ConvertSwizzleType(swizzleG));
3086 device->setSwizzleB(samplerType, samplerIndex, es2sw::ConvertSwizzleType(swizzleB));
3087 device->setSwizzleA(samplerType, samplerIndex, es2sw::ConvertSwizzleType(swizzleA));
3088 device->setMinLod(samplerType, samplerIndex, minLOD);
3089 device->setMaxLod(samplerType, samplerIndex, maxLOD);
3090 device->setBaseLevel(samplerType, samplerIndex, baseLevel);
3091 device->setMaxLevel(samplerType, samplerIndex, maxLevel);
3093 device->setTextureFilter(samplerType, samplerIndex, es2sw::ConvertTextureFilter(minFilter, magFilter, maxAnisotropy));
3094 device->setMipmapFilter(samplerType, samplerIndex, es2sw::ConvertMipMapFilter(minFilter));
3095 device->setMaxAnisotropy(samplerType, samplerIndex, maxAnisotropy);
3097 applyTexture(samplerType, samplerIndex, texture);
3101 applyTexture(samplerType, samplerIndex, nullptr);
3106 applyTexture(samplerType, samplerIndex, nullptr);
3111 void Context::applyTexture(sw::SamplerType type, int index, Texture *baseTexture)
3113 Program *program = getCurrentProgram();
3114 int sampler = (type == sw::SAMPLER_PIXEL) ? index : 16 + index;
3115 bool textureUsed = false;
3117 if(type == sw::SAMPLER_PIXEL)
3119 textureUsed = program->getPixelShader()->usesSampler(index);
3121 else if(type == sw::SAMPLER_VERTEX)
3123 textureUsed = program->getVertexShader()->usesSampler(index);
3125 else UNREACHABLE(type);
3127 sw::Resource *resource = 0;
3129 if(baseTexture && textureUsed)
3131 resource = baseTexture->getResource();
3134 device->setTextureResource(sampler, resource);
3136 if(baseTexture && textureUsed)
3138 int levelCount = baseTexture->getLevelCount();
3140 if(baseTexture->getTarget() == GL_TEXTURE_2D || baseTexture->getTarget() == GL_TEXTURE_EXTERNAL_OES)
3142 Texture2D *texture = static_cast<Texture2D*>(baseTexture);
3144 for(int mipmapLevel = 0; mipmapLevel < sw::MIPMAP_LEVELS; mipmapLevel++)
3146 int surfaceLevel = mipmapLevel;
3148 if(surfaceLevel < 0)
3152 else if(surfaceLevel >= levelCount)
3154 surfaceLevel = levelCount - 1;
3157 egl::Image *surface = texture->getImage(surfaceLevel);
3158 device->setTextureLevel(sampler, 0, mipmapLevel, surface, sw::TEXTURE_2D);
3161 else if(baseTexture->getTarget() == GL_TEXTURE_3D_OES)
3163 Texture3D *texture = static_cast<Texture3D*>(baseTexture);
3165 for(int mipmapLevel = 0; mipmapLevel < sw::MIPMAP_LEVELS; mipmapLevel++)
3167 int surfaceLevel = mipmapLevel;
3169 if(surfaceLevel < 0)
3173 else if(surfaceLevel >= levelCount)
3175 surfaceLevel = levelCount - 1;
3178 egl::Image *surface = texture->getImage(surfaceLevel);
3179 device->setTextureLevel(sampler, 0, mipmapLevel, surface, sw::TEXTURE_3D);
3182 else if(baseTexture->getTarget() == GL_TEXTURE_2D_ARRAY)
3184 Texture2DArray *texture = static_cast<Texture2DArray*>(baseTexture);
3186 for(int mipmapLevel = 0; mipmapLevel < sw::MIPMAP_LEVELS; mipmapLevel++)
3188 int surfaceLevel = mipmapLevel;
3190 if(surfaceLevel < 0)
3194 else if(surfaceLevel >= levelCount)
3196 surfaceLevel = levelCount - 1;
3199 egl::Image *surface = texture->getImage(surfaceLevel);
3200 device->setTextureLevel(sampler, 0, mipmapLevel, surface, sw::TEXTURE_2D_ARRAY);
3203 else if(baseTexture->getTarget() == GL_TEXTURE_CUBE_MAP)
3205 for(int face = 0; face < 6; face++)
3207 TextureCubeMap *cubeTexture = static_cast<TextureCubeMap*>(baseTexture);
3209 for(int mipmapLevel = 0; mipmapLevel < sw::MIPMAP_LEVELS; mipmapLevel++)
3211 int surfaceLevel = mipmapLevel;
3213 if(surfaceLevel < 0)
3217 else if(surfaceLevel >= levelCount)
3219 surfaceLevel = levelCount - 1;
3222 egl::Image *surface = cubeTexture->getImage(face, surfaceLevel);
3223 device->setTextureLevel(sampler, face, mipmapLevel, surface, sw::TEXTURE_CUBE);
3227 else UNIMPLEMENTED();
3231 device->setTextureLevel(sampler, 0, 0, 0, sw::TEXTURE_NULL);
3235 void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
3237 Framebuffer *framebuffer = getReadFramebuffer();
3238 int framebufferWidth, framebufferHeight, framebufferSamples;
3240 if(framebuffer->completeness(framebufferWidth, framebufferHeight, framebufferSamples) != GL_FRAMEBUFFER_COMPLETE)
3242 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3245 if(getReadFramebufferName() != 0 && framebufferSamples != 0)
3247 return error(GL_INVALID_OPERATION);
3250 GLenum readFormat = GL_NONE;
3251 GLenum readType = GL_NONE;
3254 case GL_DEPTH_COMPONENT:
3255 readFormat = framebuffer->getDepthReadFormat();
3256 readType = framebuffer->getDepthReadType();
3259 readFormat = framebuffer->getImplementationColorReadFormat();
3260 readType = framebuffer->getImplementationColorReadType();
3264 if(!(readFormat == format && readType == type) && !ValidReadPixelsFormatType(readFormat, readType, format, type, clientVersion))
3266 return error(GL_INVALID_OPERATION);
3269 GLsizei outputWidth = (mState.packRowLength > 0) ? mState.packRowLength : width;
3270 GLsizei outputPitch = egl::ComputePitch(outputWidth, format, type, mState.packAlignment);
3271 GLsizei outputHeight = (mState.packImageHeight == 0) ? height : mState.packImageHeight;
3272 pixels = getPixelPackBuffer() ? (unsigned char*)getPixelPackBuffer()->data() + (ptrdiff_t)pixels : (unsigned char*)pixels;
3273 pixels = ((char*)pixels) + egl::ComputePackingOffset(format, type, outputWidth, outputHeight, mState.packAlignment, mState.packSkipImages, mState.packSkipRows, mState.packSkipPixels);
3275 // Sized query sanity check
3278 int requiredSize = outputPitch * height;
3279 if(requiredSize > *bufSize)
3281 return error(GL_INVALID_OPERATION);
3285 egl::Image *renderTarget = nullptr;
3288 case GL_DEPTH_COMPONENT:
3289 renderTarget = framebuffer->getDepthBuffer();
3292 renderTarget = framebuffer->getReadRenderTarget();
3298 return error(GL_INVALID_OPERATION);
3301 sw::Rect rect = {x, y, x + width, y + height};
3302 sw::Rect dstRect = { 0, 0, width, height };
3303 rect.clip(0, 0, renderTarget->getWidth(), renderTarget->getHeight());
3305 sw::Surface externalSurface(width, height, 1, egl::ConvertFormatType(format, type), pixels, outputPitch, outputPitch * outputHeight);
3306 sw::SliceRect sliceRect(rect);
3307 sw::SliceRect dstSliceRect(dstRect);
3308 device->blit(renderTarget, sliceRect, &externalSurface, dstSliceRect, false);
3310 renderTarget->release();
3313 void Context::clear(GLbitfield mask)
3315 if(mState.rasterizerDiscardEnabled)
3320 Framebuffer *framebuffer = getDrawFramebuffer();
3322 if(!framebuffer || framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3324 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3327 if(!applyRenderTarget())
3332 if(mask & GL_COLOR_BUFFER_BIT)
3334 unsigned int rgbaMask = getColorMask();
3338 device->clearColor(mState.colorClearValue.red, mState.colorClearValue.green, mState.colorClearValue.blue, mState.colorClearValue.alpha, rgbaMask);
3342 if(mask & GL_DEPTH_BUFFER_BIT)
3344 if(mState.depthMask != 0)
3346 float depth = clamp01(mState.depthClearValue);
3347 device->clearDepth(depth);
3351 if(mask & GL_STENCIL_BUFFER_BIT)
3353 if(mState.stencilWritemask != 0)
3355 int stencil = mState.stencilClearValue & 0x000000FF;
3356 device->clearStencil(stencil, mState.stencilWritemask);
3361 void Context::clearColorBuffer(GLint drawbuffer, void *value, sw::Format format)
3363 unsigned int rgbaMask = getColorMask();
3364 if(rgbaMask && !mState.rasterizerDiscardEnabled)
3366 Framebuffer *framebuffer = getDrawFramebuffer();
3367 egl::Image *colorbuffer = framebuffer->getRenderTarget(drawbuffer);
3371 sw::SliceRect clearRect = colorbuffer->getRect();
3373 if(mState.scissorTestEnabled)
3375 clearRect.clip(mState.scissorX, mState.scissorY, mState.scissorX + mState.scissorWidth, mState.scissorY + mState.scissorHeight);
3378 device->clear(value, format, colorbuffer, clearRect, rgbaMask);
3380 colorbuffer->release();
3385 void Context::clearColorBuffer(GLint drawbuffer, const GLint *value)
3387 clearColorBuffer(drawbuffer, (void*)value, sw::FORMAT_A32B32G32R32I);
3390 void Context::clearColorBuffer(GLint drawbuffer, const GLuint *value)
3392 clearColorBuffer(drawbuffer, (void*)value, sw::FORMAT_A32B32G32R32UI);
3395 void Context::clearColorBuffer(GLint drawbuffer, const GLfloat *value)
3397 clearColorBuffer(drawbuffer, (void*)value, sw::FORMAT_A32B32G32R32F);
3400 void Context::clearDepthBuffer(const GLfloat value)
3402 if(mState.depthMask && !mState.rasterizerDiscardEnabled)
3404 Framebuffer *framebuffer = getDrawFramebuffer();
3405 egl::Image *depthbuffer = framebuffer->getDepthBuffer();
3409 float depth = clamp01(value);
3410 sw::SliceRect clearRect = depthbuffer->getRect();
3412 if(mState.scissorTestEnabled)
3414 clearRect.clip(mState.scissorX, mState.scissorY, mState.scissorX + mState.scissorWidth, mState.scissorY + mState.scissorHeight);
3417 depthbuffer->clearDepth(depth, clearRect.x0, clearRect.y0, clearRect.width(), clearRect.height());
3419 depthbuffer->release();
3424 void Context::clearStencilBuffer(const GLint value)
3426 if(mState.stencilWritemask && !mState.rasterizerDiscardEnabled)
3428 Framebuffer *framebuffer = getDrawFramebuffer();
3429 egl::Image *stencilbuffer = framebuffer->getStencilBuffer();
3433 unsigned char stencil = value < 0 ? 0 : static_cast<unsigned char>(value & 0x000000FF);
3434 sw::SliceRect clearRect = stencilbuffer->getRect();
3436 if(mState.scissorTestEnabled)
3438 clearRect.clip(mState.scissorX, mState.scissorY, mState.scissorX + mState.scissorWidth, mState.scissorY + mState.scissorHeight);
3441 stencilbuffer->clearStencil(stencil, static_cast<unsigned char>(mState.stencilWritemask), clearRect.x0, clearRect.y0, clearRect.width(), clearRect.height());
3443 stencilbuffer->release();
3448 void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instanceCount)
3450 if(!mState.currentProgram)
3452 return error(GL_INVALID_OPERATION);
3455 sw::DrawType primitiveType;
3457 int verticesPerPrimitive;
3459 if(!es2sw::ConvertPrimitiveType(mode, count, GL_NONE, primitiveType, primitiveCount, verticesPerPrimitive))
3460 return error(GL_INVALID_ENUM);
3462 if(primitiveCount <= 0)
3467 if(!applyRenderTarget())
3474 for(int i = 0; i < instanceCount; ++i)
3476 device->setInstanceID(i);
3478 GLenum err = applyVertexBuffer(0, first, count, i);
3479 if(err != GL_NO_ERROR)
3487 if(!getCurrentProgram()->validateSamplers(false))
3489 return error(GL_INVALID_OPERATION);
3492 TransformFeedback* transformFeedback = getTransformFeedback();
3493 if(!cullSkipsDraw(mode) || (transformFeedback->isActive() && !transformFeedback->isPaused()))
3495 device->drawPrimitive(primitiveType, primitiveCount);
3497 if(transformFeedback)
3499 transformFeedback->addVertexOffset(primitiveCount * verticesPerPrimitive);
3504 void Context::drawElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const void *indices, GLsizei instanceCount)
3506 if(!mState.currentProgram)
3508 return error(GL_INVALID_OPERATION);
3511 if(!indices && !getCurrentVertexArray()->getElementArrayBuffer())
3513 return error(GL_INVALID_OPERATION);
3516 sw::DrawType primitiveType;
3518 int verticesPerPrimitive;
3520 if(!es2sw::ConvertPrimitiveType(mode, count, type, primitiveType, primitiveCount, verticesPerPrimitive))
3521 return error(GL_INVALID_ENUM);
3523 if(primitiveCount <= 0)
3528 if(!applyRenderTarget())
3535 for(int i = 0; i < instanceCount; ++i)
3537 device->setInstanceID(i);
3539 TranslatedIndexData indexInfo;
3540 GLenum err = applyIndexBuffer(indices, start, end, count, mode, type, &indexInfo);
3541 if(err != GL_NO_ERROR)
3546 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
3547 err = applyVertexBuffer(-(int)indexInfo.minIndex, indexInfo.minIndex, vertexCount, i);
3548 if(err != GL_NO_ERROR)
3556 if(!getCurrentProgram()->validateSamplers(false))
3558 return error(GL_INVALID_OPERATION);
3561 TransformFeedback* transformFeedback = getTransformFeedback();
3562 if(!cullSkipsDraw(mode) || (transformFeedback->isActive() && !transformFeedback->isPaused()))
3564 device->drawIndexedPrimitive(primitiveType, indexInfo.indexOffset, primitiveCount);
3566 if(transformFeedback)
3568 transformFeedback->addVertexOffset(primitiveCount * verticesPerPrimitive);
3573 void Context::finish()
3578 void Context::flush()
3580 // We don't queue anything without processing it as fast as possible
3583 void Context::recordInvalidEnum()
3585 mInvalidEnum = true;
3588 void Context::recordInvalidValue()
3590 mInvalidValue = true;
3593 void Context::recordInvalidOperation()
3595 mInvalidOperation = true;
3598 void Context::recordOutOfMemory()
3600 mOutOfMemory = true;
3603 void Context::recordInvalidFramebufferOperation()
3605 mInvalidFramebufferOperation = true;
3608 // Get one of the recorded errors and clear its flag, if any.
3609 // [OpenGL ES 2.0.24] section 2.5 page 13.
3610 GLenum Context::getError()
3614 mInvalidEnum = false;
3616 return GL_INVALID_ENUM;
3621 mInvalidValue = false;
3623 return GL_INVALID_VALUE;
3626 if(mInvalidOperation)
3628 mInvalidOperation = false;
3630 return GL_INVALID_OPERATION;
3635 mOutOfMemory = false;
3637 return GL_OUT_OF_MEMORY;
3640 if(mInvalidFramebufferOperation)
3642 mInvalidFramebufferOperation = false;
3644 return GL_INVALID_FRAMEBUFFER_OPERATION;
3650 int Context::getSupportedMultisampleCount(int requested)
3654 for(int i = NUM_MULTISAMPLE_COUNTS - 1; i >= 0; i--)
3656 if(supported >= requested)
3661 supported = multisampleCount[i];
3667 void Context::detachBuffer(GLuint buffer)
3669 // [OpenGL ES 2.0.24] section 2.9 page 22:
3670 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3671 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3673 if(mState.copyReadBuffer.name() == buffer)
3675 mState.copyReadBuffer = nullptr;
3678 if(mState.copyWriteBuffer.name() == buffer)
3680 mState.copyWriteBuffer = nullptr;
3683 if(mState.pixelPackBuffer.name() == buffer)
3685 mState.pixelPackBuffer = nullptr;
3688 if(mState.pixelUnpackBuffer.name() == buffer)
3690 mState.pixelUnpackBuffer = nullptr;
3693 if(mState.genericUniformBuffer.name() == buffer)
3695 mState.genericUniformBuffer = nullptr;
3698 if(getArrayBufferName() == buffer)
3700 mState.arrayBuffer = nullptr;
3703 // Only detach from the current transform feedback
3704 TransformFeedback* currentTransformFeedback = getTransformFeedback();
3705 if(currentTransformFeedback)
3707 currentTransformFeedback->detachBuffer(buffer);
3710 // Only detach from the current vertex array
3711 VertexArray* currentVertexArray = getCurrentVertexArray();
3712 if(currentVertexArray)
3714 currentVertexArray->detachBuffer(buffer);
3717 for(int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3719 if(mState.vertexAttribute[attribute].mBoundBuffer.name() == buffer)
3721 mState.vertexAttribute[attribute].mBoundBuffer = nullptr;
3726 void Context::detachTexture(GLuint texture)
3728 // [OpenGL ES 2.0.24] section 3.8 page 84:
3729 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3730 // rebound to texture object zero
3732 for(int type = 0; type < TEXTURE_TYPE_COUNT; type++)
3734 for(int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS; sampler++)
3736 if(mState.samplerTexture[type][sampler].name() == texture)
3738 mState.samplerTexture[type][sampler] = nullptr;
3743 // [OpenGL ES 2.0.24] section 4.4 page 112:
3744 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3745 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3746 // image was attached in the currently bound framebuffer.
3748 Framebuffer *readFramebuffer = getReadFramebuffer();
3749 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3753 readFramebuffer->detachTexture(texture);
3756 if(drawFramebuffer && drawFramebuffer != readFramebuffer)
3758 drawFramebuffer->detachTexture(texture);
3762 void Context::detachFramebuffer(GLuint framebuffer)
3764 // [OpenGL ES 2.0.24] section 4.4 page 107:
3765 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3766 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3768 if(mState.readFramebuffer == framebuffer)
3770 bindReadFramebuffer(0);
3773 if(mState.drawFramebuffer == framebuffer)
3775 bindDrawFramebuffer(0);
3779 void Context::detachRenderbuffer(GLuint renderbuffer)
3781 // [OpenGL ES 2.0.24] section 4.4 page 109:
3782 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3783 // had been executed with the target RENDERBUFFER and name of zero.
3785 if(mState.renderbuffer.name() == renderbuffer)
3787 bindRenderbuffer(0);
3790 // [OpenGL ES 2.0.24] section 4.4 page 111:
3791 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3792 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3793 // point to which this image was attached in the currently bound framebuffer.
3795 Framebuffer *readFramebuffer = getReadFramebuffer();
3796 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3800 readFramebuffer->detachRenderbuffer(renderbuffer);
3803 if(drawFramebuffer && drawFramebuffer != readFramebuffer)
3805 drawFramebuffer->detachRenderbuffer(renderbuffer);
3809 void Context::detachSampler(GLuint sampler)
3811 // [OpenGL ES 3.0.2] section 3.8.2 pages 123-124:
3812 // If a sampler object that is currently bound to one or more texture units is
3813 // deleted, it is as though BindSampler is called once for each texture unit to
3814 // which the sampler is bound, with unit set to the texture unit and sampler set to zero.
3815 for(size_t textureUnit = 0; textureUnit < MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++textureUnit)
3817 gl::BindingPointer<Sampler> &samplerBinding = mState.sampler[textureUnit];
3818 if(samplerBinding.name() == sampler)
3820 samplerBinding = nullptr;
3825 bool Context::cullSkipsDraw(GLenum drawMode)
3827 return mState.cullFaceEnabled && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
3830 bool Context::isTriangleMode(GLenum drawMode)
3835 case GL_TRIANGLE_FAN:
3836 case GL_TRIANGLE_STRIP:
3843 default: UNREACHABLE(drawMode);
3849 void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3851 ASSERT(index < MAX_VERTEX_ATTRIBS);
3853 mState.vertexAttribute[index].setCurrentValue(values);
3855 mVertexDataManager->dirtyCurrentValue(index);
3858 void Context::setVertexAttrib(GLuint index, const GLint *values)
3860 ASSERT(index < MAX_VERTEX_ATTRIBS);
3862 mState.vertexAttribute[index].setCurrentValue(values);
3864 mVertexDataManager->dirtyCurrentValue(index);
3867 void Context::setVertexAttrib(GLuint index, const GLuint *values)
3869 ASSERT(index < MAX_VERTEX_ATTRIBS);
3871 mState.vertexAttribute[index].setCurrentValue(values);
3873 mVertexDataManager->dirtyCurrentValue(index);
3876 void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3877 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3880 Framebuffer *readFramebuffer = getReadFramebuffer();
3881 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3883 int readBufferWidth, readBufferHeight, readBufferSamples;
3884 int drawBufferWidth, drawBufferHeight, drawBufferSamples;
3886 if(!readFramebuffer || readFramebuffer->completeness(readBufferWidth, readBufferHeight, readBufferSamples) != GL_FRAMEBUFFER_COMPLETE ||
3887 !drawFramebuffer || drawFramebuffer->completeness(drawBufferWidth, drawBufferHeight, drawBufferSamples) != GL_FRAMEBUFFER_COMPLETE)
3889 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3892 if(drawBufferSamples > 1)
3894 return error(GL_INVALID_OPERATION);
3897 sw::SliceRect sourceRect;
3898 sw::SliceRect destRect;
3899 bool flipX = (srcX0 < srcX1) ^ (dstX0 < dstX1);
3900 bool flipy = (srcY0 < srcY1) ^ (dstY0 < dstY1);
3904 sourceRect.x0 = srcX0;
3905 sourceRect.x1 = srcX1;
3909 sourceRect.x0 = srcX1;
3910 sourceRect.x1 = srcX0;
3915 destRect.x0 = dstX0;
3916 destRect.x1 = dstX1;
3920 destRect.x0 = dstX1;
3921 destRect.x1 = dstX0;
3926 sourceRect.y0 = srcY0;
3927 sourceRect.y1 = srcY1;
3931 sourceRect.y0 = srcY1;
3932 sourceRect.y1 = srcY0;
3937 destRect.y0 = dstY0;
3938 destRect.y1 = dstY1;
3942 destRect.y0 = dstY1;
3943 destRect.y1 = dstY0;
3946 sw::Rect sourceScissoredRect = sourceRect;
3947 sw::Rect destScissoredRect = destRect;
3949 if(mState.scissorTestEnabled) // Only write to parts of the destination framebuffer which pass the scissor test
3951 if(destRect.x0 < mState.scissorX)
3953 int xDiff = mState.scissorX - destRect.x0;
3954 destScissoredRect.x0 = mState.scissorX;
3955 sourceScissoredRect.x0 += xDiff;
3958 if(destRect.x1 > mState.scissorX + mState.scissorWidth)
3960 int xDiff = destRect.x1 - (mState.scissorX + mState.scissorWidth);
3961 destScissoredRect.x1 = mState.scissorX + mState.scissorWidth;
3962 sourceScissoredRect.x1 -= xDiff;
3965 if(destRect.y0 < mState.scissorY)
3967 int yDiff = mState.scissorY - destRect.y0;
3968 destScissoredRect.y0 = mState.scissorY;
3969 sourceScissoredRect.y0 += yDiff;
3972 if(destRect.y1 > mState.scissorY + mState.scissorHeight)
3974 int yDiff = destRect.y1 - (mState.scissorY + mState.scissorHeight);
3975 destScissoredRect.y1 = mState.scissorY + mState.scissorHeight;
3976 sourceScissoredRect.y1 -= yDiff;
3980 sw::Rect sourceTrimmedRect = sourceScissoredRect;
3981 sw::Rect destTrimmedRect = destScissoredRect;
3983 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3984 // the actual draw and read surfaces.
3985 if(sourceTrimmedRect.x0 < 0)
3987 int xDiff = 0 - sourceTrimmedRect.x0;
3988 sourceTrimmedRect.x0 = 0;
3989 destTrimmedRect.x0 += xDiff;
3992 if(sourceTrimmedRect.x1 > readBufferWidth)
3994 int xDiff = sourceTrimmedRect.x1 - readBufferWidth;
3995 sourceTrimmedRect.x1 = readBufferWidth;
3996 destTrimmedRect.x1 -= xDiff;
3999 if(sourceTrimmedRect.y0 < 0)
4001 int yDiff = 0 - sourceTrimmedRect.y0;
4002 sourceTrimmedRect.y0 = 0;
4003 destTrimmedRect.y0 += yDiff;
4006 if(sourceTrimmedRect.y1 > readBufferHeight)
4008 int yDiff = sourceTrimmedRect.y1 - readBufferHeight;
4009 sourceTrimmedRect.y1 = readBufferHeight;
4010 destTrimmedRect.y1 -= yDiff;
4013 if(destTrimmedRect.x0 < 0)
4015 int xDiff = 0 - destTrimmedRect.x0;
4016 destTrimmedRect.x0 = 0;
4017 sourceTrimmedRect.x0 += xDiff;
4020 if(destTrimmedRect.x1 > drawBufferWidth)
4022 int xDiff = destTrimmedRect.x1 - drawBufferWidth;
4023 destTrimmedRect.x1 = drawBufferWidth;
4024 sourceTrimmedRect.x1 -= xDiff;
4027 if(destTrimmedRect.y0 < 0)
4029 int yDiff = 0 - destTrimmedRect.y0;
4030 destTrimmedRect.y0 = 0;
4031 sourceTrimmedRect.y0 += yDiff;
4034 if(destTrimmedRect.y1 > drawBufferHeight)
4036 int yDiff = destTrimmedRect.y1 - drawBufferHeight;
4037 destTrimmedRect.y1 = drawBufferHeight;
4038 sourceTrimmedRect.y1 -= yDiff;
4041 bool partialBufferCopy = false;
4043 if(sourceTrimmedRect.y1 - sourceTrimmedRect.y0 < readBufferHeight ||
4044 sourceTrimmedRect.x1 - sourceTrimmedRect.x0 < readBufferWidth ||
4045 destTrimmedRect.y1 - destTrimmedRect.y0 < drawBufferHeight ||
4046 destTrimmedRect.x1 - destTrimmedRect.x0 < drawBufferWidth ||
4047 sourceTrimmedRect.y0 != 0 || destTrimmedRect.y0 != 0 || sourceTrimmedRect.x0 != 0 || destTrimmedRect.x0 != 0)
4049 partialBufferCopy = true;
4052 bool blitRenderTarget = false;
4053 bool blitDepthStencil = false;
4055 if(mask & GL_COLOR_BUFFER_BIT)
4057 GLenum readColorbufferType = readFramebuffer->getColorbufferType(getReadFramebufferColorIndex());
4058 GLenum drawColorbufferType = drawFramebuffer->getColorbufferType(0);
4059 const bool validReadType = readColorbufferType == GL_TEXTURE_2D || Framebuffer::IsRenderbuffer(readColorbufferType);
4060 const bool validDrawType = drawColorbufferType == GL_TEXTURE_2D || Framebuffer::IsRenderbuffer(drawColorbufferType);
4061 if(!validReadType || !validDrawType)
4063 return error(GL_INVALID_OPERATION);
4066 if(partialBufferCopy && readBufferSamples > 1)
4068 return error(GL_INVALID_OPERATION);
4071 blitRenderTarget = true;
4074 if(mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
4076 Renderbuffer *readDSBuffer = nullptr;
4077 Renderbuffer *drawDSBuffer = nullptr;
4079 if(mask & GL_DEPTH_BUFFER_BIT)
4081 if(readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
4083 if(readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType())
4085 return error(GL_INVALID_OPERATION);
4088 blitDepthStencil = true;
4089 readDSBuffer = readFramebuffer->getDepthbuffer();
4090 drawDSBuffer = drawFramebuffer->getDepthbuffer();
4094 if(mask & GL_STENCIL_BUFFER_BIT)
4096 if(readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
4098 if(readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType())
4100 return error(GL_INVALID_OPERATION);
4103 blitDepthStencil = true;
4104 readDSBuffer = readFramebuffer->getStencilbuffer();
4105 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4109 if(partialBufferCopy)
4111 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
4112 return error(GL_INVALID_OPERATION); // Only whole-buffer copies are permitted
4115 if((drawDSBuffer && drawDSBuffer->getSamples() > 1) ||
4116 (readDSBuffer && readDSBuffer->getSamples() > 1))
4118 return error(GL_INVALID_OPERATION);
4122 if(blitRenderTarget || blitDepthStencil)
4124 if(blitRenderTarget)
4126 egl::Image *readRenderTarget = readFramebuffer->getReadRenderTarget();
4127 egl::Image *drawRenderTarget = drawFramebuffer->getRenderTarget(0);
4131 swap(destRect.x0, destRect.x1);
4135 swap(destRect.y0, destRect.y1);
4138 bool success = device->stretchRect(readRenderTarget, &sourceRect, drawRenderTarget, &destRect, false);
4140 readRenderTarget->release();
4141 drawRenderTarget->release();
4145 ERR("BlitFramebuffer failed.");
4150 if(blitDepthStencil)
4152 bool success = device->stretchRect(readFramebuffer->getDepthBuffer(), nullptr, drawFramebuffer->getDepthBuffer(), nullptr, false);
4156 ERR("BlitFramebuffer failed.");
4163 void Context::bindTexImage(egl::Surface *surface)
4165 es2::Texture2D *textureObject = getTexture2D();
4169 textureObject->bindTexImage(surface);
4173 EGLenum Context::validateSharedImage(EGLenum target, GLuint name, GLuint textureLevel)
4175 GLenum textureTarget = GL_NONE;
4179 case EGL_GL_TEXTURE_2D_KHR:
4180 textureTarget = GL_TEXTURE_2D;
4182 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
4183 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
4184 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
4185 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
4186 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
4187 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
4188 textureTarget = GL_TEXTURE_CUBE_MAP;
4190 case EGL_GL_RENDERBUFFER_KHR:
4193 return EGL_BAD_PARAMETER;
4196 if(textureLevel >= es2::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
4198 return EGL_BAD_MATCH;
4201 if(textureTarget != GL_NONE)
4203 es2::Texture *texture = getTexture(name);
4205 if(!texture || texture->getTarget() != textureTarget)
4207 return EGL_BAD_PARAMETER;
4210 if(texture->isShared(textureTarget, textureLevel)) // Bound to an EGLSurface or already an EGLImage sibling
4212 return EGL_BAD_ACCESS;
4215 if(textureLevel != 0 && !texture->isSamplerComplete())
4217 return EGL_BAD_PARAMETER;
4220 if(textureLevel == 0 && !(texture->isSamplerComplete() && texture->getLevelCount() == 1))
4222 return EGL_BAD_PARAMETER;
4225 else if(target == EGL_GL_RENDERBUFFER_KHR)
4227 es2::Renderbuffer *renderbuffer = getRenderbuffer(name);
4231 return EGL_BAD_PARAMETER;
4234 if(renderbuffer->isShared()) // Already an EGLImage sibling
4236 return EGL_BAD_ACCESS;
4239 else UNREACHABLE(target);
4244 egl::Image *Context::createSharedImage(EGLenum target, GLuint name, GLuint textureLevel)
4246 GLenum textureTarget = GL_NONE;
4250 case EGL_GL_TEXTURE_2D_KHR: textureTarget = GL_TEXTURE_2D; break;
4251 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR: textureTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X; break;
4252 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR: textureTarget = GL_TEXTURE_CUBE_MAP_NEGATIVE_X; break;
4253 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR: textureTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_Y; break;
4254 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR: textureTarget = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y; break;
4255 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR: textureTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_Z; break;
4256 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR: textureTarget = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; break;
4259 if(textureTarget != GL_NONE)
4261 es2::Texture *texture = getTexture(name);
4263 return texture->createSharedImage(textureTarget, textureLevel);
4265 else if(target == EGL_GL_RENDERBUFFER_KHR)
4267 es2::Renderbuffer *renderbuffer = getRenderbuffer(name);
4269 return renderbuffer->createSharedImage();
4271 else UNREACHABLE(target);
4276 egl::Image *Context::getSharedImage(GLeglImageOES image)
4278 return display->getSharedImage(image);
4281 Device *Context::getDevice()
4286 const GLubyte* Context::getExtensions(GLuint index, GLuint* numExt) const
4288 // Keep list sorted in following order:
4291 // Vendor extensions
4292 static const GLubyte* extensions[] = {
4293 (const GLubyte*)"GL_OES_compressed_ETC1_RGB8_texture",
4294 (const GLubyte*)"GL_OES_depth24",
4295 (const GLubyte*)"GL_OES_depth32",
4296 (const GLubyte*)"GL_OES_depth_texture",
4297 (const GLubyte*)"GL_OES_depth_texture_cube_map",
4298 (const GLubyte*)"GL_OES_EGL_image",
4299 (const GLubyte*)"GL_OES_EGL_image_external",
4300 (const GLubyte*)"GL_OES_EGL_sync",
4301 (const GLubyte*)"GL_OES_element_index_uint",
4302 (const GLubyte*)"GL_OES_framebuffer_object",
4303 (const GLubyte*)"GL_OES_packed_depth_stencil",
4304 (const GLubyte*)"GL_OES_rgb8_rgba8",
4305 (const GLubyte*)"GL_OES_standard_derivatives",
4306 (const GLubyte*)"GL_OES_texture_float",
4307 (const GLubyte*)"GL_OES_texture_float_linear",
4308 (const GLubyte*)"GL_OES_texture_half_float",
4309 (const GLubyte*)"GL_OES_texture_half_float_linear",
4310 (const GLubyte*)"GL_OES_texture_npot",
4311 (const GLubyte*)"GL_OES_texture_3D",
4312 (const GLubyte*)"GL_EXT_blend_minmax",
4313 (const GLubyte*)"GL_EXT_color_buffer_half_float",
4314 (const GLubyte*)"GL_EXT_draw_buffers",
4315 (const GLubyte*)"GL_EXT_occlusion_query_boolean",
4316 (const GLubyte*)"GL_EXT_read_format_bgra",
4318 (const GLubyte*)"GL_EXT_texture_compression_dxt1",
4320 (const GLubyte*)"GL_EXT_texture_filter_anisotropic",
4321 (const GLubyte*)"GL_EXT_texture_format_BGRA8888",
4322 (const GLubyte*)"GL_ANGLE_framebuffer_blit",
4323 (const GLubyte*)"GL_NV_framebuffer_blit",
4324 (const GLubyte*)"GL_ANGLE_framebuffer_multisample",
4326 (const GLubyte*)"GL_ANGLE_texture_compression_dxt3",
4327 (const GLubyte*)"GL_ANGLE_texture_compression_dxt5",
4329 (const GLubyte*)"GL_NV_fence",
4330 (const GLubyte*)"GL_NV_read_depth",
4331 (const GLubyte*)"GL_EXT_instanced_arrays",
4332 (const GLubyte*)"GL_ANGLE_instanced_arrays",
4334 static const GLuint numExtensions = sizeof(extensions) / sizeof(*extensions);
4338 *numExt = numExtensions;
4342 if(index == GL_INVALID_INDEX)
4344 static GLubyte* extensionsCat = nullptr;
4345 if(!extensionsCat && (numExtensions > 0))
4347 size_t totalLength = numExtensions; // 1 space between each extension name + terminating null
4348 for(unsigned int i = 0; i < numExtensions; i++)
4350 totalLength += strlen(reinterpret_cast<const char*>(extensions[i]));
4352 extensionsCat = new GLubyte[totalLength];
4353 extensionsCat[0] = '\0';
4354 for(unsigned int i = 0; i < numExtensions; i++)
4358 strcat(reinterpret_cast<char*>(extensionsCat), " ");
4360 strcat(reinterpret_cast<char*>(extensionsCat), reinterpret_cast<const char*>(extensions[i]));
4363 return extensionsCat;
4366 if(index >= numExtensions)
4371 return extensions[index];
4376 egl::Context *es2CreateContext(egl::Display *display, const egl::Context *shareContext, int clientVersion)
4378 ASSERT(!shareContext || shareContext->getClientVersion() == clientVersion); // Should be checked by eglCreateContext
4379 return new es2::Context(display, static_cast<const es2::Context*>(shareContext), clientVersion);