2 // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
7 // Context.cpp: Implements the gl::Context class, managing all GL state and performing
8 // rendering operations. It is the GLES2 specific implementation of EGLContext.
10 #include "libGLESv2/Context.h"
12 #include "libGLESv2/main.h"
13 #include "common/utilities.h"
14 #include "common/platform.h"
15 #include "libGLESv2/formatutils.h"
16 #include "libGLESv2/Buffer.h"
17 #include "libGLESv2/Fence.h"
18 #include "libGLESv2/Framebuffer.h"
19 #include "libGLESv2/FramebufferAttachment.h"
20 #include "libGLESv2/Renderbuffer.h"
21 #include "libGLESv2/Program.h"
22 #include "libGLESv2/ProgramBinary.h"
23 #include "libGLESv2/Query.h"
24 #include "libGLESv2/Texture.h"
25 #include "libGLESv2/ResourceManager.h"
26 #include "libGLESv2/renderer/d3d/IndexDataManager.h"
27 #include "libGLESv2/renderer/Renderer.h"
28 #include "libGLESv2/VertexArray.h"
29 #include "libGLESv2/Sampler.h"
30 #include "libGLESv2/validationES.h"
31 #include "libGLESv2/TransformFeedback.h"
33 #include "libEGL/Surface.h"
40 Context::Context(int clientVersion, const gl::Context *shareContext, rx::Renderer *renderer, bool notifyResets, bool robustAccess)
43 ASSERT(robustAccess == false); // Unimplemented
45 initCaps(clientVersion);
46 mState.initialize(mCaps, clientVersion);
48 mClientVersion = clientVersion;
50 mFenceNVHandleAllocator.setBaseHandle(0);
52 if (shareContext != NULL)
54 mResourceManager = shareContext->mResourceManager;
55 mResourceManager->addRef();
59 mResourceManager = new ResourceManager(mRenderer);
62 // [OpenGL ES 2.0.24] section 3.7 page 83:
63 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
64 // and cube map texture state vectors respectively associated with them.
65 // In order that access to these initial textures not be lost, they are treated as texture
66 // objects all of whose names are 0.
68 mZeroTextures[GL_TEXTURE_2D].set(new Texture2D(mRenderer->createTexture(GL_TEXTURE_2D), 0));
69 bindTexture(GL_TEXTURE_2D, 0);
71 mZeroTextures[GL_TEXTURE_CUBE_MAP].set(new TextureCubeMap(mRenderer->createTexture(GL_TEXTURE_CUBE_MAP), 0));
72 bindTexture(GL_TEXTURE_CUBE_MAP, 0);
74 if (mClientVersion >= 3)
76 // TODO: These could also be enabled via extension
77 mZeroTextures[GL_TEXTURE_3D].set(new Texture3D(mRenderer->createTexture(GL_TEXTURE_3D), 0));
78 bindTexture(GL_TEXTURE_3D, 0);
80 mZeroTextures[GL_TEXTURE_2D_ARRAY].set(new Texture2DArray(mRenderer->createTexture(GL_TEXTURE_2D_ARRAY), 0));
81 bindTexture(GL_TEXTURE_2D_ARRAY, 0);
86 bindElementArrayBuffer(0);
88 bindReadFramebuffer(0);
89 bindDrawFramebuffer(0);
92 bindGenericUniformBuffer(0);
93 for (int i = 0; i < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; i++)
95 bindIndexedUniformBuffer(0, i, 0, -1);
98 bindGenericTransformFeedbackBuffer(0);
99 for (int i = 0; i < IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
101 bindIndexedTransformFeedbackBuffer(0, i, 0, -1);
104 bindCopyReadBuffer(0);
105 bindCopyWriteBuffer(0);
106 bindPixelPackBuffer(0);
107 bindPixelUnpackBuffer(0);
109 // [OpenGL ES 3.0.2] section 2.14.1 pg 85:
110 // In the initial state, a default transform feedback object is bound and treated as
111 // a transform feedback object with a name of zero. That object is bound any time
112 // BindTransformFeedback is called with id of zero
113 mTransformFeedbackZero.set(new TransformFeedback(mRenderer->createTransformFeedback(), 0));
114 bindTransformFeedback(0);
116 mHasBeenCurrent = false;
117 mContextLost = false;
118 mResetStatus = GL_NO_ERROR;
119 mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
120 mRobustAccess = robustAccess;
122 mState.setContext(this);
127 GLuint currentProgram = mState.getCurrentProgramId();
128 if (currentProgram != 0)
130 Program *programObject = mResourceManager->getProgram(currentProgram);
133 programObject->release();
137 mState.setCurrentProgram(0, NULL);
139 while (!mFramebufferMap.empty())
141 deleteFramebuffer(mFramebufferMap.begin()->first);
144 while (!mFenceNVMap.empty())
146 deleteFenceNV(mFenceNVMap.begin()->first);
149 while (!mQueryMap.empty())
151 deleteQuery(mQueryMap.begin()->first);
154 while (!mVertexArrayMap.empty())
156 deleteVertexArray(mVertexArrayMap.begin()->first);
159 mTransformFeedbackZero.set(NULL);
160 while (!mTransformFeedbackMap.empty())
162 deleteTransformFeedback(mTransformFeedbackMap.begin()->first);
165 for (TextureMap::iterator i = mIncompleteTextures.begin(); i != mIncompleteTextures.end(); i++)
169 mIncompleteTextures.clear();
171 for (TextureMap::iterator i = mZeroTextures.begin(); i != mZeroTextures.end(); i++)
175 mZeroTextures.clear();
177 mResourceManager->release();
180 void Context::makeCurrent(egl::Surface *surface)
182 if (!mHasBeenCurrent)
184 initRendererString();
185 initExtensionStrings();
187 mState.setViewportParams(0, 0, surface->getWidth(), surface->getHeight());
188 mState.setScissorParams(0, 0, surface->getWidth(), surface->getHeight());
190 mHasBeenCurrent = true;
193 // Wrap the existing swapchain resources into GL objects and assign them to the '0' names
194 rx::SwapChain *swapchain = surface->getSwapChain();
196 Colorbuffer *colorbufferZero = new Colorbuffer(mRenderer, swapchain);
197 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(mRenderer, swapchain);
198 Framebuffer *framebufferZero = new DefaultFramebuffer(mRenderer, colorbufferZero, depthStencilbufferZero);
200 setFramebufferZero(framebufferZero);
202 // Store the current client version in the renderer
203 mRenderer->setCurrentClientVersion(mClientVersion);
206 // NOTE: this function should not assume that this context is current!
207 void Context::markContextLost()
209 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
210 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
214 bool Context::isContextLost()
219 GLuint Context::createBuffer()
221 return mResourceManager->createBuffer();
224 GLuint Context::createProgram()
226 return mResourceManager->createProgram();
229 GLuint Context::createShader(GLenum type)
231 return mResourceManager->createShader(type);
234 GLuint Context::createTexture()
236 return mResourceManager->createTexture();
239 GLuint Context::createRenderbuffer()
241 return mResourceManager->createRenderbuffer();
244 GLsync Context::createFenceSync(GLenum condition)
246 GLuint handle = mResourceManager->createFenceSync();
248 gl::FenceSync *fenceSync = mResourceManager->getFenceSync(handle);
251 fenceSync->set(condition);
253 return reinterpret_cast<GLsync>(handle);
256 GLuint Context::createVertexArray()
258 GLuint handle = mVertexArrayHandleAllocator.allocate();
260 // Although the spec states VAO state is not initialized until the object is bound,
261 // we create it immediately. The resulting behaviour is transparent to the application,
262 // since it's not currently possible to access the state until the object is bound.
263 VertexArray *vertexArray = new VertexArray(mRenderer->createVertexArray(), handle, MAX_VERTEX_ATTRIBS);
264 mVertexArrayMap[handle] = vertexArray;
268 GLuint Context::createSampler()
270 return mResourceManager->createSampler();
273 GLuint Context::createTransformFeedback()
275 GLuint handle = mTransformFeedbackAllocator.allocate();
276 TransformFeedback *transformFeedback = new TransformFeedback(mRenderer->createTransformFeedback(), handle);
277 transformFeedback->addRef();
278 mTransformFeedbackMap[handle] = transformFeedback;
282 // Returns an unused framebuffer name
283 GLuint Context::createFramebuffer()
285 GLuint handle = mFramebufferHandleAllocator.allocate();
287 mFramebufferMap[handle] = NULL;
292 GLuint Context::createFenceNV()
294 GLuint handle = mFenceNVHandleAllocator.allocate();
296 mFenceNVMap[handle] = new FenceNV(mRenderer);
301 // Returns an unused query name
302 GLuint Context::createQuery()
304 GLuint handle = mQueryHandleAllocator.allocate();
306 mQueryMap[handle] = NULL;
311 void Context::deleteBuffer(GLuint buffer)
313 if (mResourceManager->getBuffer(buffer))
315 detachBuffer(buffer);
318 mResourceManager->deleteBuffer(buffer);
321 void Context::deleteShader(GLuint shader)
323 mResourceManager->deleteShader(shader);
326 void Context::deleteProgram(GLuint program)
328 mResourceManager->deleteProgram(program);
331 void Context::deleteTexture(GLuint texture)
333 if (mResourceManager->getTexture(texture))
335 detachTexture(texture);
338 mResourceManager->deleteTexture(texture);
341 void Context::deleteRenderbuffer(GLuint renderbuffer)
343 if (mResourceManager->getRenderbuffer(renderbuffer))
345 detachRenderbuffer(renderbuffer);
348 mResourceManager->deleteRenderbuffer(renderbuffer);
351 void Context::deleteFenceSync(GLsync fenceSync)
353 // The spec specifies the underlying Fence object is not deleted until all current
354 // wait commands finish. However, since the name becomes invalid, we cannot query the fence,
355 // and since our API is currently designed for being called from a single thread, we can delete
356 // the fence immediately.
357 mResourceManager->deleteFenceSync(reinterpret_cast<GLuint>(fenceSync));
360 void Context::deleteVertexArray(GLuint vertexArray)
362 auto vertexArrayObject = mVertexArrayMap.find(vertexArray);
364 if (vertexArrayObject != mVertexArrayMap.end())
366 detachVertexArray(vertexArray);
368 mVertexArrayHandleAllocator.release(vertexArrayObject->first);
369 delete vertexArrayObject->second;
370 mVertexArrayMap.erase(vertexArrayObject);
374 void Context::deleteSampler(GLuint sampler)
376 if (mResourceManager->getSampler(sampler))
378 detachSampler(sampler);
381 mResourceManager->deleteSampler(sampler);
384 void Context::deleteTransformFeedback(GLuint transformFeedback)
386 TransformFeedbackMap::const_iterator iter = mTransformFeedbackMap.find(transformFeedback);
387 if (iter != mTransformFeedbackMap.end())
389 detachTransformFeedback(transformFeedback);
390 mTransformFeedbackAllocator.release(transformFeedback);
391 iter->second->release();
392 mTransformFeedbackMap.erase(iter);
396 void Context::deleteFramebuffer(GLuint framebuffer)
398 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
400 if (framebufferObject != mFramebufferMap.end())
402 detachFramebuffer(framebuffer);
404 mFramebufferHandleAllocator.release(framebufferObject->first);
405 delete framebufferObject->second;
406 mFramebufferMap.erase(framebufferObject);
410 void Context::deleteFenceNV(GLuint fence)
412 FenceNVMap::iterator fenceObject = mFenceNVMap.find(fence);
414 if (fenceObject != mFenceNVMap.end())
416 mFenceNVHandleAllocator.release(fenceObject->first);
417 delete fenceObject->second;
418 mFenceNVMap.erase(fenceObject);
422 void Context::deleteQuery(GLuint query)
424 QueryMap::iterator queryObject = mQueryMap.find(query);
425 if (queryObject != mQueryMap.end())
427 mQueryHandleAllocator.release(queryObject->first);
428 if (queryObject->second)
430 queryObject->second->release();
432 mQueryMap.erase(queryObject);
436 Buffer *Context::getBuffer(GLuint handle)
438 return mResourceManager->getBuffer(handle);
441 Shader *Context::getShader(GLuint handle) const
443 return mResourceManager->getShader(handle);
446 Program *Context::getProgram(GLuint handle) const
448 return mResourceManager->getProgram(handle);
451 Texture *Context::getTexture(GLuint handle) const
453 return mResourceManager->getTexture(handle);
456 Renderbuffer *Context::getRenderbuffer(GLuint handle)
458 return mResourceManager->getRenderbuffer(handle);
461 FenceSync *Context::getFenceSync(GLsync handle) const
463 return mResourceManager->getFenceSync(reinterpret_cast<GLuint>(handle));
466 VertexArray *Context::getVertexArray(GLuint handle) const
468 auto vertexArray = mVertexArrayMap.find(handle);
470 if (vertexArray == mVertexArrayMap.end())
476 return vertexArray->second;
480 Sampler *Context::getSampler(GLuint handle) const
482 return mResourceManager->getSampler(handle);
485 TransformFeedback *Context::getTransformFeedback(GLuint handle) const
489 return mTransformFeedbackZero.get();
493 TransformFeedbackMap::const_iterator iter = mTransformFeedbackMap.find(handle);
494 return (iter != mTransformFeedbackMap.end()) ? iter->second : NULL;
498 bool Context::isSampler(GLuint samplerName) const
500 return mResourceManager->isSampler(samplerName);
503 void Context::bindArrayBuffer(unsigned int buffer)
505 mResourceManager->checkBufferAllocation(buffer);
507 mState.setArrayBufferBinding(getBuffer(buffer));
510 void Context::bindElementArrayBuffer(unsigned int buffer)
512 mResourceManager->checkBufferAllocation(buffer);
514 mState.getVertexArray()->setElementArrayBuffer(getBuffer(buffer));
517 void Context::bindTexture(GLenum target, GLuint texture)
519 mResourceManager->checkTextureAllocation(texture, target);
521 mState.setSamplerTexture(target, getTexture(texture));
524 void Context::bindReadFramebuffer(GLuint framebuffer)
526 if (!getFramebuffer(framebuffer))
528 mFramebufferMap[framebuffer] = new Framebuffer(mRenderer, framebuffer);
531 mState.setReadFramebufferBinding(getFramebuffer(framebuffer));
534 void Context::bindDrawFramebuffer(GLuint framebuffer)
536 if (!getFramebuffer(framebuffer))
538 mFramebufferMap[framebuffer] = new Framebuffer(mRenderer, framebuffer);
541 mState.setDrawFramebufferBinding(getFramebuffer(framebuffer));
544 void Context::bindRenderbuffer(GLuint renderbuffer)
546 mResourceManager->checkRenderbufferAllocation(renderbuffer);
548 mState.setRenderbufferBinding(getRenderbuffer(renderbuffer));
551 void Context::bindVertexArray(GLuint vertexArray)
553 if (!getVertexArray(vertexArray))
555 VertexArray *vertexArrayObject = new VertexArray(mRenderer->createVertexArray(), vertexArray, MAX_VERTEX_ATTRIBS);
556 mVertexArrayMap[vertexArray] = vertexArrayObject;
559 mState.setVertexArrayBinding(getVertexArray(vertexArray));
562 void Context::bindSampler(GLuint textureUnit, GLuint sampler)
564 ASSERT(textureUnit < mCaps.maxCombinedTextureImageUnits);
565 mResourceManager->checkSamplerAllocation(sampler);
567 mState.setSamplerBinding(textureUnit, getSampler(sampler));
570 void Context::bindGenericUniformBuffer(GLuint buffer)
572 mResourceManager->checkBufferAllocation(buffer);
574 mState.setGenericUniformBufferBinding(getBuffer(buffer));
577 void Context::bindIndexedUniformBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size)
579 mResourceManager->checkBufferAllocation(buffer);
581 mState.setIndexedUniformBufferBinding(index, getBuffer(buffer), offset, size);
584 void Context::bindGenericTransformFeedbackBuffer(GLuint buffer)
586 mResourceManager->checkBufferAllocation(buffer);
588 mState.setGenericTransformFeedbackBufferBinding(getBuffer(buffer));
591 void Context::bindIndexedTransformFeedbackBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size)
593 mResourceManager->checkBufferAllocation(buffer);
595 mState.setIndexedTransformFeedbackBufferBinding(index, getBuffer(buffer), offset, size);
598 void Context::bindCopyReadBuffer(GLuint buffer)
600 mResourceManager->checkBufferAllocation(buffer);
602 mState.setCopyReadBufferBinding(getBuffer(buffer));
605 void Context::bindCopyWriteBuffer(GLuint buffer)
607 mResourceManager->checkBufferAllocation(buffer);
609 mState.setCopyWriteBufferBinding(getBuffer(buffer));
612 void Context::bindPixelPackBuffer(GLuint buffer)
614 mResourceManager->checkBufferAllocation(buffer);
616 mState.setPixelPackBufferBinding(getBuffer(buffer));
619 void Context::bindPixelUnpackBuffer(GLuint buffer)
621 mResourceManager->checkBufferAllocation(buffer);
623 mState.setPixelUnpackBufferBinding(getBuffer(buffer));
626 void Context::useProgram(GLuint program)
628 GLuint priorProgramId = mState.getCurrentProgramId();
629 Program *priorProgram = mResourceManager->getProgram(priorProgramId);
631 if (priorProgramId != program)
633 mState.setCurrentProgram(program, mResourceManager->getProgram(program));
637 priorProgram->release();
642 void Context::linkProgram(GLuint program)
644 Program *programObject = mResourceManager->getProgram(program);
646 bool linked = programObject->link(getCaps());
648 // if the current program was relinked successfully we
649 // need to install the new executables
650 if (linked && program == mState.getCurrentProgramId())
652 mState.setCurrentProgramBinary(programObject->getProgramBinary());
656 void Context::setProgramBinary(GLuint program, GLenum binaryFormat, const void *binary, GLint length)
658 Program *programObject = mResourceManager->getProgram(program);
660 bool loaded = programObject->setProgramBinary(binaryFormat, binary, length);
662 // if the current program was reloaded successfully we
663 // need to install the new executables
664 if (loaded && program == mState.getCurrentProgramId())
666 mState.setCurrentProgramBinary(programObject->getProgramBinary());
671 void Context::bindTransformFeedback(GLuint transformFeedback)
673 mState.setTransformFeedbackBinding(getTransformFeedback(transformFeedback));
676 Error Context::beginQuery(GLenum target, GLuint query)
678 Query *queryObject = getQuery(query, true, target);
682 Error error = queryObject->begin();
688 // set query as active for specified target only if begin succeeded
689 mState.setActiveQuery(target, queryObject);
691 return Error(GL_NO_ERROR);
694 Error Context::endQuery(GLenum target)
696 Query *queryObject = mState.getActiveQuery(target);
699 gl::Error error = queryObject->end();
701 // Always unbind the query, even if there was an error. This may delete the query object.
702 mState.setActiveQuery(target, NULL);
707 void Context::setFramebufferZero(Framebuffer *buffer)
709 // First, check to see if the old default framebuffer
710 // was set for draw or read framebuffer, and change
711 // the bindings to point to the new one before deleting it.
712 if (mState.getDrawFramebuffer()->id() == 0)
714 mState.setDrawFramebufferBinding(buffer);
717 if (mState.getReadFramebuffer()->id() == 0)
719 mState.setReadFramebufferBinding(buffer);
722 delete mFramebufferMap[0];
723 mFramebufferMap[0] = buffer;
726 void Context::setRenderbufferStorage(GLsizei width, GLsizei height, GLenum internalformat, GLsizei samples)
728 ASSERT(getTextureCaps().get(internalformat).renderable);
730 RenderbufferStorage *renderbuffer = NULL;
732 const InternalFormat &formatInfo = GetInternalFormatInfo(internalformat);
733 if (formatInfo.depthBits > 0 && formatInfo.stencilBits > 0)
735 renderbuffer = new gl::DepthStencilbuffer(mRenderer, width, height, samples);
737 else if (formatInfo.depthBits > 0)
739 renderbuffer = new gl::Depthbuffer(mRenderer, width, height, samples);
741 else if (formatInfo.stencilBits > 0)
743 renderbuffer = new gl::Stencilbuffer(mRenderer, width, height, samples);
747 renderbuffer = new gl::Colorbuffer(mRenderer, width, height, internalformat, samples);
750 mState.getCurrentRenderbuffer()->setStorage(renderbuffer);
753 Framebuffer *Context::getFramebuffer(unsigned int handle) const
755 FramebufferMap::const_iterator framebuffer = mFramebufferMap.find(handle);
757 if (framebuffer == mFramebufferMap.end())
763 return framebuffer->second;
767 FenceNV *Context::getFenceNV(unsigned int handle)
769 FenceNVMap::iterator fence = mFenceNVMap.find(handle);
771 if (fence == mFenceNVMap.end())
777 return fence->second;
781 Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
783 QueryMap::iterator query = mQueryMap.find(handle);
785 if (query == mQueryMap.end())
791 if (!query->second && create)
793 query->second = new Query(mRenderer->createQuery(type), handle);
794 query->second->addRef();
796 return query->second;
800 Texture *Context::getTargetTexture(GLenum target) const
802 if (!ValidTextureTarget(this, target))
809 case GL_TEXTURE_2D: return getTexture2D();
810 case GL_TEXTURE_CUBE_MAP: return getTextureCubeMap();
811 case GL_TEXTURE_3D: return getTexture3D();
812 case GL_TEXTURE_2D_ARRAY: return getTexture2DArray();
813 default: return NULL;
817 Texture2D *Context::getTexture2D() const
819 return static_cast<Texture2D*>(getSamplerTexture(mState.getActiveSampler(), GL_TEXTURE_2D));
822 TextureCubeMap *Context::getTextureCubeMap() const
824 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.getActiveSampler(), GL_TEXTURE_CUBE_MAP));
827 Texture3D *Context::getTexture3D() const
829 return static_cast<Texture3D*>(getSamplerTexture(mState.getActiveSampler(), GL_TEXTURE_3D));
832 Texture2DArray *Context::getTexture2DArray() const
834 return static_cast<Texture2DArray*>(getSamplerTexture(mState.getActiveSampler(), GL_TEXTURE_2D_ARRAY));
837 Texture *Context::getSamplerTexture(unsigned int sampler, GLenum type) const
839 if (mState.getSamplerTextureId(sampler, type) == 0)
841 return mZeroTextures.at(type).get();
845 return mState.getSamplerTexture(sampler, type);
849 void Context::getBooleanv(GLenum pname, GLboolean *params)
853 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
854 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
856 mState.getBooleanv(pname, params);
861 void Context::getFloatv(GLenum pname, GLfloat *params)
863 // Queries about context capabilities and maximums are answered by Context.
864 // Queries about current GL state values are answered by State.
867 case GL_ALIASED_LINE_WIDTH_RANGE:
868 params[0] = mCaps.minAliasedLineWidth;
869 params[1] = mCaps.maxAliasedLineWidth;
871 case GL_ALIASED_POINT_SIZE_RANGE:
872 params[0] = mCaps.minAliasedPointSize;
873 params[1] = mCaps.maxAliasedPointSize;
875 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
876 ASSERT(mExtensions.textureFilterAnisotropic);
877 *params = mExtensions.maxTextureAnisotropy;
880 mState.getFloatv(pname, params);
885 void Context::getIntegerv(GLenum pname, GLint *params)
887 // Queries about context capabilities and maximums are answered by Context.
888 // Queries about current GL state values are answered by State.
892 case GL_MAX_VERTEX_ATTRIBS: *params = mCaps.maxVertexAttributes; break;
893 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = mCaps.maxVertexUniformVectors; break;
894 case GL_MAX_VERTEX_UNIFORM_COMPONENTS: *params = mCaps.maxVertexUniformComponents; break;
895 case GL_MAX_VARYING_VECTORS: *params = mCaps.maxVaryingVectors; break;
896 case GL_MAX_VARYING_COMPONENTS: *params = mCaps.maxVertexOutputComponents; break;
897 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = mCaps.maxCombinedTextureImageUnits; break;
898 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = mCaps.maxVertexTextureImageUnits; break;
899 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = mCaps.maxTextureImageUnits; break;
900 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = mCaps.maxFragmentUniformVectors; break;
901 case GL_MAX_FRAGMENT_UNIFORM_COMPONENTS: *params = mCaps.maxFragmentInputComponents; break;
902 case GL_MAX_RENDERBUFFER_SIZE: *params = mCaps.maxRenderbufferSize; break;
903 case GL_MAX_COLOR_ATTACHMENTS_EXT: *params = mCaps.maxColorAttachments; break;
904 case GL_MAX_DRAW_BUFFERS_EXT: *params = mCaps.maxDrawBuffers; break;
905 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
906 case GL_SUBPIXEL_BITS: *params = 4; break;
907 case GL_MAX_TEXTURE_SIZE: *params = mCaps.max2DTextureSize; break;
908 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = mCaps.maxCubeMapTextureSize; break;
909 case GL_MAX_3D_TEXTURE_SIZE: *params = mCaps.max3DTextureSize; break;
910 case GL_MAX_ARRAY_TEXTURE_LAYERS: *params = mCaps.maxArrayTextureLayers; break;
911 case GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT: *params = mCaps.uniformBufferOffsetAlignment; break;
912 case GL_MAX_UNIFORM_BUFFER_BINDINGS: *params = mCaps.maxUniformBufferBindings; break;
913 case GL_MAX_VERTEX_UNIFORM_BLOCKS: *params = mCaps.maxVertexUniformBlocks; break;
914 case GL_MAX_FRAGMENT_UNIFORM_BLOCKS: *params = mCaps.maxFragmentUniformBlocks; break;
915 case GL_MAX_COMBINED_UNIFORM_BLOCKS: *params = mCaps.maxCombinedTextureImageUnits; break;
916 case GL_MAJOR_VERSION: *params = mClientVersion; break;
917 case GL_MINOR_VERSION: *params = 0; break;
918 case GL_MAX_ELEMENTS_INDICES: *params = mCaps.maxElementsIndices; break;
919 case GL_MAX_ELEMENTS_VERTICES: *params = mCaps.maxElementsVertices; break;
920 case GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS: *params = mCaps.maxTransformFeedbackInterleavedComponents; break;
921 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS: *params = mCaps.maxTransformFeedbackSeparateAttributes; break;
922 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS: *params = mCaps.maxTransformFeedbackSeparateComponents; break;
923 case GL_NUM_COMPRESSED_TEXTURE_FORMATS: *params = mCaps.compressedTextureFormats.size(); break;
924 case GL_MAX_SAMPLES_ANGLE: *params = mExtensions.maxSamples; break;
925 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
926 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
928 GLenum internalFormat, format, type;
929 getCurrentReadFormatType(&internalFormat, &format, &type);
930 if (pname == GL_IMPLEMENTATION_COLOR_READ_FORMAT)
936 case GL_MAX_VIEWPORT_DIMS:
938 params[0] = mCaps.maxViewportWidth;
939 params[1] = mCaps.maxViewportHeight;
942 case GL_COMPRESSED_TEXTURE_FORMATS:
943 std::copy(mCaps.compressedTextureFormats.begin(), mCaps.compressedTextureFormats.end(), params);
945 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
946 *params = mResetStrategy;
948 case GL_NUM_SHADER_BINARY_FORMATS:
949 *params = mCaps.shaderBinaryFormats.size();
951 case GL_SHADER_BINARY_FORMATS:
952 std::copy(mCaps.shaderBinaryFormats.begin(), mCaps.shaderBinaryFormats.end(), params);
954 case GL_NUM_PROGRAM_BINARY_FORMATS:
955 *params = mCaps.programBinaryFormats.size();
957 case GL_PROGRAM_BINARY_FORMATS:
958 std::copy(mCaps.programBinaryFormats.begin(), mCaps.programBinaryFormats.end(), params);
960 case GL_NUM_EXTENSIONS:
961 *params = static_cast<GLint>(mExtensionStrings.size());
964 mState.getIntegerv(pname, params);
969 void Context::getInteger64v(GLenum pname, GLint64 *params)
971 // Queries about context capabilities and maximums are answered by Context.
972 // Queries about current GL state values are answered by State.
975 case GL_MAX_ELEMENT_INDEX:
976 *params = mCaps.maxElementIndex;
978 case GL_MAX_UNIFORM_BLOCK_SIZE:
979 *params = mCaps.maxUniformBlockSize;
981 case GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS:
982 *params = mCaps.maxCombinedVertexUniformComponents;
984 case GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS:
985 *params = mCaps.maxCombinedFragmentUniformComponents;
987 case GL_MAX_SERVER_WAIT_TIMEOUT:
988 *params = mCaps.maxServerWaitTimeout;
996 bool Context::getIndexedIntegerv(GLenum target, GLuint index, GLint *data)
998 // Queries about context capabilities and maximums are answered by Context.
999 // Queries about current GL state values are answered by State.
1000 // Indexed integer queries all refer to current state, so this function is a
1001 // mere passthrough.
1002 return mState.getIndexedIntegerv(target, index, data);
1005 bool Context::getIndexedInteger64v(GLenum target, GLuint index, GLint64 *data)
1007 // Queries about context capabilities and maximums are answered by Context.
1008 // Queries about current GL state values are answered by State.
1009 // Indexed integer queries all refer to current state, so this function is a
1010 // mere passthrough.
1011 return mState.getIndexedInteger64v(target, index, data);
1014 bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1016 if (pname >= GL_DRAW_BUFFER0_EXT && pname <= GL_DRAW_BUFFER15_EXT)
1023 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1024 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1025 // to the fact that it is stored internally as a float, and so would require conversion
1026 // if returned from Context::getIntegerv. Since this conversion is already implemented
1027 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1028 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1032 case GL_COMPRESSED_TEXTURE_FORMATS:
1035 *numParams = mCaps.compressedTextureFormats.size();
1038 case GL_PROGRAM_BINARY_FORMATS_OES:
1041 *numParams = mCaps.programBinaryFormats.size();
1044 case GL_SHADER_BINARY_FORMATS:
1047 *numParams = mCaps.shaderBinaryFormats.size();
1050 case GL_MAX_VERTEX_ATTRIBS:
1051 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1052 case GL_MAX_VARYING_VECTORS:
1053 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1054 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1055 case GL_MAX_TEXTURE_IMAGE_UNITS:
1056 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1057 case GL_MAX_RENDERBUFFER_SIZE:
1058 case GL_MAX_COLOR_ATTACHMENTS_EXT:
1059 case GL_MAX_DRAW_BUFFERS_EXT:
1060 case GL_NUM_SHADER_BINARY_FORMATS:
1061 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1062 case GL_ARRAY_BUFFER_BINDING:
1063 //case GL_FRAMEBUFFER_BINDING: // equivalent to DRAW_FRAMEBUFFER_BINDING_ANGLE
1064 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE:
1065 case GL_READ_FRAMEBUFFER_BINDING_ANGLE:
1066 case GL_RENDERBUFFER_BINDING:
1067 case GL_CURRENT_PROGRAM:
1068 case GL_PACK_ALIGNMENT:
1069 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
1070 case GL_UNPACK_ALIGNMENT:
1071 case GL_GENERATE_MIPMAP_HINT:
1072 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
1078 case GL_STENCIL_BITS:
1079 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1080 case GL_CULL_FACE_MODE:
1082 case GL_ACTIVE_TEXTURE:
1083 case GL_STENCIL_FUNC:
1084 case GL_STENCIL_VALUE_MASK:
1085 case GL_STENCIL_REF:
1086 case GL_STENCIL_FAIL:
1087 case GL_STENCIL_PASS_DEPTH_FAIL:
1088 case GL_STENCIL_PASS_DEPTH_PASS:
1089 case GL_STENCIL_BACK_FUNC:
1090 case GL_STENCIL_BACK_VALUE_MASK:
1091 case GL_STENCIL_BACK_REF:
1092 case GL_STENCIL_BACK_FAIL:
1093 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1094 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1096 case GL_BLEND_SRC_RGB:
1097 case GL_BLEND_SRC_ALPHA:
1098 case GL_BLEND_DST_RGB:
1099 case GL_BLEND_DST_ALPHA:
1100 case GL_BLEND_EQUATION_RGB:
1101 case GL_BLEND_EQUATION_ALPHA:
1102 case GL_STENCIL_WRITEMASK:
1103 case GL_STENCIL_BACK_WRITEMASK:
1104 case GL_STENCIL_CLEAR_VALUE:
1105 case GL_SUBPIXEL_BITS:
1106 case GL_MAX_TEXTURE_SIZE:
1107 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1108 case GL_SAMPLE_BUFFERS:
1110 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1111 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1112 case GL_TEXTURE_BINDING_2D:
1113 case GL_TEXTURE_BINDING_CUBE_MAP:
1114 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1115 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1121 case GL_MAX_SAMPLES_ANGLE:
1123 if (mExtensions.framebufferMultisample)
1134 case GL_PIXEL_PACK_BUFFER_BINDING:
1135 case GL_PIXEL_UNPACK_BUFFER_BINDING:
1137 if (mExtensions.pixelBufferObject)
1148 case GL_MAX_VIEWPORT_DIMS:
1155 case GL_SCISSOR_BOX:
1161 case GL_SHADER_COMPILER:
1162 case GL_SAMPLE_COVERAGE_INVERT:
1163 case GL_DEPTH_WRITEMASK:
1164 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1165 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1166 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1167 case GL_SAMPLE_COVERAGE:
1168 case GL_SCISSOR_TEST:
1169 case GL_STENCIL_TEST:
1173 case GL_CONTEXT_ROBUST_ACCESS_EXT:
1179 case GL_COLOR_WRITEMASK:
1185 case GL_POLYGON_OFFSET_FACTOR:
1186 case GL_POLYGON_OFFSET_UNITS:
1187 case GL_SAMPLE_COVERAGE_VALUE:
1188 case GL_DEPTH_CLEAR_VALUE:
1195 case GL_ALIASED_LINE_WIDTH_RANGE:
1196 case GL_ALIASED_POINT_SIZE_RANGE:
1197 case GL_DEPTH_RANGE:
1203 case GL_COLOR_CLEAR_VALUE:
1204 case GL_BLEND_COLOR:
1210 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1211 if (!mExtensions.maxTextureAnisotropy)
1220 if (mClientVersion < 3)
1225 // Check for ES3.0+ parameter names
1228 case GL_MAX_UNIFORM_BUFFER_BINDINGS:
1229 case GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT:
1230 case GL_UNIFORM_BUFFER_BINDING:
1231 case GL_TRANSFORM_FEEDBACK_BINDING:
1232 case GL_COPY_READ_BUFFER_BINDING:
1233 case GL_COPY_WRITE_BUFFER_BINDING:
1234 case GL_TEXTURE_BINDING_3D:
1235 case GL_TEXTURE_BINDING_2D_ARRAY:
1236 case GL_MAX_3D_TEXTURE_SIZE:
1237 case GL_MAX_ARRAY_TEXTURE_LAYERS:
1238 case GL_MAX_VERTEX_UNIFORM_BLOCKS:
1239 case GL_MAX_FRAGMENT_UNIFORM_BLOCKS:
1240 case GL_MAX_COMBINED_UNIFORM_BLOCKS:
1241 case GL_MAX_VARYING_COMPONENTS:
1242 case GL_VERTEX_ARRAY_BINDING:
1243 case GL_MAX_VERTEX_UNIFORM_COMPONENTS:
1244 case GL_MAX_FRAGMENT_UNIFORM_COMPONENTS:
1245 case GL_NUM_EXTENSIONS:
1246 case GL_MAJOR_VERSION:
1247 case GL_MINOR_VERSION:
1248 case GL_MAX_ELEMENTS_INDICES:
1249 case GL_MAX_ELEMENTS_VERTICES:
1250 case GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS:
1251 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS:
1252 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS:
1259 case GL_MAX_ELEMENT_INDEX:
1260 case GL_MAX_UNIFORM_BLOCK_SIZE:
1261 case GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS:
1262 case GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS:
1263 case GL_MAX_SERVER_WAIT_TIMEOUT:
1265 *type = GL_INT_64_ANGLEX;
1270 case GL_TRANSFORM_FEEDBACK_ACTIVE:
1271 case GL_TRANSFORM_FEEDBACK_PAUSED:
1282 bool Context::getIndexedQueryParameterInfo(GLenum target, GLenum *type, unsigned int *numParams)
1284 if (mClientVersion < 3)
1291 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
1292 case GL_UNIFORM_BUFFER_BINDING:
1298 case GL_TRANSFORM_FEEDBACK_BUFFER_START:
1299 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE:
1300 case GL_UNIFORM_BUFFER_START:
1301 case GL_UNIFORM_BUFFER_SIZE:
1303 *type = GL_INT_64_ANGLEX;
1311 // Applies the render target surface, depth stencil surface, viewport rectangle and
1312 // scissor rectangle to the renderer
1313 void Context::applyRenderTarget(GLenum drawMode, bool ignoreViewport)
1315 Framebuffer *framebufferObject = mState.getDrawFramebuffer();
1316 ASSERT(framebufferObject && framebufferObject->completeness() == GL_FRAMEBUFFER_COMPLETE);
1318 mRenderer->applyRenderTarget(framebufferObject);
1321 mState.getDepthRange(&nearZ, &farZ);
1322 mRenderer->setViewport(mState.getViewport(), nearZ, farZ, drawMode, mState.getRasterizerState().frontFace,
1325 mRenderer->setScissorRectangle(mState.getScissor(), mState.isScissorTestEnabled());
1328 // Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
1329 void Context::applyState(GLenum drawMode)
1331 Framebuffer *framebufferObject = mState.getDrawFramebuffer();
1332 int samples = framebufferObject->getSamples();
1334 RasterizerState rasterizer = mState.getRasterizerState();
1335 rasterizer.pointDrawMode = (drawMode == GL_POINTS);
1336 rasterizer.multiSample = (samples != 0);
1338 mRenderer->setRasterizerState(rasterizer);
1340 unsigned int mask = 0;
1341 if (mState.isSampleCoverageEnabled())
1343 GLclampf coverageValue;
1344 bool coverageInvert = false;
1345 mState.getSampleCoverageParams(&coverageValue, &coverageInvert);
1346 if (coverageValue != 0)
1349 float threshold = 0.5f;
1351 for (int i = 0; i < samples; ++i)
1355 if ((i + 1) * coverageValue >= threshold)
1372 mRenderer->setBlendState(framebufferObject, mState.getBlendState(), mState.getBlendColor(), mask);
1374 mRenderer->setDepthStencilState(mState.getDepthStencilState(), mState.getStencilRef(), mState.getStencilBackRef(),
1375 rasterizer.frontFace == GL_CCW);
1378 // Applies the shaders and shader constants to the Direct3D 9 device
1379 void Context::applyShaders(ProgramBinary *programBinary, bool transformFeedbackActive)
1381 const VertexAttribute *vertexAttributes = mState.getVertexArray()->getVertexAttributes();
1383 VertexFormat inputLayout[MAX_VERTEX_ATTRIBS];
1384 VertexFormat::GetInputLayout(inputLayout, programBinary, vertexAttributes, mState.getVertexAttribCurrentValues());
1386 const Framebuffer *fbo = mState.getDrawFramebuffer();
1388 mRenderer->applyShaders(programBinary, inputLayout, fbo, mState.getRasterizerState().rasterizerDiscard, transformFeedbackActive);
1390 programBinary->applyUniforms();
1393 void Context::generateSwizzles(ProgramBinary *programBinary, SamplerType type)
1395 size_t samplerRange = programBinary->getUsedSamplerRange(type);
1397 for (size_t i = 0; i < samplerRange; i++)
1399 GLenum textureType = programBinary->getSamplerTextureType(type, i);
1400 GLint textureUnit = programBinary->getSamplerMapping(type, i, getCaps());
1401 if (textureUnit != -1)
1403 Texture* texture = getSamplerTexture(textureUnit, textureType);
1404 if (texture->getSamplerState().swizzleRequired())
1406 mRenderer->generateSwizzle(texture);
1412 void Context::generateSwizzles(ProgramBinary *programBinary)
1414 generateSwizzles(programBinary, SAMPLER_VERTEX);
1415 generateSwizzles(programBinary, SAMPLER_PIXEL);
1418 // For each Direct3D sampler of either the pixel or vertex stage,
1419 // looks up the corresponding OpenGL texture image unit and texture type,
1420 // and sets the texture and its addressing/filtering state (or NULL when inactive).
1421 void Context::applyTextures(ProgramBinary *programBinary, SamplerType shaderType,
1422 const FramebufferTextureSerialArray &framebufferSerials, size_t framebufferSerialCount)
1424 size_t samplerRange = programBinary->getUsedSamplerRange(shaderType);
1425 for (size_t samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
1427 GLenum textureType = programBinary->getSamplerTextureType(shaderType, samplerIndex);
1428 GLint textureUnit = programBinary->getSamplerMapping(shaderType, samplerIndex, getCaps());
1429 if (textureUnit != -1)
1431 SamplerState sampler;
1432 Texture* texture = getSamplerTexture(textureUnit, textureType);
1433 texture->getSamplerStateWithNativeOffset(&sampler);
1435 Sampler *samplerObject = mState.getSampler(textureUnit);
1438 samplerObject->getState(&sampler);
1441 // TODO: std::binary_search may become unavailable using older versions of GCC
1442 if (texture->isSamplerComplete(sampler, mTextureCaps, mExtensions, mClientVersion) &&
1443 !std::binary_search(framebufferSerials.begin(), framebufferSerials.begin() + framebufferSerialCount, texture->getTextureSerial()))
1445 mRenderer->setSamplerState(shaderType, samplerIndex, sampler);
1446 mRenderer->setTexture(shaderType, samplerIndex, texture);
1450 // Texture is not sampler complete or it is in use by the framebuffer. Bind the incomplete texture.
1451 Texture *incompleteTexture = getIncompleteTexture(textureType);
1452 mRenderer->setTexture(shaderType, samplerIndex, incompleteTexture);
1457 // No texture bound to this slot even though it is used by the shader, bind a NULL texture
1458 mRenderer->setTexture(shaderType, samplerIndex, NULL);
1462 // Set all the remaining textures to NULL
1463 size_t samplerCount = (shaderType == SAMPLER_PIXEL) ? mCaps.maxTextureImageUnits
1464 : mCaps.maxVertexTextureImageUnits;
1465 for (size_t samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
1467 mRenderer->setTexture(shaderType, samplerIndex, NULL);
1471 void Context::applyTextures(ProgramBinary *programBinary)
1473 FramebufferTextureSerialArray framebufferSerials;
1474 size_t framebufferSerialCount = getBoundFramebufferTextureSerials(&framebufferSerials);
1476 applyTextures(programBinary, SAMPLER_VERTEX, framebufferSerials, framebufferSerialCount);
1477 applyTextures(programBinary, SAMPLER_PIXEL, framebufferSerials, framebufferSerialCount);
1480 bool Context::applyUniformBuffers()
1482 Program *programObject = getProgram(mState.getCurrentProgramId());
1483 ProgramBinary *programBinary = programObject->getProgramBinary();
1485 std::vector<Buffer*> boundBuffers;
1487 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < programBinary->getActiveUniformBlockCount(); uniformBlockIndex++)
1489 GLuint blockBinding = programObject->getUniformBlockBinding(uniformBlockIndex);
1491 if (mState.getIndexedUniformBuffer(blockBinding)->id() == 0)
1493 // undefined behaviour
1498 Buffer *uniformBuffer = mState.getIndexedUniformBuffer(blockBinding);
1499 ASSERT(uniformBuffer);
1500 boundBuffers.push_back(uniformBuffer);
1504 return programBinary->applyUniformBuffers(boundBuffers, getCaps());
1507 bool Context::applyTransformFeedbackBuffers()
1509 TransformFeedback *curTransformFeedback = mState.getCurrentTransformFeedback();
1510 if (curTransformFeedback && curTransformFeedback->isStarted() && !curTransformFeedback->isPaused())
1512 Buffer *transformFeedbackBuffers[IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1513 GLintptr transformFeedbackOffsets[IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
1514 for (size_t i = 0; i < IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1516 transformFeedbackBuffers[i] = mState.getIndexedTransformFeedbackBuffer(i);
1517 transformFeedbackOffsets[i] = mState.getIndexedTransformFeedbackBufferOffset(i);
1519 mRenderer->applyTransformFeedbackBuffers(transformFeedbackBuffers, transformFeedbackOffsets);
1528 void Context::markTransformFeedbackUsage()
1530 for (size_t i = 0; i < IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS; i++)
1532 Buffer *buffer = mState.getIndexedTransformFeedbackBuffer(i);
1535 buffer->markTransformFeedbackUsage();
1540 Error Context::clear(GLbitfield mask)
1542 if (mState.isRasterizerDiscardEnabled())
1544 return Error(GL_NO_ERROR);
1547 ClearParameters clearParams = mState.getClearParameters(mask);
1549 applyRenderTarget(GL_TRIANGLES, true); // Clips the clear to the scissor rectangle but not the viewport
1551 return mRenderer->clear(clearParams, mState.getDrawFramebuffer());
1554 Error Context::clearBufferfv(GLenum buffer, int drawbuffer, const float *values)
1556 if (mState.isRasterizerDiscardEnabled())
1558 return Error(GL_NO_ERROR);
1561 // glClearBufferfv can be called to clear the color buffer or depth buffer
1562 ClearParameters clearParams = mState.getClearParameters(0);
1564 if (buffer == GL_COLOR)
1566 for (unsigned int i = 0; i < ArraySize(clearParams.clearColor); i++)
1568 clearParams.clearColor[i] = (drawbuffer == static_cast<int>(i));
1570 clearParams.colorFClearValue = ColorF(values[0], values[1], values[2], values[3]);
1571 clearParams.colorClearType = GL_FLOAT;
1574 if (buffer == GL_DEPTH)
1576 clearParams.clearDepth = true;
1577 clearParams.depthClearValue = values[0];
1580 applyRenderTarget(GL_TRIANGLES, true); // Clips the clear to the scissor rectangle but not the viewport
1582 return mRenderer->clear(clearParams, mState.getDrawFramebuffer());
1585 Error Context::clearBufferuiv(GLenum buffer, int drawbuffer, const unsigned int *values)
1587 if (mState.isRasterizerDiscardEnabled())
1589 return Error(GL_NO_ERROR);
1592 // glClearBufferuv can only be called to clear a color buffer
1593 ClearParameters clearParams = mState.getClearParameters(0);
1594 for (unsigned int i = 0; i < ArraySize(clearParams.clearColor); i++)
1596 clearParams.clearColor[i] = (drawbuffer == static_cast<int>(i));
1598 clearParams.colorUIClearValue = ColorUI(values[0], values[1], values[2], values[3]);
1599 clearParams.colorClearType = GL_UNSIGNED_INT;
1601 applyRenderTarget(GL_TRIANGLES, true); // Clips the clear to the scissor rectangle but not the viewport
1603 return mRenderer->clear(clearParams, mState.getDrawFramebuffer());
1606 Error Context::clearBufferiv(GLenum buffer, int drawbuffer, const int *values)
1608 if (mState.isRasterizerDiscardEnabled())
1610 return Error(GL_NO_ERROR);
1613 // glClearBufferfv can be called to clear the color buffer or stencil buffer
1614 ClearParameters clearParams = mState.getClearParameters(0);
1616 if (buffer == GL_COLOR)
1618 for (unsigned int i = 0; i < ArraySize(clearParams.clearColor); i++)
1620 clearParams.clearColor[i] = (drawbuffer == static_cast<int>(i));
1622 clearParams.colorIClearValue = ColorI(values[0], values[1], values[2], values[3]);
1623 clearParams.colorClearType = GL_INT;
1626 if (buffer == GL_STENCIL)
1628 clearParams.clearStencil = true;
1629 clearParams.stencilClearValue = values[1];
1632 applyRenderTarget(GL_TRIANGLES, true); // Clips the clear to the scissor rectangle but not the viewport
1634 return mRenderer->clear(clearParams, mState.getDrawFramebuffer());
1637 Error Context::clearBufferfi(GLenum buffer, int drawbuffer, float depth, int stencil)
1639 if (mState.isRasterizerDiscardEnabled())
1641 return Error(GL_NO_ERROR);
1644 // glClearBufferfi can only be called to clear a depth stencil buffer
1645 ClearParameters clearParams = mState.getClearParameters(0);
1646 clearParams.clearDepth = true;
1647 clearParams.depthClearValue = depth;
1648 clearParams.clearStencil = true;
1649 clearParams.stencilClearValue = stencil;
1651 applyRenderTarget(GL_TRIANGLES, true); // Clips the clear to the scissor rectangle but not the viewport
1653 return mRenderer->clear(clearParams, mState.getDrawFramebuffer());
1656 Error Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
1657 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
1659 Framebuffer *framebuffer = mState.getReadFramebuffer();
1661 GLenum sizedInternalFormat = GetSizedInternalFormat(format, type);
1662 const InternalFormat &sizedFormatInfo = GetInternalFormatInfo(sizedInternalFormat);
1663 GLuint outputPitch = sizedFormatInfo.computeRowPitch(type, width, mState.getPackAlignment());
1665 return mRenderer->readPixels(framebuffer, x, y, width, height, format, type, outputPitch, mState.getPackState(),
1666 reinterpret_cast<uint8_t*>(pixels));
1669 void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
1671 ASSERT(mState.getCurrentProgramId() != 0);
1673 ProgramBinary *programBinary = mState.getCurrentProgramBinary();
1674 programBinary->updateSamplerMapping();
1676 generateSwizzles(programBinary);
1678 if (!mRenderer->applyPrimitiveType(mode, count))
1683 applyRenderTarget(mode, false);
1686 Error error = mRenderer->applyVertexBuffer(programBinary, mState.getVertexArray()->getVertexAttributes(), mState.getVertexAttribCurrentValues(), first, count, instances);
1687 if (error.isError())
1689 return gl::error(error.getCode());
1692 bool transformFeedbackActive = applyTransformFeedbackBuffers();
1694 applyShaders(programBinary, transformFeedbackActive);
1696 applyTextures(programBinary);
1698 if (!applyUniformBuffers())
1703 if (!skipDraw(mode))
1705 mRenderer->drawArrays(mode, count, instances, transformFeedbackActive);
1707 if (transformFeedbackActive)
1709 markTransformFeedbackUsage();
1714 void Context::drawElements(GLenum mode, GLsizei count, GLenum type,
1715 const GLvoid *indices, GLsizei instances,
1716 const rx::RangeUI &indexRange)
1718 ASSERT(mState.getCurrentProgramId() != 0);
1720 ProgramBinary *programBinary = mState.getCurrentProgramBinary();
1721 programBinary->updateSamplerMapping();
1723 generateSwizzles(programBinary);
1725 if (!mRenderer->applyPrimitiveType(mode, count))
1730 applyRenderTarget(mode, false);
1733 VertexArray *vao = mState.getVertexArray();
1734 rx::TranslatedIndexData indexInfo;
1735 indexInfo.indexRange = indexRange;
1736 Error error = mRenderer->applyIndexBuffer(indices, vao->getElementArrayBuffer(), count, mode, type, &indexInfo);
1737 if (error.isError())
1739 return gl::error(error.getCode());
1742 GLsizei vertexCount = indexInfo.indexRange.length() + 1;
1743 error = mRenderer->applyVertexBuffer(programBinary, vao->getVertexAttributes(),
1744 mState.getVertexAttribCurrentValues(),
1745 indexInfo.indexRange.start, vertexCount, instances);
1746 if (error.isError())
1748 return gl::error(error.getCode());
1751 bool transformFeedbackActive = applyTransformFeedbackBuffers();
1752 // Transform feedback is not allowed for DrawElements, this error should have been caught at the API validation
1754 ASSERT(!transformFeedbackActive);
1756 applyShaders(programBinary, transformFeedbackActive);
1758 applyTextures(programBinary);
1760 if (!applyUniformBuffers())
1765 if (!skipDraw(mode))
1767 mRenderer->drawElements(mode, count, type, indices, vao->getElementArrayBuffer(), indexInfo, instances);
1771 // Implements glFlush when block is false, glFinish when block is true
1772 void Context::sync(bool block)
1774 mRenderer->sync(block);
1777 void Context::recordError(const Error &error)
1779 if (error.isError())
1781 mErrors.insert(error.getCode());
1785 // Get one of the recorded errors and clear its flag, if any.
1786 // [OpenGL ES 2.0.24] section 2.5 page 13.
1787 GLenum Context::getError()
1789 if (mErrors.empty())
1795 GLenum error = *mErrors.begin();
1796 mErrors.erase(mErrors.begin());
1801 GLenum Context::getResetStatus()
1803 if (mResetStatus == GL_NO_ERROR && !mContextLost)
1805 // mResetStatus will be set by the markContextLost callback
1806 // in the case a notification is sent
1807 mRenderer->testDeviceLost(true);
1810 GLenum status = mResetStatus;
1812 if (mResetStatus != GL_NO_ERROR)
1814 ASSERT(mContextLost);
1816 if (mRenderer->testDeviceResettable())
1818 mResetStatus = GL_NO_ERROR;
1825 bool Context::isResetNotificationEnabled()
1827 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
1830 int Context::getClientVersion() const
1832 return mClientVersion;
1835 const Caps &Context::getCaps() const
1840 const TextureCapsMap &Context::getTextureCaps() const
1842 return mTextureCaps;
1845 const Extensions &Context::getExtensions() const
1850 void Context::getCurrentReadFormatType(GLenum *internalFormat, GLenum *format, GLenum *type)
1852 Framebuffer *framebuffer = mState.getReadFramebuffer();
1853 ASSERT(framebuffer && framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE);
1855 FramebufferAttachment *attachment = framebuffer->getReadColorbuffer();
1858 GLenum actualFormat = attachment->getActualFormat();
1859 const InternalFormat &actualFormatInfo = GetInternalFormatInfo(actualFormat);
1861 *internalFormat = actualFormat;
1862 *format = actualFormatInfo.format;
1863 *type = actualFormatInfo.type;
1866 void Context::detachTexture(GLuint texture)
1868 // Simple pass-through to State's detachTexture method, as textures do not require
1869 // allocation map management either here or in the resource manager at detach time.
1870 // Zero textures are held by the Context, and we don't attempt to request them from
1872 mState.detachTexture(texture);
1875 void Context::detachBuffer(GLuint buffer)
1877 // Buffer detachment is handled by Context, because the buffer must also be
1878 // attached from any VAOs in existence, and Context holds the VAO map.
1880 // [OpenGL ES 2.0.24] section 2.9 page 22:
1881 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
1882 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
1884 mState.removeArrayBufferBinding(buffer);
1886 // mark as freed among the vertex array objects
1887 for (auto vaoIt = mVertexArrayMap.begin(); vaoIt != mVertexArrayMap.end(); vaoIt++)
1889 vaoIt->second->detachBuffer(buffer);
1893 void Context::detachFramebuffer(GLuint framebuffer)
1895 // Framebuffer detachment is handled by Context, because 0 is a valid
1896 // Framebuffer object, and a pointer to it must be passed from Context
1897 // to State at binding time.
1899 // [OpenGL ES 2.0.24] section 4.4 page 107:
1900 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
1901 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
1903 if (mState.removeReadFramebufferBinding(framebuffer))
1905 bindReadFramebuffer(0);
1908 if (mState.removeDrawFramebufferBinding(framebuffer))
1910 bindDrawFramebuffer(0);
1914 void Context::detachRenderbuffer(GLuint renderbuffer)
1916 mState.detachRenderbuffer(renderbuffer);
1919 void Context::detachVertexArray(GLuint vertexArray)
1921 // Vertex array detachment is handled by Context, because 0 is a valid
1922 // VAO, and a pointer to it must be passed from Context to State at
1925 // [OpenGL ES 3.0.2] section 2.10 page 43:
1926 // If a vertex array object that is currently bound is deleted, the binding
1927 // for that object reverts to zero and the default vertex array becomes current.
1928 if (mState.removeVertexArrayBinding(vertexArray))
1934 void Context::detachTransformFeedback(GLuint transformFeedback)
1936 mState.detachTransformFeedback(transformFeedback);
1939 void Context::detachSampler(GLuint sampler)
1941 mState.detachSampler(sampler);
1944 Texture *Context::getIncompleteTexture(GLenum type)
1946 if (mIncompleteTextures.find(type) == mIncompleteTextures.end())
1948 const GLubyte color[] = { 0, 0, 0, 255 };
1949 const PixelUnpackState incompleteUnpackState(1);
1956 // default falls through to TEXTURE_2D
1960 Texture2D *incomplete2d = new Texture2D(mRenderer->createTexture(GL_TEXTURE_2D), Texture::INCOMPLETE_TEXTURE_ID);
1961 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, incompleteUnpackState, color);
1966 case GL_TEXTURE_CUBE_MAP:
1968 TextureCubeMap *incompleteCube = new TextureCubeMap(mRenderer->createTexture(GL_TEXTURE_CUBE_MAP), Texture::INCOMPLETE_TEXTURE_ID);
1970 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, incompleteUnpackState, color);
1971 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, incompleteUnpackState, color);
1972 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, incompleteUnpackState, color);
1973 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, incompleteUnpackState, color);
1974 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, incompleteUnpackState, color);
1975 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, incompleteUnpackState, color);
1983 Texture3D *incomplete3d = new Texture3D(mRenderer->createTexture(GL_TEXTURE_3D), Texture::INCOMPLETE_TEXTURE_ID);
1984 incomplete3d->setImage(0, 1, 1, 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, incompleteUnpackState, color);
1990 case GL_TEXTURE_2D_ARRAY:
1992 Texture2DArray *incomplete2darray = new Texture2DArray(mRenderer->createTexture(GL_TEXTURE_2D_ARRAY), Texture::INCOMPLETE_TEXTURE_ID);
1993 incomplete2darray->setImage(0, 1, 1, 1, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, incompleteUnpackState, color);
1995 t = incomplete2darray;
2000 mIncompleteTextures[type].set(t);
2003 return mIncompleteTextures[type].get();
2006 bool Context::skipDraw(GLenum drawMode)
2008 if (drawMode == GL_POINTS)
2010 // ProgramBinary assumes non-point rendering if gl_PointSize isn't written,
2011 // which affects varying interpolation. Since the value of gl_PointSize is
2012 // undefined when not written, just skip drawing to avoid unexpected results.
2013 if (!mState.getCurrentProgramBinary()->usesPointSize())
2015 // This is stictly speaking not an error, but developers should be
2016 // notified of risking undefined behavior.
2017 ERR("Point rendering without writing to gl_PointSize.");
2022 else if (IsTriangleMode(drawMode))
2024 if (mState.getRasterizerState().cullFace && mState.getRasterizerState().cullMode == GL_FRONT_AND_BACK)
2033 void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
2035 mState.getVertexArray()->setVertexAttribDivisor(index, divisor);
2038 void Context::samplerParameteri(GLuint sampler, GLenum pname, GLint param)
2040 mResourceManager->checkSamplerAllocation(sampler);
2042 Sampler *samplerObject = getSampler(sampler);
2043 ASSERT(samplerObject);
2047 case GL_TEXTURE_MIN_FILTER: samplerObject->setMinFilter(static_cast<GLenum>(param)); break;
2048 case GL_TEXTURE_MAG_FILTER: samplerObject->setMagFilter(static_cast<GLenum>(param)); break;
2049 case GL_TEXTURE_WRAP_S: samplerObject->setWrapS(static_cast<GLenum>(param)); break;
2050 case GL_TEXTURE_WRAP_T: samplerObject->setWrapT(static_cast<GLenum>(param)); break;
2051 case GL_TEXTURE_WRAP_R: samplerObject->setWrapR(static_cast<GLenum>(param)); break;
2052 case GL_TEXTURE_MIN_LOD: samplerObject->setMinLod(static_cast<GLfloat>(param)); break;
2053 case GL_TEXTURE_MAX_LOD: samplerObject->setMaxLod(static_cast<GLfloat>(param)); break;
2054 case GL_TEXTURE_COMPARE_MODE: samplerObject->setComparisonMode(static_cast<GLenum>(param)); break;
2055 case GL_TEXTURE_COMPARE_FUNC: samplerObject->setComparisonFunc(static_cast<GLenum>(param)); break;
2056 default: UNREACHABLE(); break;
2060 void Context::samplerParameterf(GLuint sampler, GLenum pname, GLfloat param)
2062 mResourceManager->checkSamplerAllocation(sampler);
2064 Sampler *samplerObject = getSampler(sampler);
2065 ASSERT(samplerObject);
2069 case GL_TEXTURE_MIN_FILTER: samplerObject->setMinFilter(uiround<GLenum>(param)); break;
2070 case GL_TEXTURE_MAG_FILTER: samplerObject->setMagFilter(uiround<GLenum>(param)); break;
2071 case GL_TEXTURE_WRAP_S: samplerObject->setWrapS(uiround<GLenum>(param)); break;
2072 case GL_TEXTURE_WRAP_T: samplerObject->setWrapT(uiround<GLenum>(param)); break;
2073 case GL_TEXTURE_WRAP_R: samplerObject->setWrapR(uiround<GLenum>(param)); break;
2074 case GL_TEXTURE_MIN_LOD: samplerObject->setMinLod(param); break;
2075 case GL_TEXTURE_MAX_LOD: samplerObject->setMaxLod(param); break;
2076 case GL_TEXTURE_COMPARE_MODE: samplerObject->setComparisonMode(uiround<GLenum>(param)); break;
2077 case GL_TEXTURE_COMPARE_FUNC: samplerObject->setComparisonFunc(uiround<GLenum>(param)); break;
2078 default: UNREACHABLE(); break;
2082 GLint Context::getSamplerParameteri(GLuint sampler, GLenum pname)
2084 mResourceManager->checkSamplerAllocation(sampler);
2086 Sampler *samplerObject = getSampler(sampler);
2087 ASSERT(samplerObject);
2091 case GL_TEXTURE_MIN_FILTER: return static_cast<GLint>(samplerObject->getMinFilter());
2092 case GL_TEXTURE_MAG_FILTER: return static_cast<GLint>(samplerObject->getMagFilter());
2093 case GL_TEXTURE_WRAP_S: return static_cast<GLint>(samplerObject->getWrapS());
2094 case GL_TEXTURE_WRAP_T: return static_cast<GLint>(samplerObject->getWrapT());
2095 case GL_TEXTURE_WRAP_R: return static_cast<GLint>(samplerObject->getWrapR());
2096 case GL_TEXTURE_MIN_LOD: return uiround<GLint>(samplerObject->getMinLod());
2097 case GL_TEXTURE_MAX_LOD: return uiround<GLint>(samplerObject->getMaxLod());
2098 case GL_TEXTURE_COMPARE_MODE: return static_cast<GLint>(samplerObject->getComparisonMode());
2099 case GL_TEXTURE_COMPARE_FUNC: return static_cast<GLint>(samplerObject->getComparisonFunc());
2100 default: UNREACHABLE(); return 0;
2104 GLfloat Context::getSamplerParameterf(GLuint sampler, GLenum pname)
2106 mResourceManager->checkSamplerAllocation(sampler);
2108 Sampler *samplerObject = getSampler(sampler);
2109 ASSERT(samplerObject);
2113 case GL_TEXTURE_MIN_FILTER: return static_cast<GLfloat>(samplerObject->getMinFilter());
2114 case GL_TEXTURE_MAG_FILTER: return static_cast<GLfloat>(samplerObject->getMagFilter());
2115 case GL_TEXTURE_WRAP_S: return static_cast<GLfloat>(samplerObject->getWrapS());
2116 case GL_TEXTURE_WRAP_T: return static_cast<GLfloat>(samplerObject->getWrapT());
2117 case GL_TEXTURE_WRAP_R: return static_cast<GLfloat>(samplerObject->getWrapR());
2118 case GL_TEXTURE_MIN_LOD: return samplerObject->getMinLod();
2119 case GL_TEXTURE_MAX_LOD: return samplerObject->getMaxLod();
2120 case GL_TEXTURE_COMPARE_MODE: return static_cast<GLfloat>(samplerObject->getComparisonMode());
2121 case GL_TEXTURE_COMPARE_FUNC: return static_cast<GLfloat>(samplerObject->getComparisonFunc());
2122 default: UNREACHABLE(); return 0;
2126 void Context::initRendererString()
2128 std::ostringstream rendererString;
2129 rendererString << "ANGLE (";
2130 rendererString << mRenderer->getRendererDescription();
2131 rendererString << ")";
2133 mRendererString = MakeStaticString(rendererString.str());
2136 const std::string &Context::getRendererString() const
2138 return mRendererString;
2141 void Context::initExtensionStrings()
2143 mExtensionStrings = mExtensions.getStrings();
2145 std::ostringstream combinedStringStream;
2146 std::copy(mExtensionStrings.begin(), mExtensionStrings.end(), std::ostream_iterator<std::string>(combinedStringStream, " "));
2147 mExtensionString = combinedStringStream.str();
2150 const std::string &Context::getExtensionString() const
2152 return mExtensionString;
2155 const std::string &Context::getExtensionString(size_t idx) const
2157 return mExtensionStrings[idx];
2160 size_t Context::getExtensionStringCount() const
2162 return mExtensionStrings.size();
2165 size_t Context::getBoundFramebufferTextureSerials(FramebufferTextureSerialArray *outSerialArray)
2167 size_t serialCount = 0;
2169 Framebuffer *drawFramebuffer = mState.getDrawFramebuffer();
2170 for (unsigned int i = 0; i < IMPLEMENTATION_MAX_DRAW_BUFFERS; i++)
2172 FramebufferAttachment *attachment = drawFramebuffer->getColorbuffer(i);
2173 if (attachment && attachment->isTexture())
2175 Texture *texture = attachment->getTexture();
2176 (*outSerialArray)[serialCount++] = texture->getTextureSerial();
2180 FramebufferAttachment *depthStencilAttachment = drawFramebuffer->getDepthOrStencilbuffer();
2181 if (depthStencilAttachment && depthStencilAttachment->isTexture())
2183 Texture *depthStencilTexture = depthStencilAttachment->getTexture();
2184 (*outSerialArray)[serialCount++] = depthStencilTexture->getTextureSerial();
2187 std::sort(outSerialArray->begin(), outSerialArray->begin() + serialCount);
2192 void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2193 GLbitfield mask, GLenum filter)
2195 Framebuffer *readFramebuffer = mState.getReadFramebuffer();
2196 Framebuffer *drawFramebuffer = mState.getDrawFramebuffer();
2198 bool blitRenderTarget = false;
2199 bool blitDepth = false;
2200 bool blitStencil = false;
2201 if ((mask & GL_COLOR_BUFFER_BIT) && readFramebuffer->getReadColorbuffer() && drawFramebuffer->getFirstColorbuffer())
2203 blitRenderTarget = true;
2205 if ((mask & GL_STENCIL_BUFFER_BIT) && readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
2209 if ((mask & GL_DEPTH_BUFFER_BIT) && readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
2214 Rectangle srcRect(srcX0, srcY0, srcX1 - srcX0, srcY1 - srcY0);
2215 Rectangle dstRect(dstX0, dstY0, dstX1 - dstX0, dstY1 - dstY0);
2216 if (blitRenderTarget || blitDepth || blitStencil)
2218 const Rectangle *scissor = mState.isScissorTestEnabled() ? &mState.getScissor() : NULL;
2219 mRenderer->blitRect(readFramebuffer, srcRect, drawFramebuffer, dstRect, scissor,
2220 blitRenderTarget, blitDepth, blitStencil, filter);
2224 void Context::releaseShaderCompiler()
2226 mRenderer->releaseShaderCompiler();
2229 void Context::initCaps(GLuint clientVersion)
2231 mCaps = mRenderer->getRendererCaps();
2233 mExtensions = mRenderer->getRendererExtensions();
2235 if (clientVersion < 3)
2237 // Disable ES3+ extensions
2238 mExtensions.colorBufferFloat = false;
2241 if (clientVersion > 2)
2243 // FIXME(geofflang): Don't support EXT_sRGB in non-ES2 contexts
2244 //mExtensions.sRGB = false;
2247 // Apply implementation limits
2248 mCaps.maxVertexAttributes = std::min<GLuint>(mCaps.maxVertexAttributes, MAX_VERTEX_ATTRIBS);
2249 mCaps.maxVertexUniformBlocks = std::min<GLuint>(mCaps.maxVertexUniformBlocks, IMPLEMENTATION_MAX_VERTEX_SHADER_UNIFORM_BUFFERS);
2250 mCaps.maxVertexOutputComponents = std::min<GLuint>(mCaps.maxVertexOutputComponents, IMPLEMENTATION_MAX_VARYING_VECTORS * 4);
2252 mCaps.maxFragmentInputComponents = std::min<GLuint>(mCaps.maxFragmentInputComponents, IMPLEMENTATION_MAX_VARYING_VECTORS * 4);
2254 GLuint maxSamples = 0;
2255 mCaps.compressedTextureFormats.clear();
2257 const TextureCapsMap &rendererFormats = mRenderer->getRendererTextureCaps();
2258 for (TextureCapsMap::const_iterator i = rendererFormats.begin(); i != rendererFormats.end(); i++)
2260 GLenum format = i->first;
2261 TextureCaps formatCaps = i->second;
2263 const InternalFormat &formatInfo = GetInternalFormatInfo(format);
2264 if (formatCaps.texturable && formatInfo.textureSupport(clientVersion, mExtensions))
2266 // Update the format caps based on the client version and extensions
2267 formatCaps.renderable = formatInfo.renderSupport(clientVersion, mExtensions);
2268 formatCaps.filterable = formatInfo.filterSupport(clientVersion, mExtensions);
2270 // OpenGL ES does not support multisampling with integer formats
2271 if (formatInfo.componentType == GL_INT || formatInfo.componentType == GL_UNSIGNED_INT)
2273 formatCaps.sampleCounts.clear();
2275 maxSamples = std::max(maxSamples, formatCaps.getMaxSamples());
2277 if (formatInfo.compressed)
2279 mCaps.compressedTextureFormats.push_back(format);
2282 mTextureCaps.insert(format, formatCaps);
2286 mExtensions.maxSamples = maxSamples;
2293 gl::Context *glCreateContext(int clientVersion, const gl::Context *shareContext, rx::Renderer *renderer, bool notifyResets, bool robustAccess)
2295 return new gl::Context(clientVersion, shareContext, renderer, notifyResets, robustAccess);
2298 void glDestroyContext(gl::Context *context)
2302 if (context == gl::getContext())
2304 gl::makeCurrent(NULL, NULL, NULL);
2308 void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
2310 gl::makeCurrent(context, display, surface);
2313 gl::Context *glGetCurrentContext()
2315 return gl::getContext();