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/EGLSurface.h"
39 #include "Common/Half.hpp"
41 #include <EGL/eglext.h>
47 Context::Context(egl::Display *display, const Context *shareContext, EGLint clientVersion)
48 : egl::Context(display), clientVersion(clientVersion)
50 sw::Context *context = new sw::Context();
51 device = new es2::Device(context);
53 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
55 mState.depthClearValue = 1.0f;
56 mState.stencilClearValue = 0;
58 mState.cullFaceEnabled = false;
59 mState.cullMode = GL_BACK;
60 mState.frontFace = GL_CCW;
61 mState.depthTestEnabled = false;
62 mState.depthFunc = GL_LESS;
63 mState.blendEnabled = false;
64 mState.sourceBlendRGB = GL_ONE;
65 mState.sourceBlendAlpha = GL_ONE;
66 mState.destBlendRGB = GL_ZERO;
67 mState.destBlendAlpha = GL_ZERO;
68 mState.blendEquationRGB = GL_FUNC_ADD;
69 mState.blendEquationAlpha = GL_FUNC_ADD;
70 mState.blendColor.red = 0;
71 mState.blendColor.green = 0;
72 mState.blendColor.blue = 0;
73 mState.blendColor.alpha = 0;
74 mState.stencilTestEnabled = false;
75 mState.stencilFunc = GL_ALWAYS;
76 mState.stencilRef = 0;
77 mState.stencilMask = 0xFFFFFFFFu;
78 mState.stencilWritemask = 0xFFFFFFFFu;
79 mState.stencilBackFunc = GL_ALWAYS;
80 mState.stencilBackRef = 0;
81 mState.stencilBackMask = 0xFFFFFFFFu;
82 mState.stencilBackWritemask = 0xFFFFFFFFu;
83 mState.stencilFail = GL_KEEP;
84 mState.stencilPassDepthFail = GL_KEEP;
85 mState.stencilPassDepthPass = GL_KEEP;
86 mState.stencilBackFail = GL_KEEP;
87 mState.stencilBackPassDepthFail = GL_KEEP;
88 mState.stencilBackPassDepthPass = GL_KEEP;
89 mState.polygonOffsetFillEnabled = false;
90 mState.polygonOffsetFactor = 0.0f;
91 mState.polygonOffsetUnits = 0.0f;
92 mState.sampleAlphaToCoverageEnabled = false;
93 mState.sampleCoverageEnabled = false;
94 mState.sampleCoverageValue = 1.0f;
95 mState.sampleCoverageInvert = false;
96 mState.scissorTestEnabled = false;
97 mState.ditherEnabled = true;
98 mState.primitiveRestartFixedIndexEnabled = false;
99 mState.rasterizerDiscardEnabled = false;
100 mState.generateMipmapHint = GL_DONT_CARE;
101 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
103 mState.lineWidth = 1.0f;
105 mState.viewportX = 0;
106 mState.viewportY = 0;
107 mState.viewportWidth = 0;
108 mState.viewportHeight = 0;
114 mState.scissorWidth = 0;
115 mState.scissorHeight = 0;
117 mState.colorMaskRed = true;
118 mState.colorMaskGreen = true;
119 mState.colorMaskBlue = true;
120 mState.colorMaskAlpha = true;
121 mState.depthMask = true;
125 mResourceManager = shareContext->mResourceManager;
126 mResourceManager->addRef();
130 mResourceManager = new ResourceManager();
133 // [OpenGL ES 2.0.24] section 3.7 page 83:
134 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
135 // and cube map texture state vectors respectively associated with them.
136 // In order that access to these initial textures not be lost, they are treated as texture
137 // objects all of whose names are 0.
139 mTexture2DZero = new Texture2D(0);
140 mTexture3DZero = new Texture3D(0);
141 mTexture2DArrayZero = new Texture2DArray(0);
142 mTextureCubeMapZero = new TextureCubeMap(0);
143 mTextureExternalZero = new TextureExternal(0);
145 mState.activeSampler = 0;
148 bindElementArrayBuffer(0);
149 bindTextureCubeMap(0);
151 bindReadFramebuffer(0);
152 bindDrawFramebuffer(0);
154 bindGenericUniformBuffer(0);
155 bindTransformFeedback(0);
157 mState.currentProgram = 0;
159 mState.packAlignment = 4;
160 mState.unpackInfo.alignment = 4;
161 mState.packRowLength = 0;
162 mState.packImageHeight = 0;
163 mState.packSkipPixels = 0;
164 mState.packSkipRows = 0;
165 mState.packSkipImages = 0;
166 mState.unpackInfo.rowLength = 0;
167 mState.unpackInfo.imageHeight = 0;
168 mState.unpackInfo.skipPixels = 0;
169 mState.unpackInfo.skipRows = 0;
170 mState.unpackInfo.skipImages = 0;
172 mVertexDataManager = nullptr;
173 mIndexDataManager = nullptr;
175 mInvalidEnum = false;
176 mInvalidValue = false;
177 mInvalidOperation = false;
178 mOutOfMemory = false;
179 mInvalidFramebufferOperation = false;
181 mHasBeenCurrent = false;
188 if(mState.currentProgram != 0)
190 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
193 programObject->release();
195 mState.currentProgram = 0;
198 while(!mFramebufferNameSpace.empty())
200 deleteFramebuffer(mFramebufferNameSpace.firstName());
203 while(!mFenceNameSpace.empty())
205 deleteFence(mFenceNameSpace.firstName());
208 while(!mQueryNameSpace.empty())
210 deleteQuery(mQueryNameSpace.firstName());
213 while(!mVertexArrayNameSpace.empty())
215 deleteVertexArray(mVertexArrayNameSpace.lastName());
218 while(!mTransformFeedbackNameSpace.empty())
220 deleteTransformFeedback(mTransformFeedbackNameSpace.firstName());
223 for(int type = 0; type < TEXTURE_TYPE_COUNT; type++)
225 for(int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS; sampler++)
227 mState.samplerTexture[type][sampler] = nullptr;
231 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
233 mState.vertexAttribute[i].mBoundBuffer = nullptr;
236 for(int i = 0; i < QUERY_TYPE_COUNT; i++)
238 mState.activeQuery[i] = nullptr;
241 mState.arrayBuffer = nullptr;
242 mState.copyReadBuffer = nullptr;
243 mState.copyWriteBuffer = nullptr;
244 mState.pixelPackBuffer = nullptr;
245 mState.pixelUnpackBuffer = nullptr;
246 mState.genericUniformBuffer = nullptr;
247 mState.renderbuffer = nullptr;
249 for(int i = 0; i < MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++i)
251 mState.sampler[i] = nullptr;
254 mTexture2DZero = nullptr;
255 mTexture3DZero = nullptr;
256 mTexture2DArrayZero = nullptr;
257 mTextureCubeMapZero = nullptr;
258 mTextureExternalZero = nullptr;
260 delete mVertexDataManager;
261 delete mIndexDataManager;
263 mResourceManager->release();
267 void Context::makeCurrent(egl::Surface *surface)
271 mVertexDataManager = new VertexDataManager(this);
272 mIndexDataManager = new IndexDataManager();
274 mState.viewportX = 0;
275 mState.viewportY = 0;
276 mState.viewportWidth = surface->getWidth();
277 mState.viewportHeight = surface->getHeight();
281 mState.scissorWidth = surface->getWidth();
282 mState.scissorHeight = surface->getHeight();
284 mHasBeenCurrent = true;
287 // Wrap the existing resources into GL objects and assign them to the '0' names
288 egl::Image *defaultRenderTarget = surface->getRenderTarget();
289 egl::Image *depthStencil = surface->getDepthStencil();
291 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
292 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
293 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
295 setFramebufferZero(framebufferZero);
297 if(defaultRenderTarget)
299 defaultRenderTarget->release();
304 depthStencil->release();
310 EGLint Context::getClientVersion() const
312 return clientVersion;
315 // This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
316 void Context::markAllStateDirty()
318 mAppliedProgramSerial = 0;
320 mDepthStateDirty = true;
321 mMaskStateDirty = true;
322 mBlendStateDirty = true;
323 mStencilStateDirty = true;
324 mPolygonOffsetStateDirty = true;
325 mSampleStateDirty = true;
326 mDitherStateDirty = true;
327 mFrontFaceDirty = true;
330 void Context::setClearColor(float red, float green, float blue, float alpha)
332 mState.colorClearValue.red = red;
333 mState.colorClearValue.green = green;
334 mState.colorClearValue.blue = blue;
335 mState.colorClearValue.alpha = alpha;
338 void Context::setClearDepth(float depth)
340 mState.depthClearValue = depth;
343 void Context::setClearStencil(int stencil)
345 mState.stencilClearValue = stencil;
348 void Context::setCullFaceEnabled(bool enabled)
350 mState.cullFaceEnabled = enabled;
353 bool Context::isCullFaceEnabled() const
355 return mState.cullFaceEnabled;
358 void Context::setCullMode(GLenum mode)
360 mState.cullMode = mode;
363 void Context::setFrontFace(GLenum front)
365 if(mState.frontFace != front)
367 mState.frontFace = front;
368 mFrontFaceDirty = true;
372 void Context::setDepthTestEnabled(bool enabled)
374 if(mState.depthTestEnabled != enabled)
376 mState.depthTestEnabled = enabled;
377 mDepthStateDirty = true;
381 bool Context::isDepthTestEnabled() const
383 return mState.depthTestEnabled;
386 void Context::setDepthFunc(GLenum depthFunc)
388 if(mState.depthFunc != depthFunc)
390 mState.depthFunc = depthFunc;
391 mDepthStateDirty = true;
395 void Context::setDepthRange(float zNear, float zFar)
397 mState.zNear = zNear;
401 void Context::setBlendEnabled(bool enabled)
403 if(mState.blendEnabled != enabled)
405 mState.blendEnabled = enabled;
406 mBlendStateDirty = true;
410 bool Context::isBlendEnabled() const
412 return mState.blendEnabled;
415 void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
417 if(mState.sourceBlendRGB != sourceRGB ||
418 mState.sourceBlendAlpha != sourceAlpha ||
419 mState.destBlendRGB != destRGB ||
420 mState.destBlendAlpha != destAlpha)
422 mState.sourceBlendRGB = sourceRGB;
423 mState.destBlendRGB = destRGB;
424 mState.sourceBlendAlpha = sourceAlpha;
425 mState.destBlendAlpha = destAlpha;
426 mBlendStateDirty = true;
430 void Context::setBlendColor(float red, float green, float blue, float alpha)
432 if(mState.blendColor.red != red ||
433 mState.blendColor.green != green ||
434 mState.blendColor.blue != blue ||
435 mState.blendColor.alpha != alpha)
437 mState.blendColor.red = red;
438 mState.blendColor.green = green;
439 mState.blendColor.blue = blue;
440 mState.blendColor.alpha = alpha;
441 mBlendStateDirty = true;
445 void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
447 if(mState.blendEquationRGB != rgbEquation ||
448 mState.blendEquationAlpha != alphaEquation)
450 mState.blendEquationRGB = rgbEquation;
451 mState.blendEquationAlpha = alphaEquation;
452 mBlendStateDirty = true;
456 void Context::setStencilTestEnabled(bool enabled)
458 if(mState.stencilTestEnabled != enabled)
460 mState.stencilTestEnabled = enabled;
461 mStencilStateDirty = true;
465 bool Context::isStencilTestEnabled() const
467 return mState.stencilTestEnabled;
470 void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
472 if(mState.stencilFunc != stencilFunc ||
473 mState.stencilRef != stencilRef ||
474 mState.stencilMask != stencilMask)
476 mState.stencilFunc = stencilFunc;
477 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
478 mState.stencilMask = stencilMask;
479 mStencilStateDirty = true;
483 void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
485 if(mState.stencilBackFunc != stencilBackFunc ||
486 mState.stencilBackRef != stencilBackRef ||
487 mState.stencilBackMask != stencilBackMask)
489 mState.stencilBackFunc = stencilBackFunc;
490 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
491 mState.stencilBackMask = stencilBackMask;
492 mStencilStateDirty = true;
496 void Context::setStencilWritemask(GLuint stencilWritemask)
498 if(mState.stencilWritemask != stencilWritemask)
500 mState.stencilWritemask = stencilWritemask;
501 mStencilStateDirty = true;
505 void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
507 if(mState.stencilBackWritemask != stencilBackWritemask)
509 mState.stencilBackWritemask = stencilBackWritemask;
510 mStencilStateDirty = true;
514 void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
516 if(mState.stencilFail != stencilFail ||
517 mState.stencilPassDepthFail != stencilPassDepthFail ||
518 mState.stencilPassDepthPass != stencilPassDepthPass)
520 mState.stencilFail = stencilFail;
521 mState.stencilPassDepthFail = stencilPassDepthFail;
522 mState.stencilPassDepthPass = stencilPassDepthPass;
523 mStencilStateDirty = true;
527 void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
529 if(mState.stencilBackFail != stencilBackFail ||
530 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
531 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
533 mState.stencilBackFail = stencilBackFail;
534 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
535 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
536 mStencilStateDirty = true;
540 void Context::setPolygonOffsetFillEnabled(bool enabled)
542 if(mState.polygonOffsetFillEnabled != enabled)
544 mState.polygonOffsetFillEnabled = enabled;
545 mPolygonOffsetStateDirty = true;
549 bool Context::isPolygonOffsetFillEnabled() const
551 return mState.polygonOffsetFillEnabled;
554 void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
556 if(mState.polygonOffsetFactor != factor ||
557 mState.polygonOffsetUnits != units)
559 mState.polygonOffsetFactor = factor;
560 mState.polygonOffsetUnits = units;
561 mPolygonOffsetStateDirty = true;
565 void Context::setSampleAlphaToCoverageEnabled(bool enabled)
567 if(mState.sampleAlphaToCoverageEnabled != enabled)
569 mState.sampleAlphaToCoverageEnabled = enabled;
570 mSampleStateDirty = true;
574 bool Context::isSampleAlphaToCoverageEnabled() const
576 return mState.sampleAlphaToCoverageEnabled;
579 void Context::setSampleCoverageEnabled(bool enabled)
581 if(mState.sampleCoverageEnabled != enabled)
583 mState.sampleCoverageEnabled = enabled;
584 mSampleStateDirty = true;
588 bool Context::isSampleCoverageEnabled() const
590 return mState.sampleCoverageEnabled;
593 void Context::setSampleCoverageParams(GLclampf value, bool invert)
595 if(mState.sampleCoverageValue != value ||
596 mState.sampleCoverageInvert != invert)
598 mState.sampleCoverageValue = value;
599 mState.sampleCoverageInvert = invert;
600 mSampleStateDirty = true;
604 void Context::setScissorTestEnabled(bool enabled)
606 mState.scissorTestEnabled = enabled;
609 bool Context::isScissorTestEnabled() const
611 return mState.scissorTestEnabled;
614 void Context::setDitherEnabled(bool enabled)
616 if(mState.ditherEnabled != enabled)
618 mState.ditherEnabled = enabled;
619 mDitherStateDirty = true;
623 bool Context::isDitherEnabled() const
625 return mState.ditherEnabled;
628 void Context::setPrimitiveRestartFixedIndexEnabled(bool enabled)
630 mState.primitiveRestartFixedIndexEnabled = enabled;
633 bool Context::isPrimitiveRestartFixedIndexEnabled() const
635 return mState.primitiveRestartFixedIndexEnabled;
638 void Context::setRasterizerDiscardEnabled(bool enabled)
640 mState.rasterizerDiscardEnabled = enabled;
643 bool Context::isRasterizerDiscardEnabled() const
645 return mState.rasterizerDiscardEnabled;
648 void Context::setLineWidth(GLfloat width)
650 mState.lineWidth = width;
651 device->setLineWidth(clamp(width, ALIASED_LINE_WIDTH_RANGE_MIN, ALIASED_LINE_WIDTH_RANGE_MAX));
654 void Context::setGenerateMipmapHint(GLenum hint)
656 mState.generateMipmapHint = hint;
659 void Context::setFragmentShaderDerivativeHint(GLenum hint)
661 mState.fragmentShaderDerivativeHint = hint;
662 // TODO: Propagate the hint to shader translator so we can write
663 // ddx, ddx_coarse, or ddx_fine depending on the hint.
664 // Ignore for now. It is valid for implementations to ignore hint.
667 void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
669 mState.viewportX = x;
670 mState.viewportY = y;
671 mState.viewportWidth = std::min<GLsizei>(width, IMPLEMENTATION_MAX_RENDERBUFFER_SIZE); // GL_MAX_VIEWPORT_DIMS[0]
672 mState.viewportHeight = std::min<GLsizei>(height, IMPLEMENTATION_MAX_RENDERBUFFER_SIZE); // GL_MAX_VIEWPORT_DIMS[1]
675 void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
679 mState.scissorWidth = width;
680 mState.scissorHeight = height;
683 void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
685 if(mState.colorMaskRed != red || mState.colorMaskGreen != green ||
686 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
688 mState.colorMaskRed = red;
689 mState.colorMaskGreen = green;
690 mState.colorMaskBlue = blue;
691 mState.colorMaskAlpha = alpha;
692 mMaskStateDirty = true;
696 unsigned int Context::getColorMask() const
698 return (mState.colorMaskRed ? 0x1 : 0) |
699 (mState.colorMaskGreen ? 0x2 : 0) |
700 (mState.colorMaskBlue ? 0x4 : 0) |
701 (mState.colorMaskAlpha ? 0x8 : 0);
704 void Context::setDepthMask(bool mask)
706 if(mState.depthMask != mask)
708 mState.depthMask = mask;
709 mMaskStateDirty = true;
713 void Context::setActiveSampler(unsigned int active)
715 mState.activeSampler = active;
718 GLuint Context::getReadFramebufferName() const
720 return mState.readFramebuffer;
723 GLuint Context::getDrawFramebufferName() const
725 return mState.drawFramebuffer;
728 GLuint Context::getRenderbufferName() const
730 return mState.renderbuffer.name();
733 void Context::setFramebufferReadBuffer(GLuint buf)
735 getReadFramebuffer()->setReadBuffer(buf);
738 void Context::setFramebufferDrawBuffers(GLsizei n, const GLenum *bufs)
740 Framebuffer *drawFramebuffer = getDrawFramebuffer();
742 for(int i = 0; i < MAX_COLOR_ATTACHMENTS; i++)
744 drawFramebuffer->setDrawBuffer(i, (i < n) ? bufs[i] : GL_NONE);
748 GLuint Context::getReadFramebufferColorIndex() const
750 GLenum buf = getReadFramebuffer()->getReadBuffer();
756 return GL_INVALID_INDEX;
758 return buf - GL_COLOR_ATTACHMENT0;
762 GLuint Context::getArrayBufferName() const
764 return mState.arrayBuffer.name();
767 GLuint Context::getElementArrayBufferName() const
769 Buffer* elementArrayBuffer = getCurrentVertexArray()->getElementArrayBuffer();
770 return elementArrayBuffer ? elementArrayBuffer->name : 0;
773 GLuint Context::getActiveQuery(GLenum target) const
775 Query *queryObject = nullptr;
779 case GL_ANY_SAMPLES_PASSED_EXT:
780 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED];
782 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
783 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE];
785 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
786 queryObject = mState.activeQuery[QUERY_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN];
794 return queryObject->name;
800 void Context::setVertexAttribArrayEnabled(unsigned int attribNum, bool enabled)
802 getCurrentVertexArray()->enableAttribute(attribNum, enabled);
805 void Context::setVertexAttribDivisor(unsigned int attribNum, GLuint divisor)
807 getCurrentVertexArray()->setVertexAttribDivisor(attribNum, divisor);
810 const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum) const
812 return getCurrentVertexArray()->getVertexAttribute(attribNum);
815 void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
816 GLsizei stride, const void *pointer)
818 getCurrentVertexArray()->setAttributeState(attribNum, boundBuffer, size, type, normalized, stride, pointer);
821 const void *Context::getVertexAttribPointer(unsigned int attribNum) const
823 return getCurrentVertexArray()->getVertexAttribute(attribNum).mPointer;
826 const VertexAttributeArray &Context::getVertexArrayAttributes()
828 return getCurrentVertexArray()->getVertexAttributes();
831 const VertexAttributeArray &Context::getCurrentVertexAttributes()
833 return mState.vertexAttribute;
836 void Context::setPackAlignment(GLint alignment)
838 mState.packAlignment = alignment;
841 void Context::setUnpackAlignment(GLint alignment)
843 mState.unpackInfo.alignment = alignment;
846 const egl::Image::UnpackInfo& Context::getUnpackInfo() const
848 return mState.unpackInfo;
851 void Context::setPackRowLength(GLint rowLength)
853 mState.packRowLength = rowLength;
856 void Context::setPackImageHeight(GLint imageHeight)
858 mState.packImageHeight = imageHeight;
861 void Context::setPackSkipPixels(GLint skipPixels)
863 mState.packSkipPixels = skipPixels;
866 void Context::setPackSkipRows(GLint skipRows)
868 mState.packSkipRows = skipRows;
871 void Context::setPackSkipImages(GLint skipImages)
873 mState.packSkipImages = skipImages;
876 void Context::setUnpackRowLength(GLint rowLength)
878 mState.unpackInfo.rowLength = rowLength;
881 void Context::setUnpackImageHeight(GLint imageHeight)
883 mState.unpackInfo.imageHeight = imageHeight;
886 void Context::setUnpackSkipPixels(GLint skipPixels)
888 mState.unpackInfo.skipPixels = skipPixels;
891 void Context::setUnpackSkipRows(GLint skipRows)
893 mState.unpackInfo.skipRows = skipRows;
896 void Context::setUnpackSkipImages(GLint skipImages)
898 mState.unpackInfo.skipImages = skipImages;
901 GLuint Context::createBuffer()
903 return mResourceManager->createBuffer();
906 GLuint Context::createProgram()
908 return mResourceManager->createProgram();
911 GLuint Context::createShader(GLenum type)
913 return mResourceManager->createShader(type);
916 GLuint Context::createTexture()
918 return mResourceManager->createTexture();
921 GLuint Context::createRenderbuffer()
923 return mResourceManager->createRenderbuffer();
926 // Returns an unused framebuffer name
927 GLuint Context::createFramebuffer()
929 return mFramebufferNameSpace.allocate();
932 GLuint Context::createFence()
934 return mFenceNameSpace.allocate(new Fence());
937 // Returns an unused query name
938 GLuint Context::createQuery()
940 return mQueryNameSpace.allocate();
943 // Returns an unused vertex array name
944 GLuint Context::createVertexArray()
946 return mVertexArrayNameSpace.allocate();
949 GLsync Context::createFenceSync(GLenum condition, GLbitfield flags)
951 GLuint handle = mResourceManager->createFenceSync(condition, flags);
953 return reinterpret_cast<GLsync>(static_cast<uintptr_t>(handle));
956 // Returns an unused transform feedback name
957 GLuint Context::createTransformFeedback()
959 return mTransformFeedbackNameSpace.allocate();
962 // Returns an unused sampler name
963 GLuint Context::createSampler()
965 return mResourceManager->createSampler();
968 void Context::deleteBuffer(GLuint buffer)
970 detachBuffer(buffer);
972 mResourceManager->deleteBuffer(buffer);
975 void Context::deleteShader(GLuint shader)
977 mResourceManager->deleteShader(shader);
980 void Context::deleteProgram(GLuint program)
982 mResourceManager->deleteProgram(program);
985 void Context::deleteTexture(GLuint texture)
987 detachTexture(texture);
989 mResourceManager->deleteTexture(texture);
992 void Context::deleteRenderbuffer(GLuint renderbuffer)
994 if(mResourceManager->getRenderbuffer(renderbuffer))
996 detachRenderbuffer(renderbuffer);
999 mResourceManager->deleteRenderbuffer(renderbuffer);
1002 void Context::deleteFramebuffer(GLuint framebuffer)
1004 detachFramebuffer(framebuffer);
1006 Framebuffer *framebufferObject = mFramebufferNameSpace.remove(framebuffer);
1008 if(framebufferObject)
1010 delete framebufferObject;
1014 void Context::deleteFence(GLuint fence)
1016 Fence *fenceObject = mFenceNameSpace.remove(fence);
1024 void Context::deleteQuery(GLuint query)
1026 Query *queryObject = mQueryNameSpace.remove(query);
1030 queryObject->release();
1034 void Context::deleteVertexArray(GLuint vertexArray)
1036 // [OpenGL ES 3.0.2] section 2.10 page 43:
1037 // If a vertex array object that is currently bound is deleted, the binding
1038 // for that object reverts to zero and the default vertex array becomes current.
1039 if(getCurrentVertexArray()->name == vertexArray)
1044 VertexArray *vertexArrayObject = mVertexArrayNameSpace.remove(vertexArray);
1046 if(vertexArrayObject)
1048 delete vertexArrayObject;
1052 void Context::deleteFenceSync(GLsync fenceSync)
1054 // The spec specifies the underlying Fence object is not deleted until all current
1055 // wait commands finish. However, since the name becomes invalid, we cannot query the fence,
1056 // and since our API is currently designed for being called from a single thread, we can delete
1057 // the fence immediately.
1058 mResourceManager->deleteFenceSync(static_cast<GLuint>(reinterpret_cast<uintptr_t>(fenceSync)));
1061 void Context::deleteTransformFeedback(GLuint transformFeedback)
1063 TransformFeedback *transformFeedbackObject = mTransformFeedbackNameSpace.remove(transformFeedback);
1065 if(transformFeedbackObject)
1067 delete transformFeedbackObject;
1071 void Context::deleteSampler(GLuint sampler)
1073 detachSampler(sampler);
1075 mResourceManager->deleteSampler(sampler);
1078 Buffer *Context::getBuffer(GLuint handle) const
1080 return mResourceManager->getBuffer(handle);
1083 Shader *Context::getShader(GLuint handle) const
1085 return mResourceManager->getShader(handle);
1088 Program *Context::getProgram(GLuint handle) const
1090 return mResourceManager->getProgram(handle);
1093 Texture *Context::getTexture(GLuint handle) const
1095 return mResourceManager->getTexture(handle);
1098 Renderbuffer *Context::getRenderbuffer(GLuint handle) const
1100 return mResourceManager->getRenderbuffer(handle);
1103 Framebuffer *Context::getReadFramebuffer() const
1105 return getFramebuffer(mState.readFramebuffer);
1108 Framebuffer *Context::getDrawFramebuffer() const
1110 return getFramebuffer(mState.drawFramebuffer);
1113 void Context::bindArrayBuffer(unsigned int buffer)
1115 mResourceManager->checkBufferAllocation(buffer);
1117 mState.arrayBuffer = getBuffer(buffer);
1120 void Context::bindElementArrayBuffer(unsigned int buffer)
1122 mResourceManager->checkBufferAllocation(buffer);
1124 getCurrentVertexArray()->setElementArrayBuffer(getBuffer(buffer));
1127 void Context::bindCopyReadBuffer(GLuint buffer)
1129 mResourceManager->checkBufferAllocation(buffer);
1131 mState.copyReadBuffer = getBuffer(buffer);
1134 void Context::bindCopyWriteBuffer(GLuint buffer)
1136 mResourceManager->checkBufferAllocation(buffer);
1138 mState.copyWriteBuffer = getBuffer(buffer);
1141 void Context::bindPixelPackBuffer(GLuint buffer)
1143 mResourceManager->checkBufferAllocation(buffer);
1145 mState.pixelPackBuffer = getBuffer(buffer);
1148 void Context::bindPixelUnpackBuffer(GLuint buffer)
1150 mResourceManager->checkBufferAllocation(buffer);
1152 mState.pixelUnpackBuffer = getBuffer(buffer);
1155 void Context::bindTransformFeedbackBuffer(GLuint buffer)
1157 mResourceManager->checkBufferAllocation(buffer);
1159 TransformFeedback* transformFeedback = getTransformFeedback(mState.transformFeedback);
1161 if(transformFeedback)
1163 transformFeedback->setGenericBuffer(getBuffer(buffer));
1167 void Context::bindTexture2D(GLuint texture)
1169 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
1171 mState.samplerTexture[TEXTURE_2D][mState.activeSampler] = getTexture(texture);
1174 void Context::bindTextureCubeMap(GLuint texture)
1176 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
1178 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler] = getTexture(texture);
1181 void Context::bindTextureExternal(GLuint texture)
1183 mResourceManager->checkTextureAllocation(texture, TEXTURE_EXTERNAL);
1185 mState.samplerTexture[TEXTURE_EXTERNAL][mState.activeSampler] = getTexture(texture);
1188 void Context::bindTexture3D(GLuint texture)
1190 mResourceManager->checkTextureAllocation(texture, TEXTURE_3D);
1192 mState.samplerTexture[TEXTURE_3D][mState.activeSampler] = getTexture(texture);
1195 void Context::bindTexture2DArray(GLuint texture)
1197 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D_ARRAY);
1199 mState.samplerTexture[TEXTURE_2D_ARRAY][mState.activeSampler] = getTexture(texture);
1202 void Context::bindReadFramebuffer(GLuint framebuffer)
1204 if(!getFramebuffer(framebuffer))
1206 mFramebufferNameSpace.insert(framebuffer, new Framebuffer());
1209 mState.readFramebuffer = framebuffer;
1212 void Context::bindDrawFramebuffer(GLuint framebuffer)
1214 if(!getFramebuffer(framebuffer))
1216 mFramebufferNameSpace.insert(framebuffer, new Framebuffer());
1219 mState.drawFramebuffer = framebuffer;
1222 void Context::bindRenderbuffer(GLuint renderbuffer)
1224 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1226 mState.renderbuffer = getRenderbuffer(renderbuffer);
1229 void Context::bindVertexArray(GLuint array)
1231 VertexArray *vertexArray = getVertexArray(array);
1235 vertexArray = new VertexArray(array);
1236 mVertexArrayNameSpace.insert(array, vertexArray);
1239 mState.vertexArray = array;
1242 void Context::bindGenericUniformBuffer(GLuint buffer)
1244 mResourceManager->checkBufferAllocation(buffer);
1246 mState.genericUniformBuffer = getBuffer(buffer);
1249 void Context::bindIndexedUniformBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size)
1251 mResourceManager->checkBufferAllocation(buffer);
1253 Buffer* bufferObject = getBuffer(buffer);
1254 mState.uniformBuffers[index].set(bufferObject, static_cast<int>(offset), static_cast<int>(size));
1257 void Context::bindGenericTransformFeedbackBuffer(GLuint buffer)
1259 mResourceManager->checkBufferAllocation(buffer);
1261 getTransformFeedback()->setGenericBuffer(getBuffer(buffer));
1264 void Context::bindIndexedTransformFeedbackBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size)
1266 mResourceManager->checkBufferAllocation(buffer);
1268 Buffer* bufferObject = getBuffer(buffer);
1269 getTransformFeedback()->setBuffer(index, bufferObject, offset, size);
1272 void Context::bindTransformFeedback(GLuint id)
1274 if(!getTransformFeedback(id))
1276 mTransformFeedbackNameSpace.insert(id, new TransformFeedback(id));
1279 mState.transformFeedback = id;
1282 bool Context::bindSampler(GLuint unit, GLuint sampler)
1284 mResourceManager->checkSamplerAllocation(sampler);
1286 Sampler* samplerObject = getSampler(sampler);
1288 mState.sampler[unit] = samplerObject;
1290 return !!samplerObject;
1293 void Context::useProgram(GLuint program)
1295 GLuint priorProgram = mState.currentProgram;
1296 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
1298 if(priorProgram != program)
1300 Program *newProgram = mResourceManager->getProgram(program);
1301 Program *oldProgram = mResourceManager->getProgram(priorProgram);
1305 newProgram->addRef();
1310 oldProgram->release();
1315 void Context::beginQuery(GLenum target, GLuint query)
1317 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1318 // of zero, if the active query object name for <target> is non-zero (for the
1319 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1320 // the active query for either target is non-zero), if <id> is the name of an
1321 // existing query object whose type does not match <target>, or if <id> is the
1322 // active query object name for any query type, the error INVALID_OPERATION is
1325 // Ensure no other queries are active
1326 // NOTE: If other queries than occlusion are supported, we will need to check
1328 // a) The query ID passed is not the current active query for any target/type
1329 // b) There are no active queries for the requested target (and in the case
1330 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1331 // no query may be active for either if glBeginQuery targets either.
1332 for(int i = 0; i < QUERY_TYPE_COUNT; i++)
1334 if(mState.activeQuery[i])
1336 return error(GL_INVALID_OPERATION);
1343 case GL_ANY_SAMPLES_PASSED_EXT:
1344 qType = QUERY_ANY_SAMPLES_PASSED;
1346 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1347 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1349 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN:
1350 qType = QUERY_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN;
1353 UNREACHABLE(target);
1354 return error(GL_INVALID_ENUM);
1357 Query *queryObject = createQuery(query, target);
1359 // Check that name was obtained with glGenQueries
1362 return error(GL_INVALID_OPERATION);
1365 // Check for type mismatch
1366 if(queryObject->getType() != target)
1368 return error(GL_INVALID_OPERATION);
1371 // Set query as active for specified target
1372 mState.activeQuery[qType] = queryObject;
1375 queryObject->begin();
1378 void Context::endQuery(GLenum target)
1384 case GL_ANY_SAMPLES_PASSED_EXT: qType = QUERY_ANY_SAMPLES_PASSED; break;
1385 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT: qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE; break;
1386 case GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN: qType = QUERY_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN; break;
1387 default: UNREACHABLE(target); return;
1390 Query *queryObject = mState.activeQuery[qType];
1394 return error(GL_INVALID_OPERATION);
1399 mState.activeQuery[qType] = nullptr;
1402 void Context::setFramebufferZero(Framebuffer *buffer)
1404 delete mFramebufferNameSpace.remove(0);
1405 mFramebufferNameSpace.insert(0, buffer);
1408 void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
1410 Renderbuffer *renderbufferObject = mState.renderbuffer;
1411 renderbufferObject->setStorage(renderbuffer);
1414 Framebuffer *Context::getFramebuffer(unsigned int handle) const
1416 return mFramebufferNameSpace.find(handle);
1419 Fence *Context::getFence(unsigned int handle) const
1421 return mFenceNameSpace.find(handle);
1424 FenceSync *Context::getFenceSync(GLsync handle) const
1426 return mResourceManager->getFenceSync(static_cast<GLuint>(reinterpret_cast<uintptr_t>(handle)));
1429 Query *Context::getQuery(unsigned int handle) const
1431 return mQueryNameSpace.find(handle);
1434 Query *Context::createQuery(unsigned int handle, GLenum type)
1436 if(!mQueryNameSpace.isReserved(handle))
1442 Query *query = mQueryNameSpace.find(handle);
1445 query = new Query(handle, type);
1447 mQueryNameSpace.insert(handle, query);
1454 VertexArray *Context::getVertexArray(GLuint array) const
1456 return mVertexArrayNameSpace.find(array);
1459 VertexArray *Context::getCurrentVertexArray() const
1461 return getVertexArray(mState.vertexArray);
1464 bool Context::isVertexArray(GLuint array) const
1466 return mVertexArrayNameSpace.isReserved(array);
1469 bool Context::hasZeroDivisor() const
1471 // Verify there is at least one active attribute with a divisor of zero
1472 es2::Program *programObject = getCurrentProgram();
1473 for(int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
1475 bool active = (programObject->getAttributeStream(attributeIndex) != -1);
1476 if(active && getCurrentVertexArray()->getVertexAttribute(attributeIndex).mDivisor == 0)
1485 TransformFeedback *Context::getTransformFeedback(GLuint transformFeedback) const
1487 return mTransformFeedbackNameSpace.find(transformFeedback);
1490 Sampler *Context::getSampler(GLuint sampler) const
1492 return mResourceManager->getSampler(sampler);
1495 bool Context::isSampler(GLuint sampler) const
1497 return mResourceManager->isSampler(sampler);
1500 Buffer *Context::getArrayBuffer() const
1502 return mState.arrayBuffer;
1505 Buffer *Context::getElementArrayBuffer() const
1507 return getCurrentVertexArray()->getElementArrayBuffer();
1510 Buffer *Context::getCopyReadBuffer() const
1512 return mState.copyReadBuffer;
1515 Buffer *Context::getCopyWriteBuffer() const
1517 return mState.copyWriteBuffer;
1520 Buffer *Context::getPixelPackBuffer() const
1522 return mState.pixelPackBuffer;
1525 Buffer *Context::getPixelUnpackBuffer() const
1527 return mState.pixelUnpackBuffer;
1530 Buffer *Context::getGenericUniformBuffer() const
1532 return mState.genericUniformBuffer;
1535 const GLvoid* Context::getPixels(const GLvoid* data) const
1537 es2::Buffer* unpackBuffer = getPixelUnpackBuffer();
1538 const unsigned char* unpackBufferData = unpackBuffer ? static_cast<const unsigned char*>(unpackBuffer->data()) : nullptr;
1539 return unpackBufferData ? unpackBufferData + (ptrdiff_t)(data) : data;
1542 bool Context::getBuffer(GLenum target, es2::Buffer **buffer) const
1546 case GL_ARRAY_BUFFER:
1547 *buffer = getArrayBuffer();
1549 case GL_ELEMENT_ARRAY_BUFFER:
1550 *buffer = getElementArrayBuffer();
1552 case GL_COPY_READ_BUFFER:
1553 if(clientVersion >= 3)
1555 *buffer = getCopyReadBuffer();
1559 case GL_COPY_WRITE_BUFFER:
1560 if(clientVersion >= 3)
1562 *buffer = getCopyWriteBuffer();
1566 case GL_PIXEL_PACK_BUFFER:
1567 if(clientVersion >= 3)
1569 *buffer = getPixelPackBuffer();
1573 case GL_PIXEL_UNPACK_BUFFER:
1574 if(clientVersion >= 3)
1576 *buffer = getPixelUnpackBuffer();
1580 case GL_TRANSFORM_FEEDBACK_BUFFER:
1581 if(clientVersion >= 3)
1583 TransformFeedback* transformFeedback = getTransformFeedback();
1584 *buffer = transformFeedback ? static_cast<es2::Buffer*>(transformFeedback->getGenericBuffer()) : nullptr;
1588 case GL_UNIFORM_BUFFER:
1589 if(clientVersion >= 3)
1591 *buffer = getGenericUniformBuffer();
1601 TransformFeedback *Context::getTransformFeedback() const
1603 return getTransformFeedback(mState.transformFeedback);
1606 Program *Context::getCurrentProgram() const
1608 return mResourceManager->getProgram(mState.currentProgram);
1611 Texture2D *Context::getTexture2D() const
1613 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
1616 Texture3D *Context::getTexture3D() const
1618 return static_cast<Texture3D*>(getSamplerTexture(mState.activeSampler, TEXTURE_3D));
1621 Texture2DArray *Context::getTexture2DArray() const
1623 return static_cast<Texture2DArray*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D_ARRAY));
1626 TextureCubeMap *Context::getTextureCubeMap() const
1628 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
1631 TextureExternal *Context::getTextureExternal() const
1633 return static_cast<TextureExternal*>(getSamplerTexture(mState.activeSampler, TEXTURE_EXTERNAL));
1636 Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type) const
1638 GLuint texid = mState.samplerTexture[type][sampler].name();
1640 if(texid == 0) // Special case: 0 refers to different initial textures based on the target
1644 case TEXTURE_2D: return mTexture2DZero;
1645 case TEXTURE_3D: return mTexture3DZero;
1646 case TEXTURE_2D_ARRAY: return mTexture2DArrayZero;
1647 case TEXTURE_CUBE: return mTextureCubeMapZero;
1648 case TEXTURE_EXTERNAL: return mTextureExternalZero;
1649 default: UNREACHABLE(type);
1653 return mState.samplerTexture[type][sampler];
1656 void Context::samplerParameteri(GLuint sampler, GLenum pname, GLint param)
1658 mResourceManager->checkSamplerAllocation(sampler);
1660 Sampler *samplerObject = getSampler(sampler);
1661 ASSERT(samplerObject);
1665 case GL_TEXTURE_MIN_FILTER: samplerObject->setMinFilter(static_cast<GLenum>(param)); break;
1666 case GL_TEXTURE_MAG_FILTER: samplerObject->setMagFilter(static_cast<GLenum>(param)); break;
1667 case GL_TEXTURE_WRAP_S: samplerObject->setWrapS(static_cast<GLenum>(param)); break;
1668 case GL_TEXTURE_WRAP_T: samplerObject->setWrapT(static_cast<GLenum>(param)); break;
1669 case GL_TEXTURE_WRAP_R: samplerObject->setWrapR(static_cast<GLenum>(param)); break;
1670 case GL_TEXTURE_MIN_LOD: samplerObject->setMinLod(static_cast<GLfloat>(param)); break;
1671 case GL_TEXTURE_MAX_LOD: samplerObject->setMaxLod(static_cast<GLfloat>(param)); break;
1672 case GL_TEXTURE_COMPARE_MODE: samplerObject->setComparisonMode(static_cast<GLenum>(param)); break;
1673 case GL_TEXTURE_COMPARE_FUNC: samplerObject->setComparisonFunc(static_cast<GLenum>(param)); break;
1674 default: UNREACHABLE(pname); break;
1678 void Context::samplerParameterf(GLuint sampler, GLenum pname, GLfloat param)
1680 mResourceManager->checkSamplerAllocation(sampler);
1682 Sampler *samplerObject = getSampler(sampler);
1683 ASSERT(samplerObject);
1687 case GL_TEXTURE_MIN_FILTER: samplerObject->setMinFilter(static_cast<GLenum>(roundf(param))); break;
1688 case GL_TEXTURE_MAG_FILTER: samplerObject->setMagFilter(static_cast<GLenum>(roundf(param))); break;
1689 case GL_TEXTURE_WRAP_S: samplerObject->setWrapS(static_cast<GLenum>(roundf(param))); break;
1690 case GL_TEXTURE_WRAP_T: samplerObject->setWrapT(static_cast<GLenum>(roundf(param))); break;
1691 case GL_TEXTURE_WRAP_R: samplerObject->setWrapR(static_cast<GLenum>(roundf(param))); break;
1692 case GL_TEXTURE_MIN_LOD: samplerObject->setMinLod(param); break;
1693 case GL_TEXTURE_MAX_LOD: samplerObject->setMaxLod(param); break;
1694 case GL_TEXTURE_COMPARE_MODE: samplerObject->setComparisonMode(static_cast<GLenum>(roundf(param))); break;
1695 case GL_TEXTURE_COMPARE_FUNC: samplerObject->setComparisonFunc(static_cast<GLenum>(roundf(param))); break;
1696 default: UNREACHABLE(pname); break;
1700 GLint Context::getSamplerParameteri(GLuint sampler, GLenum pname)
1702 mResourceManager->checkSamplerAllocation(sampler);
1704 Sampler *samplerObject = getSampler(sampler);
1705 ASSERT(samplerObject);
1709 case GL_TEXTURE_MIN_FILTER: return static_cast<GLint>(samplerObject->getMinFilter());
1710 case GL_TEXTURE_MAG_FILTER: return static_cast<GLint>(samplerObject->getMagFilter());
1711 case GL_TEXTURE_WRAP_S: return static_cast<GLint>(samplerObject->getWrapS());
1712 case GL_TEXTURE_WRAP_T: return static_cast<GLint>(samplerObject->getWrapT());
1713 case GL_TEXTURE_WRAP_R: return static_cast<GLint>(samplerObject->getWrapR());
1714 case GL_TEXTURE_MIN_LOD: return static_cast<GLint>(roundf(samplerObject->getMinLod()));
1715 case GL_TEXTURE_MAX_LOD: return static_cast<GLint>(roundf(samplerObject->getMaxLod()));
1716 case GL_TEXTURE_COMPARE_MODE: return static_cast<GLint>(samplerObject->getComparisonMode());
1717 case GL_TEXTURE_COMPARE_FUNC: return static_cast<GLint>(samplerObject->getComparisonFunc());
1718 default: UNREACHABLE(pname); return 0;
1722 GLfloat Context::getSamplerParameterf(GLuint sampler, GLenum pname)
1724 mResourceManager->checkSamplerAllocation(sampler);
1726 Sampler *samplerObject = getSampler(sampler);
1727 ASSERT(samplerObject);
1731 case GL_TEXTURE_MIN_FILTER: return static_cast<GLfloat>(samplerObject->getMinFilter());
1732 case GL_TEXTURE_MAG_FILTER: return static_cast<GLfloat>(samplerObject->getMagFilter());
1733 case GL_TEXTURE_WRAP_S: return static_cast<GLfloat>(samplerObject->getWrapS());
1734 case GL_TEXTURE_WRAP_T: return static_cast<GLfloat>(samplerObject->getWrapT());
1735 case GL_TEXTURE_WRAP_R: return static_cast<GLfloat>(samplerObject->getWrapR());
1736 case GL_TEXTURE_MIN_LOD: return samplerObject->getMinLod();
1737 case GL_TEXTURE_MAX_LOD: return samplerObject->getMaxLod();
1738 case GL_TEXTURE_COMPARE_MODE: return static_cast<GLfloat>(samplerObject->getComparisonMode());
1739 case GL_TEXTURE_COMPARE_FUNC: return static_cast<GLfloat>(samplerObject->getComparisonFunc());
1740 default: UNREACHABLE(pname); return 0;
1744 bool Context::getBooleanv(GLenum pname, GLboolean *params) const
1748 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1749 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1750 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
1751 case GL_COLOR_WRITEMASK:
1752 params[0] = mState.colorMaskRed;
1753 params[1] = mState.colorMaskGreen;
1754 params[2] = mState.colorMaskBlue;
1755 params[3] = mState.colorMaskAlpha;
1757 case GL_CULL_FACE: *params = mState.cullFaceEnabled; break;
1758 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFillEnabled; break;
1759 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverageEnabled; break;
1760 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverageEnabled; break;
1761 case GL_SCISSOR_TEST: *params = mState.scissorTestEnabled; break;
1762 case GL_STENCIL_TEST: *params = mState.stencilTestEnabled; break;
1763 case GL_DEPTH_TEST: *params = mState.depthTestEnabled; break;
1764 case GL_BLEND: *params = mState.blendEnabled; break;
1765 case GL_DITHER: *params = mState.ditherEnabled; break;
1766 case GL_PRIMITIVE_RESTART_FIXED_INDEX: *params = mState.primitiveRestartFixedIndexEnabled; break;
1767 case GL_RASTERIZER_DISCARD: *params = mState.rasterizerDiscardEnabled; break;
1768 case GL_TRANSFORM_FEEDBACK_ACTIVE:
1770 TransformFeedback* transformFeedback = getTransformFeedback(mState.transformFeedback);
1771 if(transformFeedback)
1773 *params = transformFeedback->isActive();
1778 case GL_TRANSFORM_FEEDBACK_PAUSED:
1780 TransformFeedback* transformFeedback = getTransformFeedback(mState.transformFeedback);
1781 if(transformFeedback)
1783 *params = transformFeedback->isPaused();
1795 bool Context::getFloatv(GLenum pname, GLfloat *params) const
1797 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1798 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1799 // GetIntegerv as its native query function. As it would require conversion in any
1800 // case, this should make no difference to the calling application.
1803 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1804 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1805 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1806 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1807 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
1808 case GL_ALIASED_LINE_WIDTH_RANGE:
1809 params[0] = ALIASED_LINE_WIDTH_RANGE_MIN;
1810 params[1] = ALIASED_LINE_WIDTH_RANGE_MAX;
1812 case GL_ALIASED_POINT_SIZE_RANGE:
1813 params[0] = ALIASED_POINT_SIZE_RANGE_MIN;
1814 params[1] = ALIASED_POINT_SIZE_RANGE_MAX;
1816 case GL_DEPTH_RANGE:
1817 params[0] = mState.zNear;
1818 params[1] = mState.zFar;
1820 case GL_COLOR_CLEAR_VALUE:
1821 params[0] = mState.colorClearValue.red;
1822 params[1] = mState.colorClearValue.green;
1823 params[2] = mState.colorClearValue.blue;
1824 params[3] = mState.colorClearValue.alpha;
1826 case GL_BLEND_COLOR:
1827 params[0] = mState.blendColor.red;
1828 params[1] = mState.blendColor.green;
1829 params[2] = mState.blendColor.blue;
1830 params[3] = mState.blendColor.alpha;
1832 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1833 *params = MAX_TEXTURE_MAX_ANISOTROPY;
1842 template bool Context::getIntegerv<GLint>(GLenum pname, GLint *params) const;
1843 template bool Context::getIntegerv<GLint64>(GLenum pname, GLint64 *params) const;
1845 template<typename T> bool Context::getIntegerv(GLenum pname, T *params) const
1847 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1848 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1849 // GetIntegerv as its native query function. As it would require conversion in any
1850 // case, this should make no difference to the calling application. You may find it in
1851 // Context::getFloatv.
1854 case GL_MAX_VERTEX_ATTRIBS: *params = MAX_VERTEX_ATTRIBS; break;
1855 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = MAX_VERTEX_UNIFORM_VECTORS; break;
1856 case GL_MAX_VARYING_VECTORS: *params = MAX_VARYING_VECTORS; break;
1857 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = MAX_COMBINED_TEXTURE_IMAGE_UNITS; break;
1858 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = MAX_VERTEX_TEXTURE_IMAGE_UNITS; break;
1859 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = MAX_TEXTURE_IMAGE_UNITS; break;
1860 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = MAX_FRAGMENT_UNIFORM_VECTORS; break;
1861 case GL_MAX_RENDERBUFFER_SIZE: *params = IMPLEMENTATION_MAX_RENDERBUFFER_SIZE; break;
1862 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1863 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
1864 case GL_ARRAY_BUFFER_BINDING: *params = getArrayBufferName(); break;
1865 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = getElementArrayBufferName(); break;
1866 // case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1867 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1868 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
1869 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.name(); break;
1870 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1871 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1872 case GL_UNPACK_ALIGNMENT: *params = mState.unpackInfo.alignment; break;
1873 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1874 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
1875 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1876 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1877 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1878 case GL_STENCIL_VALUE_MASK: *params = sw::clampToSignedInt(mState.stencilMask); break;
1879 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1880 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1881 case GL_STENCIL_BACK_VALUE_MASK: *params = sw::clampToSignedInt(mState.stencilBackMask); break;
1882 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1883 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1884 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1885 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1886 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1887 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1888 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1889 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1890 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1891 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1892 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1893 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1894 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1895 case GL_STENCIL_WRITEMASK: *params = sw::clampToSignedInt(mState.stencilWritemask); break;
1896 case GL_STENCIL_BACK_WRITEMASK: *params = sw::clampToSignedInt(mState.stencilBackWritemask); break;
1897 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1898 case GL_SUBPIXEL_BITS: *params = 4; break;
1899 case GL_MAX_TEXTURE_SIZE: *params = IMPLEMENTATION_MAX_TEXTURE_SIZE; break;
1900 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE; break;
1901 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = NUM_COMPRESSED_TEXTURE_FORMATS; break;
1902 case GL_MAX_SAMPLES_ANGLE: *params = IMPLEMENTATION_MAX_SAMPLES; break;
1903 case GL_SAMPLE_BUFFERS:
1906 Framebuffer *framebuffer = getDrawFramebuffer();
1907 int width, height, samples;
1909 if(framebuffer->completeness(width, height, samples) == GL_FRAMEBUFFER_COMPLETE)
1913 case GL_SAMPLE_BUFFERS:
1934 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1936 Framebuffer *framebuffer = getReadFramebuffer();
1937 *params = framebuffer->getImplementationColorReadType();
1940 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1942 Framebuffer *framebuffer = getReadFramebuffer();
1943 *params = framebuffer->getImplementationColorReadFormat();
1946 case GL_MAX_VIEWPORT_DIMS:
1948 int maxDimension = IMPLEMENTATION_MAX_RENDERBUFFER_SIZE;
1949 params[0] = maxDimension;
1950 params[1] = maxDimension;
1953 case GL_COMPRESSED_TEXTURE_FORMATS:
1955 for(int i = 0; i < NUM_COMPRESSED_TEXTURE_FORMATS; i++)
1957 params[i] = compressedTextureFormats[i];
1962 params[0] = mState.viewportX;
1963 params[1] = mState.viewportY;
1964 params[2] = mState.viewportWidth;
1965 params[3] = mState.viewportHeight;
1967 case GL_SCISSOR_BOX:
1968 params[0] = mState.scissorX;
1969 params[1] = mState.scissorY;
1970 params[2] = mState.scissorWidth;
1971 params[3] = mState.scissorHeight;
1973 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1974 case GL_FRONT_FACE: *params = mState.frontFace; break;
1980 Framebuffer *framebuffer = getDrawFramebuffer();
1981 Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0);
1987 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1988 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1989 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1990 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
2001 Framebuffer *framebuffer = getDrawFramebuffer();
2002 Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
2006 *params = depthbuffer->getDepthSize();
2014 case GL_STENCIL_BITS:
2016 Framebuffer *framebuffer = getDrawFramebuffer();
2017 Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
2021 *params = stencilbuffer->getStencilSize();
2029 case GL_TEXTURE_BINDING_2D:
2030 if(mState.activeSampler > MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1)
2032 error(GL_INVALID_OPERATION);
2036 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].name();
2038 case GL_TEXTURE_BINDING_CUBE_MAP:
2039 if(mState.activeSampler > MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1)
2041 error(GL_INVALID_OPERATION);
2045 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].name();
2047 case GL_TEXTURE_BINDING_EXTERNAL_OES:
2048 if(mState.activeSampler > MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1)
2050 error(GL_INVALID_OPERATION);
2054 *params = mState.samplerTexture[TEXTURE_EXTERNAL][mState.activeSampler].name();
2056 case GL_TEXTURE_BINDING_3D_OES:
2057 if(mState.activeSampler > MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1)
2059 error(GL_INVALID_OPERATION);
2063 *params = mState.samplerTexture[TEXTURE_3D][mState.activeSampler].name();
2065 case GL_TEXTURE_BINDING_2D_ARRAY:
2066 if(clientVersion < 3)
2070 else if(mState.activeSampler > MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1)
2072 error(GL_INVALID_OPERATION);
2076 *params = mState.samplerTexture[TEXTURE_2D_ARRAY][mState.activeSampler].name();
2078 case GL_COPY_READ_BUFFER_BINDING:
2079 if(clientVersion >= 3)
2081 *params = mState.copyReadBuffer.name();
2088 case GL_COPY_WRITE_BUFFER_BINDING:
2089 if(clientVersion >= 3)
2091 *params = mState.copyWriteBuffer.name();
2098 case GL_DRAW_BUFFER0:
2099 case GL_DRAW_BUFFER1:
2100 case GL_DRAW_BUFFER2:
2101 case GL_DRAW_BUFFER3:
2102 case GL_DRAW_BUFFER4:
2103 case GL_DRAW_BUFFER5:
2104 case GL_DRAW_BUFFER6:
2105 case GL_DRAW_BUFFER7:
2106 case GL_DRAW_BUFFER8:
2107 case GL_DRAW_BUFFER9:
2108 case GL_DRAW_BUFFER10:
2109 case GL_DRAW_BUFFER11:
2110 case GL_DRAW_BUFFER12:
2111 case GL_DRAW_BUFFER13:
2112 case GL_DRAW_BUFFER14:
2113 case GL_DRAW_BUFFER15:
2114 *params = getDrawFramebuffer()->getDrawBuffer(pname - GL_DRAW_BUFFER0);
2116 case GL_MAJOR_VERSION:
2117 if(clientVersion >= 3)
2119 *params = clientVersion;
2126 case GL_MAX_3D_TEXTURE_SIZE:
2127 *params = IMPLEMENTATION_MAX_TEXTURE_SIZE;
2129 case GL_MAX_ARRAY_TEXTURE_LAYERS:
2130 *params = IMPLEMENTATION_MAX_TEXTURE_SIZE;
2132 case GL_MAX_COLOR_ATTACHMENTS:
2133 *params = MAX_COLOR_ATTACHMENTS;
2135 case GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS:
2136 *params = MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS;
2138 case GL_MAX_COMBINED_UNIFORM_BLOCKS:
2139 *params = MAX_VERTEX_UNIFORM_BLOCKS + MAX_FRAGMENT_UNIFORM_BLOCKS;
2141 case GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS:
2142 *params = MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS;
2144 case GL_MAX_DRAW_BUFFERS:
2145 *params = MAX_DRAW_BUFFERS;
2147 case GL_MAX_ELEMENT_INDEX:
2148 *params = MAX_ELEMENT_INDEX;
2150 case GL_MAX_ELEMENTS_INDICES:
2151 *params = MAX_ELEMENTS_INDICES;
2153 case GL_MAX_ELEMENTS_VERTICES:
2154 *params = MAX_ELEMENTS_VERTICES;
2156 case GL_MAX_FRAGMENT_INPUT_COMPONENTS:
2157 *params = MAX_FRAGMENT_INPUT_VECTORS * 4;
2159 case GL_MAX_FRAGMENT_UNIFORM_BLOCKS:
2160 *params = MAX_FRAGMENT_UNIFORM_BLOCKS;
2162 case GL_MAX_FRAGMENT_UNIFORM_COMPONENTS:
2163 *params = MAX_FRAGMENT_UNIFORM_COMPONENTS;
2165 case GL_MAX_PROGRAM_TEXEL_OFFSET:
2167 *params = MAX_PROGRAM_TEXEL_OFFSET;
2169 case GL_MAX_SERVER_WAIT_TIMEOUT:
2173 case GL_MAX_TEXTURE_LOD_BIAS:
2177 case GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS:
2178 *params = sw::MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS;
2180 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS:
2181 *params = MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS;
2183 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS:
2184 *params = sw::MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS;
2186 case GL_MAX_UNIFORM_BLOCK_SIZE:
2187 *params = MAX_UNIFORM_BLOCK_SIZE;
2189 case GL_MAX_UNIFORM_BUFFER_BINDINGS:
2190 *params = MAX_UNIFORM_BUFFER_BINDINGS;
2192 case GL_MAX_VARYING_COMPONENTS:
2194 // FIXME: should be MAX_VARYING_VECTORS * 4, but MAX_VARYING_VECTORS
2195 // must be increased (see MAX_VERTEX_OUTPUTS and MAX_FRAGMENT_INPUTS)
2198 case GL_MAX_VERTEX_OUTPUT_COMPONENTS:
2199 *params = MAX_VERTEX_OUTPUT_VECTORS * 4;
2201 case GL_MAX_VERTEX_UNIFORM_BLOCKS:
2202 *params = MAX_VERTEX_UNIFORM_BLOCKS;
2204 case GL_MAX_VERTEX_UNIFORM_COMPONENTS:
2205 *params = MAX_VERTEX_UNIFORM_COMPONENTS;
2207 case GL_MIN_PROGRAM_TEXEL_OFFSET:
2209 *params = MIN_PROGRAM_TEXEL_OFFSET;
2211 case GL_MINOR_VERSION:
2212 if (clientVersion >= 3)
2221 case GL_NUM_EXTENSIONS:
2222 GLuint numExtensions;
2223 getExtensions(0, &numExtensions);
2224 *params = numExtensions;
2226 case GL_NUM_PROGRAM_BINARY_FORMATS:
2227 *params = NUM_PROGRAM_BINARY_FORMATS;
2229 case GL_PACK_ROW_LENGTH:
2230 *params = mState.packRowLength;
2232 case GL_PACK_SKIP_PIXELS:
2233 *params = mState.packSkipPixels;
2235 case GL_PACK_SKIP_ROWS:
2236 *params = mState.packSkipRows;
2238 case GL_PIXEL_PACK_BUFFER_BINDING:
2239 if(clientVersion >= 3)
2241 *params = mState.pixelPackBuffer.name();
2248 case GL_PIXEL_UNPACK_BUFFER_BINDING:
2249 if(clientVersion >= 3)
2251 *params = mState.pixelUnpackBuffer.name();
2258 case GL_PROGRAM_BINARY_FORMATS:
2259 // Since NUM_PROGRAM_BINARY_FORMATS is 0, the input
2260 // should be a 0 sized array, so don't write to params
2262 case GL_READ_BUFFER:
2263 *params = getReadFramebuffer()->getReadBuffer();
2265 case GL_SAMPLER_BINDING:
2266 *params = mState.sampler[mState.activeSampler].name();
2268 case GL_UNIFORM_BUFFER_BINDING:
2269 if(clientVersion >= 3)
2271 *params = mState.genericUniformBuffer.name();
2278 case GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT:
2279 *params = UNIFORM_BUFFER_OFFSET_ALIGNMENT;
2281 case GL_UNIFORM_BUFFER_SIZE:
2282 if(clientVersion >= 3)
2284 *params = static_cast<T>(mState.genericUniformBuffer->size());
2291 case GL_UNIFORM_BUFFER_START:
2292 if(clientVersion >= 3)
2294 *params = static_cast<T>(mState.genericUniformBuffer->offset());
2302 case GL_UNPACK_IMAGE_HEIGHT:
2303 *params = mState.unpackInfo.imageHeight;
2305 case GL_UNPACK_ROW_LENGTH:
2306 *params = mState.unpackInfo.rowLength;
2308 case GL_UNPACK_SKIP_IMAGES:
2309 *params = mState.unpackInfo.skipImages;
2311 case GL_UNPACK_SKIP_PIXELS:
2312 *params = mState.unpackInfo.skipPixels;
2314 case GL_UNPACK_SKIP_ROWS:
2315 *params = mState.unpackInfo.skipRows;
2317 case GL_VERTEX_ARRAY_BINDING:
2318 *params = getCurrentVertexArray()->name;
2320 case GL_TRANSFORM_FEEDBACK_BINDING:
2322 TransformFeedback* transformFeedback = getTransformFeedback(mState.transformFeedback);
2323 if(transformFeedback)
2325 *params = transformFeedback->name;
2333 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
2335 TransformFeedback* transformFeedback = getTransformFeedback(mState.transformFeedback);
2336 if(transformFeedback)
2338 *params = transformFeedback->getGenericBufferName();
2353 template bool Context::getTransformFeedbackiv<GLint>(GLuint index, GLenum pname, GLint *param) const;
2354 template bool Context::getTransformFeedbackiv<GLint64>(GLuint index, GLenum pname, GLint64 *param) const;
2356 template<typename T> bool Context::getTransformFeedbackiv(GLuint index, GLenum pname, T *param) const
2358 TransformFeedback* transformFeedback = getTransformFeedback(mState.transformFeedback);
2359 if(!transformFeedback)
2366 case GL_TRANSFORM_FEEDBACK_BINDING: // GLint, initially 0
2367 *param = transformFeedback->name;
2369 case GL_TRANSFORM_FEEDBACK_ACTIVE: // boolean, initially GL_FALSE
2370 *param = transformFeedback->isActive();
2372 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING: // name, initially 0
2373 *param = transformFeedback->getBufferName(index);
2375 case GL_TRANSFORM_FEEDBACK_PAUSED: // boolean, initially GL_FALSE
2376 *param = transformFeedback->isPaused();
2378 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE: // indexed[n] 64-bit integer, initially 0
2379 if(transformFeedback->getBuffer(index))
2381 *param = transformFeedback->getSize(index);
2385 case GL_TRANSFORM_FEEDBACK_BUFFER_START: // indexed[n] 64-bit integer, initially 0
2386 if(transformFeedback->getBuffer(index))
2388 *param = transformFeedback->getOffset(index);
2399 template bool Context::getUniformBufferiv<GLint>(GLuint index, GLenum pname, GLint *param) const;
2400 template bool Context::getUniformBufferiv<GLint64>(GLuint index, GLenum pname, GLint64 *param) const;
2402 template<typename T> bool Context::getUniformBufferiv(GLuint index, GLenum pname, T *param) const
2404 const BufferBinding& uniformBuffer = mState.uniformBuffers[index];
2408 case GL_UNIFORM_BUFFER_BINDING: // name, initially 0
2409 *param = uniformBuffer.get().name();
2411 case GL_UNIFORM_BUFFER_SIZE: // indexed[n] 64-bit integer, initially 0
2412 *param = uniformBuffer.getSize();
2414 case GL_UNIFORM_BUFFER_START: // indexed[n] 64-bit integer, initially 0
2415 *param = uniformBuffer.getOffset();
2424 bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams) const
2426 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
2427 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
2428 // to the fact that it is stored internally as a float, and so would require conversion
2429 // if returned from Context::getIntegerv. Since this conversion is already implemented
2430 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
2431 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
2435 case GL_COMPRESSED_TEXTURE_FORMATS:
2438 *numParams = NUM_COMPRESSED_TEXTURE_FORMATS;
2441 case GL_SHADER_BINARY_FORMATS:
2447 case GL_MAX_VERTEX_ATTRIBS:
2448 case GL_MAX_VERTEX_UNIFORM_VECTORS:
2449 case GL_MAX_VARYING_VECTORS:
2450 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
2451 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
2452 case GL_MAX_TEXTURE_IMAGE_UNITS:
2453 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
2454 case GL_MAX_RENDERBUFFER_SIZE:
2455 case GL_NUM_SHADER_BINARY_FORMATS:
2456 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
2457 case GL_ARRAY_BUFFER_BINDING:
2458 case GL_FRAMEBUFFER_BINDING: // Same as GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
2459 case GL_READ_FRAMEBUFFER_BINDING_ANGLE:
2460 case GL_RENDERBUFFER_BINDING:
2461 case GL_CURRENT_PROGRAM:
2462 case GL_PACK_ALIGNMENT:
2463 case GL_UNPACK_ALIGNMENT:
2464 case GL_GENERATE_MIPMAP_HINT:
2465 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
2471 case GL_STENCIL_BITS:
2472 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
2473 case GL_CULL_FACE_MODE:
2475 case GL_ACTIVE_TEXTURE:
2476 case GL_STENCIL_FUNC:
2477 case GL_STENCIL_VALUE_MASK:
2478 case GL_STENCIL_REF:
2479 case GL_STENCIL_FAIL:
2480 case GL_STENCIL_PASS_DEPTH_FAIL:
2481 case GL_STENCIL_PASS_DEPTH_PASS:
2482 case GL_STENCIL_BACK_FUNC:
2483 case GL_STENCIL_BACK_VALUE_MASK:
2484 case GL_STENCIL_BACK_REF:
2485 case GL_STENCIL_BACK_FAIL:
2486 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
2487 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
2489 case GL_BLEND_SRC_RGB:
2490 case GL_BLEND_SRC_ALPHA:
2491 case GL_BLEND_DST_RGB:
2492 case GL_BLEND_DST_ALPHA:
2493 case GL_BLEND_EQUATION_RGB:
2494 case GL_BLEND_EQUATION_ALPHA:
2495 case GL_STENCIL_WRITEMASK:
2496 case GL_STENCIL_BACK_WRITEMASK:
2497 case GL_STENCIL_CLEAR_VALUE:
2498 case GL_SUBPIXEL_BITS:
2499 case GL_MAX_TEXTURE_SIZE:
2500 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
2501 case GL_SAMPLE_BUFFERS:
2503 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
2504 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
2505 case GL_TEXTURE_BINDING_2D:
2506 case GL_TEXTURE_BINDING_CUBE_MAP:
2507 case GL_TEXTURE_BINDING_EXTERNAL_OES:
2508 case GL_TEXTURE_BINDING_3D_OES:
2509 case GL_COPY_READ_BUFFER_BINDING:
2510 case GL_COPY_WRITE_BUFFER_BINDING:
2511 case GL_DRAW_BUFFER0:
2512 case GL_DRAW_BUFFER1:
2513 case GL_DRAW_BUFFER2:
2514 case GL_DRAW_BUFFER3:
2515 case GL_DRAW_BUFFER4:
2516 case GL_DRAW_BUFFER5:
2517 case GL_DRAW_BUFFER6:
2518 case GL_DRAW_BUFFER7:
2519 case GL_DRAW_BUFFER8:
2520 case GL_DRAW_BUFFER9:
2521 case GL_DRAW_BUFFER10:
2522 case GL_DRAW_BUFFER11:
2523 case GL_DRAW_BUFFER12:
2524 case GL_DRAW_BUFFER13:
2525 case GL_DRAW_BUFFER14:
2526 case GL_DRAW_BUFFER15:
2527 case GL_MAJOR_VERSION:
2528 case GL_MAX_3D_TEXTURE_SIZE:
2529 case GL_MAX_ARRAY_TEXTURE_LAYERS:
2530 case GL_MAX_COLOR_ATTACHMENTS:
2531 case GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS:
2532 case GL_MAX_COMBINED_UNIFORM_BLOCKS:
2533 case GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS:
2534 case GL_MAX_DRAW_BUFFERS:
2535 case GL_MAX_ELEMENT_INDEX:
2536 case GL_MAX_ELEMENTS_INDICES:
2537 case GL_MAX_ELEMENTS_VERTICES:
2538 case GL_MAX_FRAGMENT_INPUT_COMPONENTS:
2539 case GL_MAX_FRAGMENT_UNIFORM_BLOCKS:
2540 case GL_MAX_FRAGMENT_UNIFORM_COMPONENTS:
2541 case GL_MAX_PROGRAM_TEXEL_OFFSET:
2542 case GL_MAX_SERVER_WAIT_TIMEOUT:
2543 case GL_MAX_TEXTURE_LOD_BIAS:
2544 case GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS:
2545 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS:
2546 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS:
2547 case GL_MAX_UNIFORM_BLOCK_SIZE:
2548 case GL_MAX_UNIFORM_BUFFER_BINDINGS:
2549 case GL_MAX_VARYING_COMPONENTS:
2550 case GL_MAX_VERTEX_OUTPUT_COMPONENTS:
2551 case GL_MAX_VERTEX_UNIFORM_BLOCKS:
2552 case GL_MAX_VERTEX_UNIFORM_COMPONENTS:
2553 case GL_MIN_PROGRAM_TEXEL_OFFSET:
2554 case GL_MINOR_VERSION:
2555 case GL_NUM_EXTENSIONS:
2556 case GL_NUM_PROGRAM_BINARY_FORMATS:
2557 case GL_PACK_ROW_LENGTH:
2558 case GL_PACK_SKIP_PIXELS:
2559 case GL_PACK_SKIP_ROWS:
2560 case GL_PIXEL_PACK_BUFFER_BINDING:
2561 case GL_PIXEL_UNPACK_BUFFER_BINDING:
2562 case GL_PROGRAM_BINARY_FORMATS:
2563 case GL_READ_BUFFER:
2564 case GL_SAMPLER_BINDING:
2565 case GL_TEXTURE_BINDING_2D_ARRAY:
2566 case GL_UNIFORM_BUFFER_BINDING:
2567 case GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT:
2568 case GL_UNIFORM_BUFFER_SIZE:
2569 case GL_UNIFORM_BUFFER_START:
2570 case GL_UNPACK_IMAGE_HEIGHT:
2571 case GL_UNPACK_ROW_LENGTH:
2572 case GL_UNPACK_SKIP_IMAGES:
2573 case GL_UNPACK_SKIP_PIXELS:
2574 case GL_UNPACK_SKIP_ROWS:
2575 case GL_VERTEX_ARRAY_BINDING:
2576 case GL_TRANSFORM_FEEDBACK_BINDING:
2577 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
2583 case GL_MAX_SAMPLES_ANGLE:
2589 case GL_MAX_VIEWPORT_DIMS:
2596 case GL_SCISSOR_BOX:
2602 case GL_SHADER_COMPILER:
2603 case GL_SAMPLE_COVERAGE_INVERT:
2604 case GL_DEPTH_WRITEMASK:
2605 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
2606 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
2607 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
2608 case GL_SAMPLE_COVERAGE:
2609 case GL_SCISSOR_TEST:
2610 case GL_STENCIL_TEST:
2614 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
2615 case GL_RASTERIZER_DISCARD:
2616 case GL_TRANSFORM_FEEDBACK_ACTIVE:
2617 case GL_TRANSFORM_FEEDBACK_PAUSED:
2623 case GL_COLOR_WRITEMASK:
2629 case GL_POLYGON_OFFSET_FACTOR:
2630 case GL_POLYGON_OFFSET_UNITS:
2631 case GL_SAMPLE_COVERAGE_VALUE:
2632 case GL_DEPTH_CLEAR_VALUE:
2639 case GL_ALIASED_LINE_WIDTH_RANGE:
2640 case GL_ALIASED_POINT_SIZE_RANGE:
2641 case GL_DEPTH_RANGE:
2647 case GL_COLOR_CLEAR_VALUE:
2648 case GL_BLEND_COLOR:
2654 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
2665 void Context::applyScissor(int width, int height)
2667 if(mState.scissorTestEnabled)
2669 sw::Rect scissor = { mState.scissorX, mState.scissorY, mState.scissorX + mState.scissorWidth, mState.scissorY + mState.scissorHeight };
2670 scissor.clip(0, 0, width, height);
2672 device->setScissorRect(scissor);
2673 device->setScissorEnable(true);
2677 device->setScissorEnable(false);
2681 // Applies the render target surface, depth stencil surface, viewport rectangle and scissor rectangle
2682 bool Context::applyRenderTarget()
2684 Framebuffer *framebuffer = getDrawFramebuffer();
2685 int width, height, samples;
2687 if(!framebuffer || framebuffer->completeness(width, height, samples) != GL_FRAMEBUFFER_COMPLETE)
2689 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
2692 for(int i = 0; i < MAX_DRAW_BUFFERS; i++)
2694 if(framebuffer->getDrawBuffer(i) != GL_NONE)
2696 egl::Image *renderTarget = framebuffer->getRenderTarget(i);
2697 device->setRenderTarget(i, renderTarget);
2698 if(renderTarget) renderTarget->release();
2702 device->setRenderTarget(i, nullptr);
2706 egl::Image *depthBuffer = framebuffer->getDepthBuffer();
2707 device->setDepthBuffer(depthBuffer);
2708 if(depthBuffer) depthBuffer->release();
2710 egl::Image *stencilBuffer = framebuffer->getStencilBuffer();
2711 device->setStencilBuffer(stencilBuffer);
2712 if(stencilBuffer) stencilBuffer->release();
2715 float zNear = clamp01(mState.zNear);
2716 float zFar = clamp01(mState.zFar);
2718 viewport.x0 = mState.viewportX;
2719 viewport.y0 = mState.viewportY;
2720 viewport.width = mState.viewportWidth;
2721 viewport.height = mState.viewportHeight;
2722 viewport.minZ = zNear;
2723 viewport.maxZ = zFar;
2725 device->setViewport(viewport);
2727 applyScissor(width, height);
2729 Program *program = getCurrentProgram();
2733 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
2734 program->setUniform1fv(program->getUniformLocation("gl_DepthRange.near"), 1, &nearFarDiff[0]);
2735 program->setUniform1fv(program->getUniformLocation("gl_DepthRange.far"), 1, &nearFarDiff[1]);
2736 program->setUniform1fv(program->getUniformLocation("gl_DepthRange.diff"), 1, &nearFarDiff[2]);
2742 // Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc)
2743 void Context::applyState(GLenum drawMode)
2745 Framebuffer *framebuffer = getDrawFramebuffer();
2747 if(mState.cullFaceEnabled)
2749 device->setCullMode(es2sw::ConvertCullMode(mState.cullMode, mState.frontFace));
2753 device->setCullMode(sw::CULL_NONE);
2756 if(mDepthStateDirty)
2758 if(mState.depthTestEnabled)
2760 device->setDepthBufferEnable(true);
2761 device->setDepthCompare(es2sw::ConvertDepthComparison(mState.depthFunc));
2765 device->setDepthBufferEnable(false);
2768 mDepthStateDirty = false;
2771 if(mBlendStateDirty)
2773 if(mState.blendEnabled)
2775 device->setAlphaBlendEnable(true);
2776 device->setSeparateAlphaBlendEnable(true);
2778 device->setBlendConstant(es2sw::ConvertColor(mState.blendColor));
2780 device->setSourceBlendFactor(es2sw::ConvertBlendFunc(mState.sourceBlendRGB));
2781 device->setDestBlendFactor(es2sw::ConvertBlendFunc(mState.destBlendRGB));
2782 device->setBlendOperation(es2sw::ConvertBlendOp(mState.blendEquationRGB));
2784 device->setSourceBlendFactorAlpha(es2sw::ConvertBlendFunc(mState.sourceBlendAlpha));
2785 device->setDestBlendFactorAlpha(es2sw::ConvertBlendFunc(mState.destBlendAlpha));
2786 device->setBlendOperationAlpha(es2sw::ConvertBlendOp(mState.blendEquationAlpha));
2790 device->setAlphaBlendEnable(false);
2793 mBlendStateDirty = false;
2796 if(mStencilStateDirty || mFrontFaceDirty)
2798 if(mState.stencilTestEnabled && framebuffer->hasStencil())
2800 device->setStencilEnable(true);
2801 device->setTwoSidedStencil(true);
2803 // get the maximum size of the stencil ref
2804 Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
2805 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2807 if(mState.frontFace == GL_CCW)
2809 device->setStencilWriteMask(mState.stencilWritemask);
2810 device->setStencilCompare(es2sw::ConvertStencilComparison(mState.stencilFunc));
2812 device->setStencilReference((mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2813 device->setStencilMask(mState.stencilMask);
2815 device->setStencilFailOperation(es2sw::ConvertStencilOp(mState.stencilFail));
2816 device->setStencilZFailOperation(es2sw::ConvertStencilOp(mState.stencilPassDepthFail));
2817 device->setStencilPassOperation(es2sw::ConvertStencilOp(mState.stencilPassDepthPass));
2819 device->setStencilWriteMaskCCW(mState.stencilBackWritemask);
2820 device->setStencilCompareCCW(es2sw::ConvertStencilComparison(mState.stencilBackFunc));
2822 device->setStencilReferenceCCW((mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2823 device->setStencilMaskCCW(mState.stencilBackMask);
2825 device->setStencilFailOperationCCW(es2sw::ConvertStencilOp(mState.stencilBackFail));
2826 device->setStencilZFailOperationCCW(es2sw::ConvertStencilOp(mState.stencilBackPassDepthFail));
2827 device->setStencilPassOperationCCW(es2sw::ConvertStencilOp(mState.stencilBackPassDepthPass));
2831 device->setStencilWriteMaskCCW(mState.stencilWritemask);
2832 device->setStencilCompareCCW(es2sw::ConvertStencilComparison(mState.stencilFunc));
2834 device->setStencilReferenceCCW((mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2835 device->setStencilMaskCCW(mState.stencilMask);
2837 device->setStencilFailOperationCCW(es2sw::ConvertStencilOp(mState.stencilFail));
2838 device->setStencilZFailOperationCCW(es2sw::ConvertStencilOp(mState.stencilPassDepthFail));
2839 device->setStencilPassOperationCCW(es2sw::ConvertStencilOp(mState.stencilPassDepthPass));
2841 device->setStencilWriteMask(mState.stencilBackWritemask);
2842 device->setStencilCompare(es2sw::ConvertStencilComparison(mState.stencilBackFunc));
2844 device->setStencilReference((mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2845 device->setStencilMask(mState.stencilBackMask);
2847 device->setStencilFailOperation(es2sw::ConvertStencilOp(mState.stencilBackFail));
2848 device->setStencilZFailOperation(es2sw::ConvertStencilOp(mState.stencilBackPassDepthFail));
2849 device->setStencilPassOperation(es2sw::ConvertStencilOp(mState.stencilBackPassDepthPass));
2854 device->setStencilEnable(false);
2857 mStencilStateDirty = false;
2858 mFrontFaceDirty = false;
2863 for(int i = 0; i < MAX_DRAW_BUFFERS; i++)
2865 device->setColorWriteMask(i, es2sw::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
2868 device->setDepthWriteEnable(mState.depthMask);
2870 mMaskStateDirty = false;
2873 if(mPolygonOffsetStateDirty)
2875 if(mState.polygonOffsetFillEnabled)
2877 Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
2880 device->setSlopeDepthBias(mState.polygonOffsetFactor);
2881 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
2882 device->setDepthBias(depthBias);
2887 device->setSlopeDepthBias(0);
2888 device->setDepthBias(0);
2891 mPolygonOffsetStateDirty = false;
2894 if(mSampleStateDirty)
2896 if(mState.sampleAlphaToCoverageEnabled)
2898 device->setTransparencyAntialiasing(sw::TRANSPARENCY_ALPHA_TO_COVERAGE);
2902 device->setTransparencyAntialiasing(sw::TRANSPARENCY_NONE);
2905 if(mState.sampleCoverageEnabled)
2907 unsigned int mask = 0;
2908 if(mState.sampleCoverageValue != 0)
2910 int width, height, samples;
2911 framebuffer->completeness(width, height, samples);
2913 float threshold = 0.5f;
2915 for(int i = 0; i < samples; i++)
2919 if((i + 1) * mState.sampleCoverageValue >= threshold)
2927 if(mState.sampleCoverageInvert)
2932 device->setMultiSampleMask(mask);
2936 device->setMultiSampleMask(0xFFFFFFFF);
2939 mSampleStateDirty = false;
2942 if(mDitherStateDirty)
2944 // UNIMPLEMENTED(); // FIXME
2946 mDitherStateDirty = false;
2949 device->setRasterizerDiscard(mState.rasterizerDiscardEnabled);
2952 GLenum Context::applyVertexBuffer(GLint base, GLint first, GLsizei count, GLsizei instanceId)
2954 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
2956 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instanceId);
2957 if(err != GL_NO_ERROR)
2962 Program *program = getCurrentProgram();
2964 device->resetInputStreams(false);
2966 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2968 if(program->getAttributeStream(i) == -1)
2973 sw::Resource *resource = attributes[i].vertexBuffer;
2974 const void *buffer = (char*)resource->data() + attributes[i].offset;
2976 int stride = attributes[i].stride;
2978 buffer = (char*)buffer + stride * base;
2980 sw::Stream attribute(resource, buffer, stride);
2982 attribute.type = attributes[i].type;
2983 attribute.count = attributes[i].count;
2984 attribute.normalized = attributes[i].normalized;
2986 int stream = program->getAttributeStream(i);
2987 device->setInputStream(stream, attribute);
2993 // Applies the indices and element array bindings
2994 GLenum Context::applyIndexBuffer(const void *indices, GLuint start, GLuint end, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
2996 GLenum err = mIndexDataManager->prepareIndexData(type, start, end, count, getCurrentVertexArray()->getElementArrayBuffer(), indices, indexInfo);
2998 if(err == GL_NO_ERROR)
3000 device->setIndexBuffer(indexInfo->indexBuffer);
3006 // Applies the shaders and shader constants
3007 void Context::applyShaders()
3009 Program *programObject = getCurrentProgram();
3010 sw::VertexShader *vertexShader = programObject->getVertexShader();
3011 sw::PixelShader *pixelShader = programObject->getPixelShader();
3013 device->setVertexShader(vertexShader);
3014 device->setPixelShader(pixelShader);
3016 if(programObject->getSerial() != mAppliedProgramSerial)
3018 programObject->dirtyAllUniforms();
3019 mAppliedProgramSerial = programObject->getSerial();
3022 programObject->applyTransformFeedback(getTransformFeedback());
3023 programObject->applyUniformBuffers(mState.uniformBuffers);
3024 programObject->applyUniforms();
3027 void Context::applyTextures()
3029 applyTextures(sw::SAMPLER_PIXEL);
3030 applyTextures(sw::SAMPLER_VERTEX);
3033 void Context::applyTextures(sw::SamplerType samplerType)
3035 Program *programObject = getCurrentProgram();
3037 int samplerCount = (samplerType == sw::SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS; // Range of samplers of given sampler type
3039 for(int samplerIndex = 0; samplerIndex < samplerCount; samplerIndex++)
3041 int textureUnit = programObject->getSamplerMapping(samplerType, samplerIndex); // OpenGL texture image unit index
3043 if(textureUnit != -1)
3045 TextureType textureType = programObject->getSamplerTextureType(samplerType, samplerIndex);
3047 Texture *texture = getSamplerTexture(textureUnit, textureType);
3049 if(texture->isSamplerComplete())
3051 GLenum wrapS, wrapT, wrapR, minFilter, magFilter;
3052 GLfloat minLOD, maxLOD;
3054 Sampler *samplerObject = mState.sampler[textureUnit];
3057 wrapS = samplerObject->getWrapS();
3058 wrapT = samplerObject->getWrapT();
3059 wrapR = samplerObject->getWrapR();
3060 minFilter = samplerObject->getMinFilter();
3061 magFilter = samplerObject->getMagFilter();
3062 minLOD = samplerObject->getMinLod();
3063 maxLOD = samplerObject->getMaxLod();
3067 wrapS = texture->getWrapS();
3068 wrapT = texture->getWrapT();
3069 wrapR = texture->getWrapR();
3070 minFilter = texture->getMinFilter();
3071 magFilter = texture->getMagFilter();
3072 minLOD = texture->getMinLOD();
3073 maxLOD = texture->getMaxLOD();
3075 GLfloat maxAnisotropy = texture->getMaxAnisotropy();
3077 GLint baseLevel = texture->getBaseLevel();
3078 GLint maxLevel = texture->getMaxLevel();
3079 GLenum swizzleR = texture->getSwizzleR();
3080 GLenum swizzleG = texture->getSwizzleG();
3081 GLenum swizzleB = texture->getSwizzleB();
3082 GLenum swizzleA = texture->getSwizzleA();
3084 device->setAddressingModeU(samplerType, samplerIndex, es2sw::ConvertTextureWrap(wrapS));
3085 device->setAddressingModeV(samplerType, samplerIndex, es2sw::ConvertTextureWrap(wrapT));
3086 device->setAddressingModeW(samplerType, samplerIndex, es2sw::ConvertTextureWrap(wrapR));
3087 device->setSwizzleR(samplerType, samplerIndex, es2sw::ConvertSwizzleType(swizzleR));
3088 device->setSwizzleG(samplerType, samplerIndex, es2sw::ConvertSwizzleType(swizzleG));
3089 device->setSwizzleB(samplerType, samplerIndex, es2sw::ConvertSwizzleType(swizzleB));
3090 device->setSwizzleA(samplerType, samplerIndex, es2sw::ConvertSwizzleType(swizzleA));
3091 device->setMinLod(samplerType, samplerIndex, minLOD);
3092 device->setMaxLod(samplerType, samplerIndex, maxLOD);
3093 device->setBaseLevel(samplerType, samplerIndex, baseLevel);
3094 device->setMaxLevel(samplerType, samplerIndex, maxLevel);
3096 device->setTextureFilter(samplerType, samplerIndex, es2sw::ConvertTextureFilter(minFilter, magFilter, maxAnisotropy));
3097 device->setMipmapFilter(samplerType, samplerIndex, es2sw::ConvertMipMapFilter(minFilter));
3098 device->setMaxAnisotropy(samplerType, samplerIndex, maxAnisotropy);
3100 applyTexture(samplerType, samplerIndex, texture);
3104 applyTexture(samplerType, samplerIndex, nullptr);
3109 applyTexture(samplerType, samplerIndex, nullptr);
3114 void Context::applyTexture(sw::SamplerType type, int index, Texture *baseTexture)
3116 Program *program = getCurrentProgram();
3117 int sampler = (type == sw::SAMPLER_PIXEL) ? index : 16 + index;
3118 bool textureUsed = false;
3120 if(type == sw::SAMPLER_PIXEL)
3122 textureUsed = program->getPixelShader()->usesSampler(index);
3124 else if(type == sw::SAMPLER_VERTEX)
3126 textureUsed = program->getVertexShader()->usesSampler(index);
3128 else UNREACHABLE(type);
3130 sw::Resource *resource = 0;
3132 if(baseTexture && textureUsed)
3134 resource = baseTexture->getResource();
3137 device->setTextureResource(sampler, resource);
3139 if(baseTexture && textureUsed)
3141 int levelCount = baseTexture->getLevelCount();
3143 if(baseTexture->getTarget() == GL_TEXTURE_2D || baseTexture->getTarget() == GL_TEXTURE_EXTERNAL_OES)
3145 Texture2D *texture = static_cast<Texture2D*>(baseTexture);
3147 for(int mipmapLevel = 0; mipmapLevel < sw::MIPMAP_LEVELS; mipmapLevel++)
3149 int surfaceLevel = mipmapLevel;
3151 if(surfaceLevel < 0)
3155 else if(surfaceLevel >= levelCount)
3157 surfaceLevel = levelCount - 1;
3160 egl::Image *surface = texture->getImage(surfaceLevel);
3161 device->setTextureLevel(sampler, 0, mipmapLevel, surface, sw::TEXTURE_2D);
3164 else if(baseTexture->getTarget() == GL_TEXTURE_3D_OES)
3166 Texture3D *texture = static_cast<Texture3D*>(baseTexture);
3168 for(int mipmapLevel = 0; mipmapLevel < sw::MIPMAP_LEVELS; mipmapLevel++)
3170 int surfaceLevel = mipmapLevel;
3172 if(surfaceLevel < 0)
3176 else if(surfaceLevel >= levelCount)
3178 surfaceLevel = levelCount - 1;
3181 egl::Image *surface = texture->getImage(surfaceLevel);
3182 device->setTextureLevel(sampler, 0, mipmapLevel, surface, sw::TEXTURE_3D);
3185 else if(baseTexture->getTarget() == GL_TEXTURE_2D_ARRAY)
3187 Texture2DArray *texture = static_cast<Texture2DArray*>(baseTexture);
3189 for(int mipmapLevel = 0; mipmapLevel < sw::MIPMAP_LEVELS; mipmapLevel++)
3191 int surfaceLevel = mipmapLevel;
3193 if(surfaceLevel < 0)
3197 else if(surfaceLevel >= levelCount)
3199 surfaceLevel = levelCount - 1;
3202 egl::Image *surface = texture->getImage(surfaceLevel);
3203 device->setTextureLevel(sampler, 0, mipmapLevel, surface, sw::TEXTURE_2D_ARRAY);
3206 else if(baseTexture->getTarget() == GL_TEXTURE_CUBE_MAP)
3208 for(int face = 0; face < 6; face++)
3210 TextureCubeMap *cubeTexture = static_cast<TextureCubeMap*>(baseTexture);
3212 for(int mipmapLevel = 0; mipmapLevel < sw::MIPMAP_LEVELS; mipmapLevel++)
3214 int surfaceLevel = mipmapLevel;
3216 if(surfaceLevel < 0)
3220 else if(surfaceLevel >= levelCount)
3222 surfaceLevel = levelCount - 1;
3225 egl::Image *surface = cubeTexture->getImage(face, surfaceLevel);
3226 device->setTextureLevel(sampler, face, mipmapLevel, surface, sw::TEXTURE_CUBE);
3230 else UNIMPLEMENTED();
3234 device->setTextureLevel(sampler, 0, 0, 0, sw::TEXTURE_NULL);
3238 void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
3240 Framebuffer *framebuffer = getReadFramebuffer();
3241 int framebufferWidth, framebufferHeight, framebufferSamples;
3243 if(framebuffer->completeness(framebufferWidth, framebufferHeight, framebufferSamples) != GL_FRAMEBUFFER_COMPLETE)
3245 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3248 if(getReadFramebufferName() != 0 && framebufferSamples != 0)
3250 return error(GL_INVALID_OPERATION);
3253 GLenum readFormat = GL_NONE;
3254 GLenum readType = GL_NONE;
3257 case GL_DEPTH_COMPONENT:
3258 readFormat = framebuffer->getDepthReadFormat();
3259 readType = framebuffer->getDepthReadType();
3262 readFormat = framebuffer->getImplementationColorReadFormat();
3263 readType = framebuffer->getImplementationColorReadType();
3267 if(!(readFormat == format && readType == type) && !ValidReadPixelsFormatType(readFormat, readType, format, type, clientVersion))
3269 return error(GL_INVALID_OPERATION);
3272 GLsizei outputWidth = (mState.packRowLength > 0) ? mState.packRowLength : width;
3273 GLsizei outputPitch = egl::ComputePitch(outputWidth, format, type, mState.packAlignment);
3274 GLsizei outputHeight = (mState.packImageHeight == 0) ? height : mState.packImageHeight;
3275 pixels = getPixelPackBuffer() ? (unsigned char*)getPixelPackBuffer()->data() + (ptrdiff_t)pixels : (unsigned char*)pixels;
3276 pixels = ((char*)pixels) + egl::ComputePackingOffset(format, type, outputWidth, outputHeight, mState.packAlignment, mState.packSkipImages, mState.packSkipRows, mState.packSkipPixels);
3278 // Sized query sanity check
3281 int requiredSize = outputPitch * height;
3282 if(requiredSize > *bufSize)
3284 return error(GL_INVALID_OPERATION);
3288 egl::Image *renderTarget = nullptr;
3291 case GL_DEPTH_COMPONENT:
3292 renderTarget = framebuffer->getDepthBuffer();
3295 renderTarget = framebuffer->getReadRenderTarget();
3301 return error(GL_INVALID_OPERATION);
3304 sw::Rect rect = {x, y, x + width, y + height};
3305 sw::Rect dstRect = { 0, 0, width, height };
3306 rect.clip(0, 0, renderTarget->getWidth(), renderTarget->getHeight());
3308 sw::Surface externalSurface(width, height, 1, egl::ConvertFormatType(format, type), pixels, outputPitch, outputPitch * outputHeight);
3309 sw::SliceRect sliceRect(rect);
3310 sw::SliceRect dstSliceRect(dstRect);
3311 device->blit(renderTarget, sliceRect, &externalSurface, dstSliceRect, false);
3313 renderTarget->release();
3316 void Context::clear(GLbitfield mask)
3318 if(mState.rasterizerDiscardEnabled)
3323 Framebuffer *framebuffer = getDrawFramebuffer();
3325 if(!framebuffer || framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3327 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3330 if(!applyRenderTarget())
3335 if(mask & GL_COLOR_BUFFER_BIT)
3337 unsigned int rgbaMask = getColorMask();
3341 device->clearColor(mState.colorClearValue.red, mState.colorClearValue.green, mState.colorClearValue.blue, mState.colorClearValue.alpha, rgbaMask);
3345 if(mask & GL_DEPTH_BUFFER_BIT)
3347 if(mState.depthMask != 0)
3349 float depth = clamp01(mState.depthClearValue);
3350 device->clearDepth(depth);
3354 if(mask & GL_STENCIL_BUFFER_BIT)
3356 if(mState.stencilWritemask != 0)
3358 int stencil = mState.stencilClearValue & 0x000000FF;
3359 device->clearStencil(stencil, mState.stencilWritemask);
3364 void Context::clearColorBuffer(GLint drawbuffer, void *value, sw::Format format)
3366 unsigned int rgbaMask = getColorMask();
3367 if(rgbaMask && !mState.rasterizerDiscardEnabled)
3369 Framebuffer *framebuffer = getDrawFramebuffer();
3370 egl::Image *colorbuffer = framebuffer->getRenderTarget(drawbuffer);
3374 sw::SliceRect clearRect = colorbuffer->getRect();
3376 if(mState.scissorTestEnabled)
3378 clearRect.clip(mState.scissorX, mState.scissorY, mState.scissorX + mState.scissorWidth, mState.scissorY + mState.scissorHeight);
3381 device->clear(value, format, colorbuffer, clearRect, rgbaMask);
3383 colorbuffer->release();
3388 void Context::clearColorBuffer(GLint drawbuffer, const GLint *value)
3390 clearColorBuffer(drawbuffer, (void*)value, sw::FORMAT_A32B32G32R32I);
3393 void Context::clearColorBuffer(GLint drawbuffer, const GLuint *value)
3395 clearColorBuffer(drawbuffer, (void*)value, sw::FORMAT_A32B32G32R32UI);
3398 void Context::clearColorBuffer(GLint drawbuffer, const GLfloat *value)
3400 clearColorBuffer(drawbuffer, (void*)value, sw::FORMAT_A32B32G32R32F);
3403 void Context::clearDepthBuffer(const GLfloat value)
3405 if(mState.depthMask && !mState.rasterizerDiscardEnabled)
3407 Framebuffer *framebuffer = getDrawFramebuffer();
3408 egl::Image *depthbuffer = framebuffer->getDepthBuffer();
3412 float depth = clamp01(value);
3413 sw::SliceRect clearRect = depthbuffer->getRect();
3415 if(mState.scissorTestEnabled)
3417 clearRect.clip(mState.scissorX, mState.scissorY, mState.scissorX + mState.scissorWidth, mState.scissorY + mState.scissorHeight);
3420 depthbuffer->clearDepth(depth, clearRect.x0, clearRect.y0, clearRect.width(), clearRect.height());
3422 depthbuffer->release();
3427 void Context::clearStencilBuffer(const GLint value)
3429 if(mState.stencilWritemask && !mState.rasterizerDiscardEnabled)
3431 Framebuffer *framebuffer = getDrawFramebuffer();
3432 egl::Image *stencilbuffer = framebuffer->getStencilBuffer();
3436 unsigned char stencil = value < 0 ? 0 : static_cast<unsigned char>(value & 0x000000FF);
3437 sw::SliceRect clearRect = stencilbuffer->getRect();
3439 if(mState.scissorTestEnabled)
3441 clearRect.clip(mState.scissorX, mState.scissorY, mState.scissorX + mState.scissorWidth, mState.scissorY + mState.scissorHeight);
3444 stencilbuffer->clearStencil(stencil, static_cast<unsigned char>(mState.stencilWritemask), clearRect.x0, clearRect.y0, clearRect.width(), clearRect.height());
3446 stencilbuffer->release();
3451 void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instanceCount)
3453 if(!mState.currentProgram)
3455 return error(GL_INVALID_OPERATION);
3458 sw::DrawType primitiveType;
3460 int verticesPerPrimitive;
3462 if(!es2sw::ConvertPrimitiveType(mode, count, GL_NONE, primitiveType, primitiveCount, verticesPerPrimitive))
3463 return error(GL_INVALID_ENUM);
3465 if(primitiveCount <= 0)
3470 if(!applyRenderTarget())
3477 for(int i = 0; i < instanceCount; ++i)
3479 device->setInstanceID(i);
3481 GLenum err = applyVertexBuffer(0, first, count, i);
3482 if(err != GL_NO_ERROR)
3490 if(!getCurrentProgram()->validateSamplers(false))
3492 return error(GL_INVALID_OPERATION);
3495 TransformFeedback* transformFeedback = getTransformFeedback();
3496 if(!cullSkipsDraw(mode) || (transformFeedback->isActive() && !transformFeedback->isPaused()))
3498 device->drawPrimitive(primitiveType, primitiveCount);
3500 if(transformFeedback)
3502 transformFeedback->addVertexOffset(primitiveCount * verticesPerPrimitive);
3507 void Context::drawElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const void *indices, GLsizei instanceCount)
3509 if(!mState.currentProgram)
3511 return error(GL_INVALID_OPERATION);
3514 if(!indices && !getCurrentVertexArray()->getElementArrayBuffer())
3516 return error(GL_INVALID_OPERATION);
3519 sw::DrawType primitiveType;
3521 int verticesPerPrimitive;
3523 if(!es2sw::ConvertPrimitiveType(mode, count, type, primitiveType, primitiveCount, verticesPerPrimitive))
3524 return error(GL_INVALID_ENUM);
3526 if(primitiveCount <= 0)
3531 if(!applyRenderTarget())
3538 for(int i = 0; i < instanceCount; ++i)
3540 device->setInstanceID(i);
3542 TranslatedIndexData indexInfo;
3543 GLenum err = applyIndexBuffer(indices, start, end, count, mode, type, &indexInfo);
3544 if(err != GL_NO_ERROR)
3549 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
3550 err = applyVertexBuffer(-(int)indexInfo.minIndex, indexInfo.minIndex, vertexCount, i);
3551 if(err != GL_NO_ERROR)
3559 if(!getCurrentProgram()->validateSamplers(false))
3561 return error(GL_INVALID_OPERATION);
3564 TransformFeedback* transformFeedback = getTransformFeedback();
3565 if(!cullSkipsDraw(mode) || (transformFeedback->isActive() && !transformFeedback->isPaused()))
3567 device->drawIndexedPrimitive(primitiveType, indexInfo.indexOffset, primitiveCount);
3569 if(transformFeedback)
3571 transformFeedback->addVertexOffset(primitiveCount * verticesPerPrimitive);
3576 void Context::finish()
3581 void Context::flush()
3583 // We don't queue anything without processing it as fast as possible
3586 void Context::recordInvalidEnum()
3588 mInvalidEnum = true;
3591 void Context::recordInvalidValue()
3593 mInvalidValue = true;
3596 void Context::recordInvalidOperation()
3598 mInvalidOperation = true;
3601 void Context::recordOutOfMemory()
3603 mOutOfMemory = true;
3606 void Context::recordInvalidFramebufferOperation()
3608 mInvalidFramebufferOperation = true;
3611 // Get one of the recorded errors and clear its flag, if any.
3612 // [OpenGL ES 2.0.24] section 2.5 page 13.
3613 GLenum Context::getError()
3617 mInvalidEnum = false;
3619 return GL_INVALID_ENUM;
3624 mInvalidValue = false;
3626 return GL_INVALID_VALUE;
3629 if(mInvalidOperation)
3631 mInvalidOperation = false;
3633 return GL_INVALID_OPERATION;
3638 mOutOfMemory = false;
3640 return GL_OUT_OF_MEMORY;
3643 if(mInvalidFramebufferOperation)
3645 mInvalidFramebufferOperation = false;
3647 return GL_INVALID_FRAMEBUFFER_OPERATION;
3653 int Context::getSupportedMultisampleCount(int requested)
3657 for(int i = NUM_MULTISAMPLE_COUNTS - 1; i >= 0; i--)
3659 if(supported >= requested)
3664 supported = multisampleCount[i];
3670 void Context::detachBuffer(GLuint buffer)
3672 // [OpenGL ES 2.0.24] section 2.9 page 22:
3673 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3674 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3676 if(mState.copyReadBuffer.name() == buffer)
3678 mState.copyReadBuffer = nullptr;
3681 if(mState.copyWriteBuffer.name() == buffer)
3683 mState.copyWriteBuffer = nullptr;
3686 if(mState.pixelPackBuffer.name() == buffer)
3688 mState.pixelPackBuffer = nullptr;
3691 if(mState.pixelUnpackBuffer.name() == buffer)
3693 mState.pixelUnpackBuffer = nullptr;
3696 if(mState.genericUniformBuffer.name() == buffer)
3698 mState.genericUniformBuffer = nullptr;
3701 if(getArrayBufferName() == buffer)
3703 mState.arrayBuffer = nullptr;
3706 // Only detach from the current transform feedback
3707 TransformFeedback* currentTransformFeedback = getTransformFeedback();
3708 if(currentTransformFeedback)
3710 currentTransformFeedback->detachBuffer(buffer);
3713 // Only detach from the current vertex array
3714 VertexArray* currentVertexArray = getCurrentVertexArray();
3715 if(currentVertexArray)
3717 currentVertexArray->detachBuffer(buffer);
3720 for(int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3722 if(mState.vertexAttribute[attribute].mBoundBuffer.name() == buffer)
3724 mState.vertexAttribute[attribute].mBoundBuffer = nullptr;
3729 void Context::detachTexture(GLuint texture)
3731 // [OpenGL ES 2.0.24] section 3.8 page 84:
3732 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3733 // rebound to texture object zero
3735 for(int type = 0; type < TEXTURE_TYPE_COUNT; type++)
3737 for(int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS; sampler++)
3739 if(mState.samplerTexture[type][sampler].name() == texture)
3741 mState.samplerTexture[type][sampler] = nullptr;
3746 // [OpenGL ES 2.0.24] section 4.4 page 112:
3747 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3748 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3749 // image was attached in the currently bound framebuffer.
3751 Framebuffer *readFramebuffer = getReadFramebuffer();
3752 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3756 readFramebuffer->detachTexture(texture);
3759 if(drawFramebuffer && drawFramebuffer != readFramebuffer)
3761 drawFramebuffer->detachTexture(texture);
3765 void Context::detachFramebuffer(GLuint framebuffer)
3767 // [OpenGL ES 2.0.24] section 4.4 page 107:
3768 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3769 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3771 if(mState.readFramebuffer == framebuffer)
3773 bindReadFramebuffer(0);
3776 if(mState.drawFramebuffer == framebuffer)
3778 bindDrawFramebuffer(0);
3782 void Context::detachRenderbuffer(GLuint renderbuffer)
3784 // [OpenGL ES 2.0.24] section 4.4 page 109:
3785 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3786 // had been executed with the target RENDERBUFFER and name of zero.
3788 if(mState.renderbuffer.name() == renderbuffer)
3790 bindRenderbuffer(0);
3793 // [OpenGL ES 2.0.24] section 4.4 page 111:
3794 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3795 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3796 // point to which this image was attached in the currently bound framebuffer.
3798 Framebuffer *readFramebuffer = getReadFramebuffer();
3799 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3803 readFramebuffer->detachRenderbuffer(renderbuffer);
3806 if(drawFramebuffer && drawFramebuffer != readFramebuffer)
3808 drawFramebuffer->detachRenderbuffer(renderbuffer);
3812 void Context::detachSampler(GLuint sampler)
3814 // [OpenGL ES 3.0.2] section 3.8.2 pages 123-124:
3815 // If a sampler object that is currently bound to one or more texture units is
3816 // deleted, it is as though BindSampler is called once for each texture unit to
3817 // which the sampler is bound, with unit set to the texture unit and sampler set to zero.
3818 for(size_t textureUnit = 0; textureUnit < MAX_COMBINED_TEXTURE_IMAGE_UNITS; ++textureUnit)
3820 gl::BindingPointer<Sampler> &samplerBinding = mState.sampler[textureUnit];
3821 if(samplerBinding.name() == sampler)
3823 samplerBinding = nullptr;
3828 bool Context::cullSkipsDraw(GLenum drawMode)
3830 return mState.cullFaceEnabled && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
3833 bool Context::isTriangleMode(GLenum drawMode)
3838 case GL_TRIANGLE_FAN:
3839 case GL_TRIANGLE_STRIP:
3846 default: UNREACHABLE(drawMode);
3852 void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3854 ASSERT(index < MAX_VERTEX_ATTRIBS);
3856 mState.vertexAttribute[index].setCurrentValue(values);
3858 mVertexDataManager->dirtyCurrentValue(index);
3861 void Context::setVertexAttrib(GLuint index, const GLint *values)
3863 ASSERT(index < MAX_VERTEX_ATTRIBS);
3865 mState.vertexAttribute[index].setCurrentValue(values);
3867 mVertexDataManager->dirtyCurrentValue(index);
3870 void Context::setVertexAttrib(GLuint index, const GLuint *values)
3872 ASSERT(index < MAX_VERTEX_ATTRIBS);
3874 mState.vertexAttribute[index].setCurrentValue(values);
3876 mVertexDataManager->dirtyCurrentValue(index);
3879 void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3880 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3881 GLbitfield mask, bool filter, bool allowPartialDepthStencilBlit)
3883 Framebuffer *readFramebuffer = getReadFramebuffer();
3884 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3886 int readBufferWidth, readBufferHeight, readBufferSamples;
3887 int drawBufferWidth, drawBufferHeight, drawBufferSamples;
3889 if(!readFramebuffer || readFramebuffer->completeness(readBufferWidth, readBufferHeight, readBufferSamples) != GL_FRAMEBUFFER_COMPLETE ||
3890 !drawFramebuffer || drawFramebuffer->completeness(drawBufferWidth, drawBufferHeight, drawBufferSamples) != GL_FRAMEBUFFER_COMPLETE)
3892 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3895 if(drawBufferSamples > 1)
3897 return error(GL_INVALID_OPERATION);
3900 sw::SliceRect sourceRect;
3901 sw::SliceRect destRect;
3902 bool flipX = (srcX0 < srcX1) ^ (dstX0 < dstX1);
3903 bool flipy = (srcY0 < srcY1) ^ (dstY0 < dstY1);
3907 sourceRect.x0 = srcX0;
3908 sourceRect.x1 = srcX1;
3912 sourceRect.x0 = srcX1;
3913 sourceRect.x1 = srcX0;
3918 destRect.x0 = dstX0;
3919 destRect.x1 = dstX1;
3923 destRect.x0 = dstX1;
3924 destRect.x1 = dstX0;
3929 sourceRect.y0 = srcY0;
3930 sourceRect.y1 = srcY1;
3934 sourceRect.y0 = srcY1;
3935 sourceRect.y1 = srcY0;
3940 destRect.y0 = dstY0;
3941 destRect.y1 = dstY1;
3945 destRect.y0 = dstY1;
3946 destRect.y1 = dstY0;
3949 sw::Rect sourceScissoredRect = sourceRect;
3950 sw::Rect destScissoredRect = destRect;
3952 if(mState.scissorTestEnabled) // Only write to parts of the destination framebuffer which pass the scissor test
3954 if(destRect.x0 < mState.scissorX)
3956 int xDiff = mState.scissorX - destRect.x0;
3957 destScissoredRect.x0 = mState.scissorX;
3958 sourceScissoredRect.x0 += xDiff;
3961 if(destRect.x1 > mState.scissorX + mState.scissorWidth)
3963 int xDiff = destRect.x1 - (mState.scissorX + mState.scissorWidth);
3964 destScissoredRect.x1 = mState.scissorX + mState.scissorWidth;
3965 sourceScissoredRect.x1 -= xDiff;
3968 if(destRect.y0 < mState.scissorY)
3970 int yDiff = mState.scissorY - destRect.y0;
3971 destScissoredRect.y0 = mState.scissorY;
3972 sourceScissoredRect.y0 += yDiff;
3975 if(destRect.y1 > mState.scissorY + mState.scissorHeight)
3977 int yDiff = destRect.y1 - (mState.scissorY + mState.scissorHeight);
3978 destScissoredRect.y1 = mState.scissorY + mState.scissorHeight;
3979 sourceScissoredRect.y1 -= yDiff;
3983 sw::Rect sourceTrimmedRect = sourceScissoredRect;
3984 sw::Rect destTrimmedRect = destScissoredRect;
3986 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3987 // the actual draw and read surfaces.
3988 if(sourceTrimmedRect.x0 < 0)
3990 int xDiff = 0 - sourceTrimmedRect.x0;
3991 sourceTrimmedRect.x0 = 0;
3992 destTrimmedRect.x0 += xDiff;
3995 if(sourceTrimmedRect.x1 > readBufferWidth)
3997 int xDiff = sourceTrimmedRect.x1 - readBufferWidth;
3998 sourceTrimmedRect.x1 = readBufferWidth;
3999 destTrimmedRect.x1 -= xDiff;
4002 if(sourceTrimmedRect.y0 < 0)
4004 int yDiff = 0 - sourceTrimmedRect.y0;
4005 sourceTrimmedRect.y0 = 0;
4006 destTrimmedRect.y0 += yDiff;
4009 if(sourceTrimmedRect.y1 > readBufferHeight)
4011 int yDiff = sourceTrimmedRect.y1 - readBufferHeight;
4012 sourceTrimmedRect.y1 = readBufferHeight;
4013 destTrimmedRect.y1 -= yDiff;
4016 if(destTrimmedRect.x0 < 0)
4018 int xDiff = 0 - destTrimmedRect.x0;
4019 destTrimmedRect.x0 = 0;
4020 sourceTrimmedRect.x0 += xDiff;
4023 if(destTrimmedRect.x1 > drawBufferWidth)
4025 int xDiff = destTrimmedRect.x1 - drawBufferWidth;
4026 destTrimmedRect.x1 = drawBufferWidth;
4027 sourceTrimmedRect.x1 -= xDiff;
4030 if(destTrimmedRect.y0 < 0)
4032 int yDiff = 0 - destTrimmedRect.y0;
4033 destTrimmedRect.y0 = 0;
4034 sourceTrimmedRect.y0 += yDiff;
4037 if(destTrimmedRect.y1 > drawBufferHeight)
4039 int yDiff = destTrimmedRect.y1 - drawBufferHeight;
4040 destTrimmedRect.y1 = drawBufferHeight;
4041 sourceTrimmedRect.y1 -= yDiff;
4044 bool partialBufferCopy = false;
4046 if(sourceTrimmedRect.y1 - sourceTrimmedRect.y0 < readBufferHeight ||
4047 sourceTrimmedRect.x1 - sourceTrimmedRect.x0 < readBufferWidth ||
4048 destTrimmedRect.y1 - destTrimmedRect.y0 < drawBufferHeight ||
4049 destTrimmedRect.x1 - destTrimmedRect.x0 < drawBufferWidth ||
4050 sourceTrimmedRect.y0 != 0 || destTrimmedRect.y0 != 0 || sourceTrimmedRect.x0 != 0 || destTrimmedRect.x0 != 0)
4052 partialBufferCopy = true;
4055 bool sameBounds = (srcX0 == dstX0 && srcY0 == dstY0 && srcX1 == dstX1 && srcY1 == dstY1);
4056 bool blitRenderTarget = false;
4057 bool blitDepth = false;
4058 bool blitStencil = false;
4060 if(mask & GL_COLOR_BUFFER_BIT)
4062 GLenum readColorbufferType = readFramebuffer->getColorbufferType(getReadFramebufferColorIndex());
4063 GLenum drawColorbufferType = drawFramebuffer->getColorbufferType(0);
4064 const bool validReadType = readColorbufferType == GL_TEXTURE_2D || Framebuffer::IsRenderbuffer(readColorbufferType);
4065 const bool validDrawType = drawColorbufferType == GL_TEXTURE_2D || Framebuffer::IsRenderbuffer(drawColorbufferType);
4066 if(!validReadType || !validDrawType)
4068 return error(GL_INVALID_OPERATION);
4071 if(partialBufferCopy && readBufferSamples > 1 && !sameBounds)
4073 return error(GL_INVALID_OPERATION);
4076 blitRenderTarget = true;
4079 if(mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
4081 Renderbuffer *readDSBuffer = nullptr;
4082 Renderbuffer *drawDSBuffer = nullptr;
4084 if(mask & GL_DEPTH_BUFFER_BIT)
4086 if(readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
4088 GLenum readDepthBufferType = readFramebuffer->getDepthbufferType();
4089 GLenum drawDepthBufferType = drawFramebuffer->getDepthbufferType();
4090 if((readDepthBufferType != drawDepthBufferType) &&
4091 !(Framebuffer::IsRenderbuffer(readDepthBufferType) && Framebuffer::IsRenderbuffer(drawDepthBufferType)))
4093 return error(GL_INVALID_OPERATION);
4097 readDSBuffer = readFramebuffer->getDepthbuffer();
4098 drawDSBuffer = drawFramebuffer->getDepthbuffer();
4102 if(mask & GL_STENCIL_BUFFER_BIT)
4104 if(readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
4106 GLenum readStencilBufferType = readFramebuffer->getStencilbufferType();
4107 GLenum drawStencilBufferType = drawFramebuffer->getStencilbufferType();
4108 if((readStencilBufferType != drawStencilBufferType) &&
4109 !(Framebuffer::IsRenderbuffer(readStencilBufferType) && Framebuffer::IsRenderbuffer(drawStencilBufferType)))
4111 return error(GL_INVALID_OPERATION);
4115 readDSBuffer = readFramebuffer->getStencilbuffer();
4116 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4120 if(partialBufferCopy && !allowPartialDepthStencilBlit)
4122 ERR("Only whole-buffer depth and stencil blits are supported by ANGLE_framebuffer_blit.");
4123 return error(GL_INVALID_OPERATION); // Only whole-buffer copies are permitted
4126 // OpenGL ES 3.0.4 spec, p.199:
4127 // ...an INVALID_OPERATION error is generated if the formats of the read
4128 // and draw framebuffers are not identical or if the source and destination
4129 // rectangles are not defined with the same(X0, Y 0) and (X1, Y 1) bounds.
4130 // If SAMPLE_BUFFERS for the draw framebuffer is greater than zero, an
4131 // INVALID_OPERATION error is generated.
4132 if((drawDSBuffer && drawDSBuffer->getSamples() > 1) ||
4133 ((readDSBuffer && readDSBuffer->getSamples() > 1) &&
4134 (!sameBounds || (drawDSBuffer->getFormat() != readDSBuffer->getFormat()))))
4136 return error(GL_INVALID_OPERATION);
4140 if(blitRenderTarget || blitDepth || blitStencil)
4142 if(blitRenderTarget)
4144 egl::Image *readRenderTarget = readFramebuffer->getReadRenderTarget();
4145 egl::Image *drawRenderTarget = drawFramebuffer->getRenderTarget(0);
4149 swap(destRect.x0, destRect.x1);
4153 swap(destRect.y0, destRect.y1);
4156 bool success = device->stretchRect(readRenderTarget, &sourceRect, drawRenderTarget, &destRect, (filter ? Device::USE_FILTER : 0) | Device::COLOR_BUFFER);
4158 readRenderTarget->release();
4159 drawRenderTarget->release();
4163 ERR("BlitFramebuffer failed.");
4170 egl::Image *readRenderTarget = readFramebuffer->getDepthBuffer();
4171 egl::Image *drawRenderTarget = drawFramebuffer->getDepthBuffer();
4173 bool success = device->stretchRect(readRenderTarget, &sourceRect, drawRenderTarget, &destRect, (filter ? Device::USE_FILTER : 0) | Device::DEPTH_BUFFER);
4175 readRenderTarget->release();
4176 drawRenderTarget->release();
4180 ERR("BlitFramebuffer failed.");
4187 egl::Image *readRenderTarget = readFramebuffer->getStencilBuffer();
4188 egl::Image *drawRenderTarget = drawFramebuffer->getStencilBuffer();
4190 bool success = device->stretchRect(readRenderTarget, &sourceRect, drawRenderTarget, &destRect, (filter ? Device::USE_FILTER : 0) | Device::STENCIL_BUFFER);
4192 readRenderTarget->release();
4193 drawRenderTarget->release();
4197 ERR("BlitFramebuffer failed.");
4204 void Context::bindTexImage(egl::Surface *surface)
4206 es2::Texture2D *textureObject = getTexture2D();
4210 textureObject->bindTexImage(surface);
4214 EGLenum Context::validateSharedImage(EGLenum target, GLuint name, GLuint textureLevel)
4216 GLenum textureTarget = GL_NONE;
4220 case EGL_GL_TEXTURE_2D_KHR:
4221 textureTarget = GL_TEXTURE_2D;
4223 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
4224 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
4225 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
4226 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
4227 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
4228 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
4229 textureTarget = GL_TEXTURE_CUBE_MAP;
4231 case EGL_GL_RENDERBUFFER_KHR:
4234 return EGL_BAD_PARAMETER;
4237 if(textureLevel >= es2::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
4239 return EGL_BAD_MATCH;
4242 if(textureTarget != GL_NONE)
4244 es2::Texture *texture = getTexture(name);
4246 if(!texture || texture->getTarget() != textureTarget)
4248 return EGL_BAD_PARAMETER;
4251 if(texture->isShared(textureTarget, textureLevel)) // Bound to an EGLSurface or already an EGLImage sibling
4253 return EGL_BAD_ACCESS;
4256 if(textureLevel != 0 && !texture->isSamplerComplete())
4258 return EGL_BAD_PARAMETER;
4261 if(textureLevel == 0 && !(texture->isSamplerComplete() && texture->getLevelCount() == 1))
4263 return EGL_BAD_PARAMETER;
4266 else if(target == EGL_GL_RENDERBUFFER_KHR)
4268 es2::Renderbuffer *renderbuffer = getRenderbuffer(name);
4272 return EGL_BAD_PARAMETER;
4275 if(renderbuffer->isShared()) // Already an EGLImage sibling
4277 return EGL_BAD_ACCESS;
4280 else UNREACHABLE(target);
4285 egl::Image *Context::createSharedImage(EGLenum target, GLuint name, GLuint textureLevel)
4287 GLenum textureTarget = GL_NONE;
4291 case EGL_GL_TEXTURE_2D_KHR: textureTarget = GL_TEXTURE_2D; break;
4292 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR: textureTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X; break;
4293 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR: textureTarget = GL_TEXTURE_CUBE_MAP_NEGATIVE_X; break;
4294 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR: textureTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_Y; break;
4295 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR: textureTarget = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y; break;
4296 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR: textureTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_Z; break;
4297 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR: textureTarget = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; break;
4300 if(textureTarget != GL_NONE)
4302 es2::Texture *texture = getTexture(name);
4304 return texture->createSharedImage(textureTarget, textureLevel);
4306 else if(target == EGL_GL_RENDERBUFFER_KHR)
4308 es2::Renderbuffer *renderbuffer = getRenderbuffer(name);
4310 return renderbuffer->createSharedImage();
4312 else UNREACHABLE(target);
4317 egl::Image *Context::getSharedImage(GLeglImageOES image)
4319 return display->getSharedImage(image);
4322 Device *Context::getDevice()
4327 const GLubyte* Context::getExtensions(GLuint index, GLuint* numExt) const
4329 // Keep list sorted in following order:
4332 // Vendor extensions
4333 static const GLubyte* extensions[] = {
4334 (const GLubyte*)"GL_OES_compressed_ETC1_RGB8_texture",
4335 (const GLubyte*)"GL_OES_depth24",
4336 (const GLubyte*)"GL_OES_depth32",
4337 (const GLubyte*)"GL_OES_depth_texture",
4338 (const GLubyte*)"GL_OES_depth_texture_cube_map",
4339 (const GLubyte*)"GL_OES_EGL_image",
4340 (const GLubyte*)"GL_OES_EGL_image_external",
4341 (const GLubyte*)"GL_OES_EGL_sync",
4342 (const GLubyte*)"GL_OES_element_index_uint",
4343 (const GLubyte*)"GL_OES_framebuffer_object",
4344 (const GLubyte*)"GL_OES_packed_depth_stencil",
4345 (const GLubyte*)"GL_OES_rgb8_rgba8",
4346 (const GLubyte*)"GL_OES_standard_derivatives",
4347 (const GLubyte*)"GL_OES_texture_float",
4348 (const GLubyte*)"GL_OES_texture_float_linear",
4349 (const GLubyte*)"GL_OES_texture_half_float",
4350 (const GLubyte*)"GL_OES_texture_half_float_linear",
4351 (const GLubyte*)"GL_OES_texture_npot",
4352 (const GLubyte*)"GL_OES_texture_3D",
4353 (const GLubyte*)"GL_EXT_blend_minmax",
4354 (const GLubyte*)"GL_EXT_color_buffer_half_float",
4355 (const GLubyte*)"GL_EXT_draw_buffers",
4356 (const GLubyte*)"GL_EXT_occlusion_query_boolean",
4357 (const GLubyte*)"GL_EXT_read_format_bgra",
4359 (const GLubyte*)"GL_EXT_texture_compression_dxt1",
4361 (const GLubyte*)"GL_EXT_texture_filter_anisotropic",
4362 (const GLubyte*)"GL_EXT_texture_format_BGRA8888",
4363 (const GLubyte*)"GL_ANGLE_framebuffer_blit",
4364 (const GLubyte*)"GL_NV_framebuffer_blit",
4365 (const GLubyte*)"GL_ANGLE_framebuffer_multisample",
4367 (const GLubyte*)"GL_ANGLE_texture_compression_dxt3",
4368 (const GLubyte*)"GL_ANGLE_texture_compression_dxt5",
4370 (const GLubyte*)"GL_NV_fence",
4371 (const GLubyte*)"GL_NV_read_depth",
4372 (const GLubyte*)"GL_EXT_instanced_arrays",
4373 (const GLubyte*)"GL_ANGLE_instanced_arrays",
4375 static const GLuint numExtensions = sizeof(extensions) / sizeof(*extensions);
4379 *numExt = numExtensions;
4383 if(index == GL_INVALID_INDEX)
4385 static GLubyte* extensionsCat = nullptr;
4386 if(!extensionsCat && (numExtensions > 0))
4388 size_t totalLength = numExtensions; // 1 space between each extension name + terminating null
4389 for(unsigned int i = 0; i < numExtensions; i++)
4391 totalLength += strlen(reinterpret_cast<const char*>(extensions[i]));
4393 extensionsCat = new GLubyte[totalLength];
4394 extensionsCat[0] = '\0';
4395 for(unsigned int i = 0; i < numExtensions; i++)
4399 strcat(reinterpret_cast<char*>(extensionsCat), " ");
4401 strcat(reinterpret_cast<char*>(extensionsCat), reinterpret_cast<const char*>(extensions[i]));
4404 return extensionsCat;
4407 if(index >= numExtensions)
4412 return extensions[index];
4417 egl::Context *es2CreateContext(egl::Display *display, const egl::Context *shareContext, int clientVersion)
4419 ASSERT(!shareContext || shareContext->getClientVersion() == clientVersion); // Should be checked by eglCreateContext
4420 return new es2::Context(display, static_cast<const es2::Context*>(shareContext), clientVersion);