Context.cpp revision d7d9b4bdfee82766c44e5a473b28d5884e4e2329
1// SwiftShader Software Renderer
2//
3// Copyright(c) 2005-2013 TransGaming Inc.
4//
5// All rights reserved. No part of this software may be copied, distributed, transmitted,
6// transcribed, stored in a retrieval system, translated into any human or computer
7// language by any means, or disclosed to third parties without the explicit written
8// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9// or implied, including but not limited to any patent rights, are granted to you.
10//
11
12// Context.cpp: Implements the es1::Context class, managing all GL state and performing
13// rendering operations. It is the GLES2 specific implementation of EGLContext.
14
15#include "Context.h"
16
17#include "main.h"
18#include "mathutil.h"
19#include "utilities.h"
20#include "ResourceManager.h"
21#include "Buffer.h"
22#include "Framebuffer.h"
23#include "Renderbuffer.h"
24#include "Texture.h"
25#include "VertexDataManager.h"
26#include "IndexDataManager.h"
27#include "libEGL/Display.h"
28#include "libEGL/Surface.h"
29#include "Common/Half.hpp"
30
31#include <EGL/eglext.h>
32
33#undef near
34#undef far
35
36namespace es1
37{
38Context::Context(const egl::Config *config, const Context *shareContext)
39    : modelViewStack(MAX_MODELVIEW_STACK_DEPTH),
40      projectionStack(MAX_PROJECTION_STACK_DEPTH),
41	  textureStack0(MAX_TEXTURE_STACK_DEPTH),
42	  textureStack1(MAX_TEXTURE_STACK_DEPTH)
43{
44	sw::Context *context = new sw::Context();
45	device = new es1::Device(context);
46
47	mVertexDataManager = new VertexDataManager(this);
48    mIndexDataManager = new IndexDataManager();
49
50    setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
51
52    mState.depthClearValue = 1.0f;
53    mState.stencilClearValue = 0;
54
55    mState.cullFace = false;
56    mState.cullMode = GL_BACK;
57    mState.frontFace = GL_CCW;
58    mState.depthTest = false;
59    mState.depthFunc = GL_LESS;
60    mState.blend = false;
61    mState.sourceBlendRGB = GL_ONE;
62    mState.sourceBlendAlpha = GL_ONE;
63    mState.destBlendRGB = GL_ZERO;
64    mState.destBlendAlpha = GL_ZERO;
65    mState.blendEquationRGB = GL_FUNC_ADD_OES;
66    mState.blendEquationAlpha = GL_FUNC_ADD_OES;
67    mState.stencilTest = false;
68    mState.stencilFunc = GL_ALWAYS;
69    mState.stencilRef = 0;
70    mState.stencilMask = -1;
71    mState.stencilWritemask = -1;
72    mState.stencilFail = GL_KEEP;
73    mState.stencilPassDepthFail = GL_KEEP;
74    mState.stencilPassDepthPass = GL_KEEP;
75    mState.polygonOffsetFill = false;
76    mState.polygonOffsetFactor = 0.0f;
77    mState.polygonOffsetUnits = 0.0f;
78    mState.sampleAlphaToCoverage = false;
79    mState.sampleCoverage = false;
80    mState.sampleCoverageValue = 1.0f;
81    mState.sampleCoverageInvert = false;
82    mState.scissorTest = false;
83    mState.dither = true;
84    mState.generateMipmapHint = GL_DONT_CARE;
85
86    mState.lineWidth = 1.0f;
87
88    mState.viewportX = 0;
89    mState.viewportY = 0;
90    mState.viewportWidth = config->mDisplayMode.width;
91    mState.viewportHeight = config->mDisplayMode.height;
92    mState.zNear = 0.0f;
93    mState.zFar = 1.0f;
94
95    mState.scissorX = 0;
96    mState.scissorY = 0;
97    mState.scissorWidth = config->mDisplayMode.width;
98    mState.scissorHeight = config->mDisplayMode.height;
99
100    mState.colorMaskRed = true;
101    mState.colorMaskGreen = true;
102    mState.colorMaskBlue = true;
103    mState.colorMaskAlpha = true;
104    mState.depthMask = true;
105
106	mState.textureEnvMode = GL_MODULATE;
107
108    if(shareContext != NULL)
109    {
110        mResourceManager = shareContext->mResourceManager;
111        mResourceManager->addRef();
112    }
113    else
114    {
115        mResourceManager = new ResourceManager();
116    }
117
118    // [OpenGL ES 2.0.24] section 3.7 page 83:
119    // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
120    // and cube map texture state vectors respectively associated with them.
121    // In order that access to these initial textures not be lost, they are treated as texture
122    // objects all of whose names are 0.
123
124    mTexture2DZero = new Texture2D(0);
125    mTextureExternalZero = new TextureExternal(0);
126
127    mState.activeSampler = 0;
128    bindArrayBuffer(0);
129    bindElementArrayBuffer(0);
130    bindTexture2D(0);
131    bindFramebuffer(0);
132    bindRenderbuffer(0);
133
134    mState.packAlignment = 4;
135    mState.unpackAlignment = 4;
136
137    mInvalidEnum = false;
138    mInvalidValue = false;
139    mInvalidOperation = false;
140    mOutOfMemory = false;
141    mInvalidFramebufferOperation = false;
142
143	lighting = false;
144
145	for(int i = 0; i < MAX_LIGHTS; i++)
146	{
147		light[i].enable = false;
148		light[i].ambient = {0.0f, 0.0f, 0.0f, 1.0f};
149		light[i].diffuse = {0.0f, 0.0f, 0.0f, 1.0f};
150		light[i].specular = {0.0f, 0.0f, 0.0f, 1.0f};
151		light[i].position = {0.0f, 0.0f, 1.0f, 0.0f};
152		light[i].direction = {0.0f, 0.0f, -1.0f};
153		light[i].attenuation = {1.0f, 0.0f, 0.0f};
154	}
155
156	light[0].diffuse = {1.0f, 1.0f, 1.0f, 1.0f};
157	light[0].specular = {1.0f, 1.0f, 1.0f, 1.0f};
158
159	globalAmbient = {0.2f, 0.2f, 0.2f, 1.0f};
160	materialAmbient = {0.2f, 0.2f, 0.2f, 1.0f};
161	materialDiffuse = {0.8f, 0.8f, 0.8f, 1.0f};
162	materialSpecular = {0.0f, 0.0f, 0.0f, 1.0f};
163	materialEmission = {0.0f, 0.0f, 0.0f, 1.0f};
164
165	matrixMode = GL_MODELVIEW;
166    texture2D = false;
167	clientTexture = GL_TEXTURE0;
168
169	setVertexAttrib(sw::Color0, 1.0f, 1.0f, 1.0f, 1.0f);
170
171	for(int i = 0; i < MAX_TEXTURE_UNITS; i++)
172	{
173		setVertexAttrib(sw::TexCoord0 + i, 0.0f, 0.0f, 0.0f, 1.0f);
174	}
175
176	setVertexAttrib(sw::Normal, 0.0f, 0.0f, 1.0f, 1.0f);
177
178    mHasBeenCurrent = false;
179
180    markAllStateDirty();
181}
182
183Context::~Context()
184{
185    while(!mFramebufferMap.empty())
186    {
187        deleteFramebuffer(mFramebufferMap.begin()->first);
188    }
189
190    for(int type = 0; type < TEXTURE_TYPE_COUNT; type++)
191    {
192        for(int sampler = 0; sampler < MAX_TEXTURE_UNITS; sampler++)
193        {
194            mState.samplerTexture[type][sampler] = NULL;
195        }
196    }
197
198    for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
199    {
200        mState.vertexAttribute[i].mBoundBuffer = NULL;
201    }
202
203    mState.arrayBuffer = NULL;
204    mState.elementArrayBuffer = NULL;
205    mState.renderbuffer = NULL;
206
207    mTexture2DZero = NULL;
208    mTextureExternalZero = NULL;
209
210    delete mVertexDataManager;
211    delete mIndexDataManager;
212
213    mResourceManager->release();
214	delete device;
215}
216
217void Context::makeCurrent(egl::Surface *surface)
218{
219    if(!mHasBeenCurrent)
220    {
221        mState.viewportX = 0;
222        mState.viewportY = 0;
223        mState.viewportWidth = surface->getWidth();
224        mState.viewportHeight = surface->getHeight();
225
226        mState.scissorX = 0;
227        mState.scissorY = 0;
228        mState.scissorWidth = surface->getWidth();
229        mState.scissorHeight = surface->getHeight();
230
231        mHasBeenCurrent = true;
232    }
233
234    // Wrap the existing resources into GL objects and assign them to the '0' names
235    egl::Image *defaultRenderTarget = surface->getRenderTarget();
236    egl::Image *depthStencil = surface->getDepthStencil();
237
238    Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
239    DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
240    Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
241
242    setFramebufferZero(framebufferZero);
243
244    if(defaultRenderTarget)
245    {
246        defaultRenderTarget->release();
247    }
248
249    if(depthStencil)
250    {
251        depthStencil->release();
252    }
253
254    markAllStateDirty();
255}
256
257void Context::destroy()
258{
259	delete this;
260}
261
262int Context::getClientVersion()
263{
264	return 1;
265}
266
267// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
268void Context::markAllStateDirty()
269{
270    mDepthStateDirty = true;
271    mMaskStateDirty = true;
272    mBlendStateDirty = true;
273    mStencilStateDirty = true;
274    mPolygonOffsetStateDirty = true;
275    mSampleStateDirty = true;
276    mDitherStateDirty = true;
277    mFrontFaceDirty = true;
278}
279
280void Context::setClearColor(float red, float green, float blue, float alpha)
281{
282    mState.colorClearValue.red = red;
283    mState.colorClearValue.green = green;
284    mState.colorClearValue.blue = blue;
285    mState.colorClearValue.alpha = alpha;
286}
287
288void Context::setClearDepth(float depth)
289{
290    mState.depthClearValue = depth;
291}
292
293void Context::setClearStencil(int stencil)
294{
295    mState.stencilClearValue = stencil;
296}
297
298void Context::setCullFace(bool enabled)
299{
300    mState.cullFace = enabled;
301}
302
303bool Context::isCullFaceEnabled() const
304{
305    return mState.cullFace;
306}
307
308void Context::setCullMode(GLenum mode)
309{
310   mState.cullMode = mode;
311}
312
313void Context::setFrontFace(GLenum front)
314{
315    if(mState.frontFace != front)
316    {
317        mState.frontFace = front;
318        mFrontFaceDirty = true;
319    }
320}
321
322void Context::setDepthTest(bool enabled)
323{
324    if(mState.depthTest != enabled)
325    {
326        mState.depthTest = enabled;
327        mDepthStateDirty = true;
328    }
329}
330
331bool Context::isDepthTestEnabled() const
332{
333    return mState.depthTest;
334}
335
336void Context::setDepthFunc(GLenum depthFunc)
337{
338    if(mState.depthFunc != depthFunc)
339    {
340        mState.depthFunc = depthFunc;
341        mDepthStateDirty = true;
342    }
343}
344
345void Context::setDepthRange(float zNear, float zFar)
346{
347    mState.zNear = zNear;
348    mState.zFar = zFar;
349}
350
351void Context::setBlend(bool enabled)
352{
353    if(mState.blend != enabled)
354    {
355        mState.blend = enabled;
356        mBlendStateDirty = true;
357    }
358}
359
360bool Context::isBlendEnabled() const
361{
362    return mState.blend;
363}
364
365void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
366{
367    if(mState.sourceBlendRGB != sourceRGB ||
368       mState.sourceBlendAlpha != sourceAlpha ||
369       mState.destBlendRGB != destRGB ||
370       mState.destBlendAlpha != destAlpha)
371    {
372        mState.sourceBlendRGB = sourceRGB;
373        mState.destBlendRGB = destRGB;
374        mState.sourceBlendAlpha = sourceAlpha;
375        mState.destBlendAlpha = destAlpha;
376        mBlendStateDirty = true;
377    }
378}
379
380void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
381{
382    if(mState.blendEquationRGB != rgbEquation ||
383       mState.blendEquationAlpha != alphaEquation)
384    {
385        mState.blendEquationRGB = rgbEquation;
386        mState.blendEquationAlpha = alphaEquation;
387        mBlendStateDirty = true;
388    }
389}
390
391void Context::setStencilTest(bool enabled)
392{
393    if(mState.stencilTest != enabled)
394    {
395        mState.stencilTest = enabled;
396        mStencilStateDirty = true;
397    }
398}
399
400bool Context::isStencilTestEnabled() const
401{
402    return mState.stencilTest;
403}
404
405void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
406{
407    if(mState.stencilFunc != stencilFunc ||
408        mState.stencilRef != stencilRef ||
409        mState.stencilMask != stencilMask)
410    {
411        mState.stencilFunc = stencilFunc;
412        mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
413        mState.stencilMask = stencilMask;
414        mStencilStateDirty = true;
415    }
416}
417
418void Context::setStencilWritemask(GLuint stencilWritemask)
419{
420    if(mState.stencilWritemask != stencilWritemask)
421    {
422        mState.stencilWritemask = stencilWritemask;
423        mStencilStateDirty = true;
424    }
425}
426
427void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
428{
429    if(mState.stencilFail != stencilFail ||
430        mState.stencilPassDepthFail != stencilPassDepthFail ||
431        mState.stencilPassDepthPass != stencilPassDepthPass)
432    {
433        mState.stencilFail = stencilFail;
434        mState.stencilPassDepthFail = stencilPassDepthFail;
435        mState.stencilPassDepthPass = stencilPassDepthPass;
436        mStencilStateDirty = true;
437    }
438}
439
440void Context::setPolygonOffsetFill(bool enabled)
441{
442    if(mState.polygonOffsetFill != enabled)
443    {
444        mState.polygonOffsetFill = enabled;
445        mPolygonOffsetStateDirty = true;
446    }
447}
448
449bool Context::isPolygonOffsetFillEnabled() const
450{
451    return mState.polygonOffsetFill;
452}
453
454void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
455{
456    if(mState.polygonOffsetFactor != factor ||
457        mState.polygonOffsetUnits != units)
458    {
459        mState.polygonOffsetFactor = factor;
460        mState.polygonOffsetUnits = units;
461        mPolygonOffsetStateDirty = true;
462    }
463}
464
465void Context::setSampleAlphaToCoverage(bool enabled)
466{
467    if(mState.sampleAlphaToCoverage != enabled)
468    {
469        mState.sampleAlphaToCoverage = enabled;
470        mSampleStateDirty = true;
471    }
472}
473
474bool Context::isSampleAlphaToCoverageEnabled() const
475{
476    return mState.sampleAlphaToCoverage;
477}
478
479void Context::setSampleCoverage(bool enabled)
480{
481    if(mState.sampleCoverage != enabled)
482    {
483        mState.sampleCoverage = enabled;
484        mSampleStateDirty = true;
485    }
486}
487
488bool Context::isSampleCoverageEnabled() const
489{
490    return mState.sampleCoverage;
491}
492
493void Context::setSampleCoverageParams(GLclampf value, bool invert)
494{
495    if(mState.sampleCoverageValue != value ||
496        mState.sampleCoverageInvert != invert)
497    {
498        mState.sampleCoverageValue = value;
499        mState.sampleCoverageInvert = invert;
500        mSampleStateDirty = true;
501    }
502}
503
504void Context::setScissorTest(bool enabled)
505{
506    mState.scissorTest = enabled;
507}
508
509bool Context::isScissorTestEnabled() const
510{
511    return mState.scissorTest;
512}
513
514void Context::setDither(bool enabled)
515{
516    if(mState.dither != enabled)
517    {
518        mState.dither = enabled;
519        mDitherStateDirty = true;
520    }
521}
522
523bool Context::isDitherEnabled() const
524{
525    return mState.dither;
526}
527
528void Context::setLighting(bool enable)
529{
530    lighting = enable;
531}
532
533void Context::setLight(int index, bool enable)
534{
535    light[index].enable = enable;
536}
537
538void Context::setLightAmbient(int index, float r, float g, float b, float a)
539{
540	light[index].ambient = {r, g, b, a};
541}
542
543void Context::setLightDiffuse(int index, float r, float g, float b, float a)
544{
545	light[index].diffuse = {r, g, b, a};
546}
547
548void Context::setLightSpecular(int index, float r, float g, float b, float a)
549{
550	light[index].specular = {r, g, b, a};
551}
552
553void Context::setLightPosition(int index, float x, float y, float z, float w)
554{
555	light[index].position = {x, y, z, w};
556}
557
558void Context::setLightDirection(int index, float x, float y, float z)
559{
560	light[index].direction = {x, y, z};
561}
562
563void Context::setLightAttenuationConstant(int index, float constant)
564{
565	light[index].attenuation.constant = constant;
566}
567
568void Context::setLightAttenuationLinear(int index, float linear)
569{
570	light[index].attenuation.linear = linear;
571}
572
573void Context::setLightAttenuationQuadratic(int index, float quadratic)
574{
575	light[index].attenuation.quadratic = quadratic;
576}
577
578void Context::setFog(bool enable)
579{
580	device->setFogEnable(enable);
581}
582
583void Context::setFogMode(GLenum mode)
584{
585	switch(mode)
586	{
587	case GL_LINEAR:
588		device->setPixelFogMode(sw::FOG_LINEAR);
589		break;
590	case GL_EXP:
591		device->setPixelFogMode(sw::FOG_EXP);
592		break;
593	case GL_EXP2:
594		device->setPixelFogMode(sw::FOG_EXP2);
595		break;
596	default:
597		UNREACHABLE();
598	}
599}
600
601void Context::setFogDensity(float fogDensity)
602{
603	device->setFogDensity(fogDensity);
604}
605
606void Context::setFogStart(float fogStart)
607{
608	device->setFogStart(fogStart);
609}
610
611void Context::setFogEnd(float fogEnd)
612{
613	device->setFogEnd(fogEnd);
614}
615
616void Context::setFogColor(float r, float g, float b, float a)
617{
618	device->setFogColor(sw::Color<float>(r, g, b, a));
619}
620
621void Context::setTexture2D(bool enable)
622{
623    texture2D = enable;
624}
625
626void Context::setLineWidth(GLfloat width)
627{
628    mState.lineWidth = width;
629	device->setLineWidth(clamp(width, ALIASED_LINE_WIDTH_RANGE_MIN, ALIASED_LINE_WIDTH_RANGE_MAX));
630}
631
632void Context::setGenerateMipmapHint(GLenum hint)
633{
634    mState.generateMipmapHint = hint;
635}
636
637void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
638{
639    mState.viewportX = x;
640    mState.viewportY = y;
641    mState.viewportWidth = width;
642    mState.viewportHeight = height;
643}
644
645void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
646{
647    mState.scissorX = x;
648    mState.scissorY = y;
649    mState.scissorWidth = width;
650    mState.scissorHeight = height;
651}
652
653void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
654{
655    if(mState.colorMaskRed != red || mState.colorMaskGreen != green ||
656       mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
657    {
658        mState.colorMaskRed = red;
659        mState.colorMaskGreen = green;
660        mState.colorMaskBlue = blue;
661        mState.colorMaskAlpha = alpha;
662        mMaskStateDirty = true;
663    }
664}
665
666void Context::setDepthMask(bool mask)
667{
668    if(mState.depthMask != mask)
669    {
670        mState.depthMask = mask;
671        mMaskStateDirty = true;
672    }
673}
674
675void Context::setActiveSampler(unsigned int active)
676{
677    mState.activeSampler = active;
678}
679
680GLuint Context::getFramebufferName() const
681{
682    return mState.framebuffer;
683}
684
685GLuint Context::getRenderbufferName() const
686{
687    return mState.renderbuffer.name();
688}
689
690GLuint Context::getArrayBufferName() const
691{
692    return mState.arrayBuffer.name();
693}
694
695void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
696{
697    mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
698}
699
700const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
701{
702    return mState.vertexAttribute[attribNum];
703}
704
705void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
706                                   GLsizei stride, const void *pointer)
707{
708    mState.vertexAttribute[attribNum].mBoundBuffer = boundBuffer;
709    mState.vertexAttribute[attribNum].mSize = size;
710    mState.vertexAttribute[attribNum].mType = type;
711    mState.vertexAttribute[attribNum].mNormalized = normalized;
712    mState.vertexAttribute[attribNum].mStride = stride;
713    mState.vertexAttribute[attribNum].mPointer = pointer;
714}
715
716const void *Context::getVertexAttribPointer(unsigned int attribNum) const
717{
718    return mState.vertexAttribute[attribNum].mPointer;
719}
720
721const VertexAttributeArray &Context::getVertexAttributes()
722{
723    return mState.vertexAttribute;
724}
725
726void Context::setPackAlignment(GLint alignment)
727{
728    mState.packAlignment = alignment;
729}
730
731GLint Context::getPackAlignment() const
732{
733    return mState.packAlignment;
734}
735
736void Context::setUnpackAlignment(GLint alignment)
737{
738    mState.unpackAlignment = alignment;
739}
740
741GLint Context::getUnpackAlignment() const
742{
743    return mState.unpackAlignment;
744}
745
746GLuint Context::createBuffer()
747{
748    return mResourceManager->createBuffer();
749}
750
751GLuint Context::createTexture()
752{
753    return mResourceManager->createTexture();
754}
755
756GLuint Context::createRenderbuffer()
757{
758    return mResourceManager->createRenderbuffer();
759}
760
761// Returns an unused framebuffer name
762GLuint Context::createFramebuffer()
763{
764    GLuint handle = mFramebufferHandleAllocator.allocate();
765
766    mFramebufferMap[handle] = NULL;
767
768    return handle;
769}
770
771void Context::deleteBuffer(GLuint buffer)
772{
773    if(mResourceManager->getBuffer(buffer))
774    {
775        detachBuffer(buffer);
776    }
777
778    mResourceManager->deleteBuffer(buffer);
779}
780
781void Context::deleteTexture(GLuint texture)
782{
783    if(mResourceManager->getTexture(texture))
784    {
785        detachTexture(texture);
786    }
787
788    mResourceManager->deleteTexture(texture);
789}
790
791void Context::deleteRenderbuffer(GLuint renderbuffer)
792{
793    if(mResourceManager->getRenderbuffer(renderbuffer))
794    {
795        detachRenderbuffer(renderbuffer);
796    }
797
798    mResourceManager->deleteRenderbuffer(renderbuffer);
799}
800
801void Context::deleteFramebuffer(GLuint framebuffer)
802{
803    FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
804
805    if(framebufferObject != mFramebufferMap.end())
806    {
807        detachFramebuffer(framebuffer);
808
809        mFramebufferHandleAllocator.release(framebufferObject->first);
810        delete framebufferObject->second;
811        mFramebufferMap.erase(framebufferObject);
812    }
813}
814
815Buffer *Context::getBuffer(GLuint handle)
816{
817    return mResourceManager->getBuffer(handle);
818}
819
820Texture *Context::getTexture(GLuint handle)
821{
822    return mResourceManager->getTexture(handle);
823}
824
825Renderbuffer *Context::getRenderbuffer(GLuint handle)
826{
827    return mResourceManager->getRenderbuffer(handle);
828}
829
830Framebuffer *Context::getFramebuffer()
831{
832    return getFramebuffer(mState.framebuffer);
833}
834
835void Context::bindArrayBuffer(unsigned int buffer)
836{
837    mResourceManager->checkBufferAllocation(buffer);
838
839    mState.arrayBuffer = getBuffer(buffer);
840}
841
842void Context::bindElementArrayBuffer(unsigned int buffer)
843{
844    mResourceManager->checkBufferAllocation(buffer);
845
846    mState.elementArrayBuffer = getBuffer(buffer);
847}
848
849void Context::bindTexture2D(GLuint texture)
850{
851    mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
852
853    mState.samplerTexture[TEXTURE_2D][mState.activeSampler] = getTexture(texture);
854}
855
856void Context::bindTextureExternal(GLuint texture)
857{
858    mResourceManager->checkTextureAllocation(texture, TEXTURE_EXTERNAL);
859
860    mState.samplerTexture[TEXTURE_EXTERNAL][mState.activeSampler] = getTexture(texture);
861}
862
863void Context::bindFramebuffer(GLuint framebuffer)
864{
865    if(!getFramebuffer(framebuffer))
866    {
867        mFramebufferMap[framebuffer] = new Framebuffer();
868    }
869
870    mState.framebuffer = framebuffer;
871}
872
873void Context::bindRenderbuffer(GLuint renderbuffer)
874{
875    mState.renderbuffer = getRenderbuffer(renderbuffer);
876}
877
878void Context::setFramebufferZero(Framebuffer *buffer)
879{
880    delete mFramebufferMap[0];
881    mFramebufferMap[0] = buffer;
882}
883
884void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
885{
886    Renderbuffer *renderbufferObject = mState.renderbuffer;
887    renderbufferObject->setStorage(renderbuffer);
888}
889
890Framebuffer *Context::getFramebuffer(unsigned int handle)
891{
892    FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
893
894    if(framebuffer == mFramebufferMap.end())
895    {
896        return NULL;
897    }
898    else
899    {
900        return framebuffer->second;
901    }
902}
903
904Buffer *Context::getArrayBuffer()
905{
906    return mState.arrayBuffer;
907}
908
909Buffer *Context::getElementArrayBuffer()
910{
911    return mState.elementArrayBuffer;
912}
913
914Texture2D *Context::getTexture2D()
915{
916    return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
917}
918
919TextureExternal *Context::getTextureExternal()
920{
921    return static_cast<TextureExternal*>(getSamplerTexture(mState.activeSampler, TEXTURE_EXTERNAL));
922}
923
924Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
925{
926    GLuint texid = mState.samplerTexture[type][sampler].name();
927
928    if(texid == 0)   // Special case: 0 refers to different initial textures based on the target
929    {
930        switch (type)
931        {
932        case TEXTURE_2D: return mTexture2DZero;
933        case TEXTURE_EXTERNAL: return mTextureExternalZero;
934        default: UNREACHABLE();
935        }
936    }
937
938    return mState.samplerTexture[type][sampler];
939}
940
941bool Context::getBooleanv(GLenum pname, GLboolean *params)
942{
943    switch (pname)
944    {
945      case GL_SAMPLE_COVERAGE_INVERT:   *params = mState.sampleCoverageInvert;      break;
946      case GL_DEPTH_WRITEMASK:          *params = mState.depthMask;                 break;
947      case GL_COLOR_WRITEMASK:
948        params[0] = mState.colorMaskRed;
949        params[1] = mState.colorMaskGreen;
950        params[2] = mState.colorMaskBlue;
951        params[3] = mState.colorMaskAlpha;
952        break;
953      case GL_CULL_FACE:                *params = mState.cullFace;                  break;
954      case GL_POLYGON_OFFSET_FILL:      *params = mState.polygonOffsetFill;         break;
955      case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage;     break;
956      case GL_SAMPLE_COVERAGE:          *params = mState.sampleCoverage;            break;
957      case GL_SCISSOR_TEST:             *params = mState.scissorTest;               break;
958      case GL_STENCIL_TEST:             *params = mState.stencilTest;               break;
959      case GL_DEPTH_TEST:               *params = mState.depthTest;                 break;
960      case GL_BLEND:                    *params = mState.blend;                     break;
961      case GL_DITHER:                   *params = mState.dither;                    break;
962      default:
963        return false;
964    }
965
966    return true;
967}
968
969bool Context::getFloatv(GLenum pname, GLfloat *params)
970{
971    // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
972    // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
973    // GetIntegerv as its native query function. As it would require conversion in any
974    // case, this should make no difference to the calling application.
975    switch (pname)
976    {
977      case GL_LINE_WIDTH:               *params = mState.lineWidth;            break;
978      case GL_SAMPLE_COVERAGE_VALUE:    *params = mState.sampleCoverageValue;  break;
979      case GL_DEPTH_CLEAR_VALUE:        *params = mState.depthClearValue;      break;
980      case GL_POLYGON_OFFSET_FACTOR:    *params = mState.polygonOffsetFactor;  break;
981      case GL_POLYGON_OFFSET_UNITS:     *params = mState.polygonOffsetUnits;   break;
982      case GL_ALIASED_LINE_WIDTH_RANGE:
983        params[0] = ALIASED_LINE_WIDTH_RANGE_MIN;
984        params[1] = ALIASED_LINE_WIDTH_RANGE_MAX;
985        break;
986      case GL_ALIASED_POINT_SIZE_RANGE:
987        params[0] = ALIASED_POINT_SIZE_RANGE_MIN;
988        params[1] = ALIASED_POINT_SIZE_RANGE_MAX;
989        break;
990      case GL_DEPTH_RANGE:
991        params[0] = mState.zNear;
992        params[1] = mState.zFar;
993        break;
994      case GL_COLOR_CLEAR_VALUE:
995        params[0] = mState.colorClearValue.red;
996        params[1] = mState.colorClearValue.green;
997        params[2] = mState.colorClearValue.blue;
998        params[3] = mState.colorClearValue.alpha;
999        break;
1000	  case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1001        *params = MAX_TEXTURE_MAX_ANISOTROPY;
1002		break;
1003	  case GL_MODELVIEW_MATRIX:
1004		for(int i = 0; i < 16; i++)
1005		{
1006			params[i] = modelViewStack.current()[i % 4][i / 4];
1007		}
1008		break;
1009	  case GL_PROJECTION_MATRIX:
1010		for(int i = 0; i < 16; i++)
1011		{
1012			params[i] = projectionStack.current()[i % 4][i / 4];
1013		}
1014		break;
1015      default:
1016        return false;
1017    }
1018
1019    return true;
1020}
1021
1022bool Context::getIntegerv(GLenum pname, GLint *params)
1023{
1024    // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1025    // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1026    // GetIntegerv as its native query function. As it would require conversion in any
1027    // case, this should make no difference to the calling application. You may find it in
1028    // Context::getFloatv.
1029    switch (pname)
1030    {
1031    case GL_ARRAY_BUFFER_BINDING:             *params = mState.arrayBuffer.name();            break;
1032    case GL_ELEMENT_ARRAY_BUFFER_BINDING:     *params = mState.elementArrayBuffer.name();     break;
1033	case GL_FRAMEBUFFER_BINDING_OES:          *params = mState.framebuffer;                   break;
1034    case GL_RENDERBUFFER_BINDING_OES:         *params = mState.renderbuffer.name();           break;
1035    case GL_PACK_ALIGNMENT:                   *params = mState.packAlignment;                 break;
1036    case GL_UNPACK_ALIGNMENT:                 *params = mState.unpackAlignment;               break;
1037    case GL_GENERATE_MIPMAP_HINT:             *params = mState.generateMipmapHint;            break;
1038    case GL_ACTIVE_TEXTURE:                   *params = (mState.activeSampler + GL_TEXTURE0); break;
1039    case GL_STENCIL_FUNC:                     *params = mState.stencilFunc;                   break;
1040    case GL_STENCIL_REF:                      *params = mState.stencilRef;                    break;
1041    case GL_STENCIL_VALUE_MASK:               *params = mState.stencilMask;                   break;
1042    case GL_STENCIL_FAIL:                     *params = mState.stencilFail;                   break;
1043    case GL_STENCIL_PASS_DEPTH_FAIL:          *params = mState.stencilPassDepthFail;          break;
1044    case GL_STENCIL_PASS_DEPTH_PASS:          *params = mState.stencilPassDepthPass;          break;
1045    case GL_DEPTH_FUNC:                       *params = mState.depthFunc;                     break;
1046    case GL_BLEND_SRC_RGB_OES:                *params = mState.sourceBlendRGB;                break;
1047    case GL_BLEND_SRC_ALPHA_OES:              *params = mState.sourceBlendAlpha;              break;
1048    case GL_BLEND_DST_RGB_OES:                *params = mState.destBlendRGB;                  break;
1049    case GL_BLEND_DST_ALPHA_OES:              *params = mState.destBlendAlpha;                break;
1050    case GL_BLEND_EQUATION_RGB_OES:           *params = mState.blendEquationRGB;              break;
1051    case GL_BLEND_EQUATION_ALPHA_OES:         *params = mState.blendEquationAlpha;            break;
1052    case GL_STENCIL_WRITEMASK:                *params = mState.stencilWritemask;              break;
1053    case GL_STENCIL_CLEAR_VALUE:              *params = mState.stencilClearValue;             break;
1054    case GL_SUBPIXEL_BITS:                    *params = 4;                                    break;
1055	case GL_MAX_TEXTURE_SIZE:                 *params = IMPLEMENTATION_MAX_TEXTURE_SIZE;      break;
1056	case GL_NUM_COMPRESSED_TEXTURE_FORMATS:   *params = NUM_COMPRESSED_TEXTURE_FORMATS;       break;
1057	case GL_SAMPLE_BUFFERS:
1058    case GL_SAMPLES:
1059        {
1060            Framebuffer *framebuffer = getFramebuffer();
1061			int width, height, samples;
1062
1063            if(framebuffer->completeness(width, height, samples) == GL_FRAMEBUFFER_COMPLETE_OES)
1064            {
1065                switch(pname)
1066                {
1067                case GL_SAMPLE_BUFFERS:
1068                    if(samples > 1)
1069                    {
1070                        *params = 1;
1071                    }
1072                    else
1073                    {
1074                        *params = 0;
1075                    }
1076                    break;
1077                case GL_SAMPLES:
1078                    *params = samples & ~1;
1079                    break;
1080                }
1081            }
1082            else
1083            {
1084                *params = 0;
1085            }
1086        }
1087        break;
1088    case GL_IMPLEMENTATION_COLOR_READ_TYPE_OES:
1089		{
1090			Framebuffer *framebuffer = getFramebuffer();
1091			*params = framebuffer->getImplementationColorReadType();
1092		}
1093		break;
1094    case GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES:
1095		{
1096			Framebuffer *framebuffer = getFramebuffer();
1097			*params = framebuffer->getImplementationColorReadFormat();
1098		}
1099		break;
1100    case GL_MAX_VIEWPORT_DIMS:
1101        {
1102			int maxDimension = IMPLEMENTATION_MAX_RENDERBUFFER_SIZE;
1103            params[0] = maxDimension;
1104            params[1] = maxDimension;
1105        }
1106        break;
1107    case GL_COMPRESSED_TEXTURE_FORMATS:
1108        {
1109			for(int i = 0; i < NUM_COMPRESSED_TEXTURE_FORMATS; i++)
1110			{
1111				params[i] = compressedTextureFormats[i];
1112			}
1113        }
1114        break;
1115    case GL_VIEWPORT:
1116        params[0] = mState.viewportX;
1117        params[1] = mState.viewportY;
1118        params[2] = mState.viewportWidth;
1119        params[3] = mState.viewportHeight;
1120        break;
1121    case GL_SCISSOR_BOX:
1122        params[0] = mState.scissorX;
1123        params[1] = mState.scissorY;
1124        params[2] = mState.scissorWidth;
1125        params[3] = mState.scissorHeight;
1126        break;
1127    case GL_CULL_FACE_MODE:                   *params = mState.cullMode;                 break;
1128    case GL_FRONT_FACE:                       *params = mState.frontFace;                break;
1129    case GL_RED_BITS:
1130    case GL_GREEN_BITS:
1131    case GL_BLUE_BITS:
1132    case GL_ALPHA_BITS:
1133        {
1134            Framebuffer *framebuffer = getFramebuffer();
1135            Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1136
1137            if(colorbuffer)
1138            {
1139                switch (pname)
1140                {
1141                  case GL_RED_BITS:   *params = colorbuffer->getRedSize();   break;
1142                  case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1143                  case GL_BLUE_BITS:  *params = colorbuffer->getBlueSize();  break;
1144                  case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1145                }
1146            }
1147            else
1148            {
1149                *params = 0;
1150            }
1151        }
1152        break;
1153    case GL_DEPTH_BITS:
1154        {
1155            Framebuffer *framebuffer = getFramebuffer();
1156            Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
1157
1158            if(depthbuffer)
1159            {
1160                *params = depthbuffer->getDepthSize();
1161            }
1162            else
1163            {
1164                *params = 0;
1165            }
1166        }
1167        break;
1168    case GL_STENCIL_BITS:
1169        {
1170            Framebuffer *framebuffer = getFramebuffer();
1171            Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
1172
1173            if(stencilbuffer)
1174            {
1175                *params = stencilbuffer->getStencilSize();
1176            }
1177            else
1178            {
1179                *params = 0;
1180            }
1181        }
1182        break;
1183    case GL_TEXTURE_BINDING_2D:
1184        {
1185            if(mState.activeSampler < 0 || mState.activeSampler > MAX_TEXTURE_UNITS - 1)
1186            {
1187                error(GL_INVALID_OPERATION);
1188                return false;
1189            }
1190
1191            *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].name();
1192        }
1193        break;
1194    case GL_TEXTURE_BINDING_CUBE_MAP_OES:
1195        {
1196            if(mState.activeSampler < 0 || mState.activeSampler > MAX_TEXTURE_UNITS - 1)
1197            {
1198                error(GL_INVALID_OPERATION);
1199                return false;
1200            }
1201
1202            *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].name();
1203        }
1204        break;
1205    case GL_TEXTURE_BINDING_EXTERNAL_OES:
1206        {
1207            if(mState.activeSampler < 0 || mState.activeSampler > MAX_TEXTURE_UNITS - 1)
1208            {
1209                error(GL_INVALID_OPERATION);
1210                return false;
1211            }
1212
1213            *params = mState.samplerTexture[TEXTURE_EXTERNAL][mState.activeSampler].name();
1214        }
1215        break;
1216	case GL_MAX_LIGHTS:                 *params = MAX_LIGHTS;                 break;
1217    case GL_MAX_MODELVIEW_STACK_DEPTH:  *params = MAX_MODELVIEW_STACK_DEPTH;  break;
1218	case GL_MAX_PROJECTION_STACK_DEPTH: *params = MAX_PROJECTION_STACK_DEPTH; break;
1219	case GL_MAX_TEXTURE_STACK_DEPTH:    *params = MAX_TEXTURE_STACK_DEPTH;    break;
1220	case GL_MAX_TEXTURE_UNITS:          *params = MAX_TEXTURE_UNITS;          break;
1221    default:
1222        return false;
1223    }
1224
1225    return true;
1226}
1227
1228int Context::getQueryParameterNum(GLenum pname)
1229{
1230    // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1231    // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1232    // to the fact that it is stored internally as a float, and so would require conversion
1233    // if returned from Context::getIntegerv. Since this conversion is already implemented
1234    // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1235    // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1236    // application.
1237    switch (pname)
1238    {
1239    case GL_COMPRESSED_TEXTURE_FORMATS:
1240		return NUM_COMPRESSED_TEXTURE_FORMATS;
1241    case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1242    case GL_ARRAY_BUFFER_BINDING:
1243    case GL_FRAMEBUFFER_BINDING_OES:
1244    case GL_RENDERBUFFER_BINDING_OES:
1245    case GL_PACK_ALIGNMENT:
1246    case GL_UNPACK_ALIGNMENT:
1247    case GL_GENERATE_MIPMAP_HINT:
1248    case GL_RED_BITS:
1249    case GL_GREEN_BITS:
1250    case GL_BLUE_BITS:
1251    case GL_ALPHA_BITS:
1252    case GL_DEPTH_BITS:
1253    case GL_STENCIL_BITS:
1254    case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1255    case GL_CULL_FACE_MODE:
1256    case GL_FRONT_FACE:
1257    case GL_ACTIVE_TEXTURE:
1258    case GL_STENCIL_FUNC:
1259    case GL_STENCIL_VALUE_MASK:
1260    case GL_STENCIL_REF:
1261    case GL_STENCIL_FAIL:
1262    case GL_STENCIL_PASS_DEPTH_FAIL:
1263    case GL_STENCIL_PASS_DEPTH_PASS:
1264    case GL_DEPTH_FUNC:
1265    case GL_BLEND_SRC_RGB_OES:
1266    case GL_BLEND_SRC_ALPHA_OES:
1267    case GL_BLEND_DST_RGB_OES:
1268    case GL_BLEND_DST_ALPHA_OES:
1269    case GL_BLEND_EQUATION_RGB_OES:
1270    case GL_BLEND_EQUATION_ALPHA_OES:
1271    case GL_STENCIL_WRITEMASK:
1272    case GL_STENCIL_CLEAR_VALUE:
1273    case GL_SUBPIXEL_BITS:
1274    case GL_MAX_TEXTURE_SIZE:
1275    case GL_MAX_CUBE_MAP_TEXTURE_SIZE_OES:
1276    case GL_SAMPLE_BUFFERS:
1277    case GL_SAMPLES:
1278    case GL_IMPLEMENTATION_COLOR_READ_TYPE_OES:
1279    case GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES:
1280    case GL_TEXTURE_BINDING_2D:
1281    case GL_TEXTURE_BINDING_CUBE_MAP_OES:
1282    case GL_TEXTURE_BINDING_EXTERNAL_OES:
1283        return 1;
1284    case GL_MAX_VIEWPORT_DIMS:
1285        return 2;
1286    case GL_VIEWPORT:
1287    case GL_SCISSOR_BOX:
1288        return 4;
1289    case GL_SAMPLE_COVERAGE_INVERT:
1290    case GL_DEPTH_WRITEMASK:
1291    case GL_CULL_FACE:                // CULL_FACE through DITHER are natural to IsEnabled,
1292    case GL_POLYGON_OFFSET_FILL:      // but can be retrieved through the Get{Type}v queries.
1293    case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1294    case GL_SAMPLE_COVERAGE:
1295    case GL_SCISSOR_TEST:
1296    case GL_STENCIL_TEST:
1297    case GL_DEPTH_TEST:
1298    case GL_BLEND:
1299    case GL_DITHER:
1300        return 1;
1301    case GL_COLOR_WRITEMASK:
1302        return 4;
1303    case GL_POLYGON_OFFSET_FACTOR:
1304    case GL_POLYGON_OFFSET_UNITS:
1305    case GL_SAMPLE_COVERAGE_VALUE:
1306    case GL_DEPTH_CLEAR_VALUE:
1307    case GL_LINE_WIDTH:
1308        return 1;
1309    case GL_ALIASED_LINE_WIDTH_RANGE:
1310    case GL_ALIASED_POINT_SIZE_RANGE:
1311    case GL_DEPTH_RANGE:
1312        return 2;
1313    case GL_COLOR_CLEAR_VALUE:
1314        return 4;
1315	case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1316	case GL_MAX_LIGHTS:
1317	case GL_MAX_MODELVIEW_STACK_DEPTH:
1318	case GL_MAX_PROJECTION_STACK_DEPTH:
1319	case GL_MAX_TEXTURE_STACK_DEPTH:
1320	case GL_MAX_TEXTURE_UNITS:
1321        return 1;
1322	default:
1323		UNREACHABLE();
1324    }
1325
1326    return -1;
1327}
1328
1329bool Context::isQueryParameterInt(GLenum pname)
1330{
1331    // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1332    // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1333    // to the fact that it is stored internally as a float, and so would require conversion
1334    // if returned from Context::getIntegerv. Since this conversion is already implemented
1335    // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1336    // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1337    // application.
1338    switch(pname)
1339    {
1340    case GL_COMPRESSED_TEXTURE_FORMATS:
1341    case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1342    case GL_ARRAY_BUFFER_BINDING:
1343    case GL_FRAMEBUFFER_BINDING_OES:
1344    case GL_RENDERBUFFER_BINDING_OES:
1345    case GL_PACK_ALIGNMENT:
1346    case GL_UNPACK_ALIGNMENT:
1347    case GL_GENERATE_MIPMAP_HINT:
1348    case GL_RED_BITS:
1349    case GL_GREEN_BITS:
1350    case GL_BLUE_BITS:
1351    case GL_ALPHA_BITS:
1352    case GL_DEPTH_BITS:
1353    case GL_STENCIL_BITS:
1354    case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1355    case GL_CULL_FACE_MODE:
1356    case GL_FRONT_FACE:
1357    case GL_ACTIVE_TEXTURE:
1358    case GL_STENCIL_FUNC:
1359    case GL_STENCIL_VALUE_MASK:
1360    case GL_STENCIL_REF:
1361    case GL_STENCIL_FAIL:
1362    case GL_STENCIL_PASS_DEPTH_FAIL:
1363    case GL_STENCIL_PASS_DEPTH_PASS:
1364    case GL_DEPTH_FUNC:
1365    case GL_BLEND_SRC_RGB_OES:
1366    case GL_BLEND_SRC_ALPHA_OES:
1367    case GL_BLEND_DST_RGB_OES:
1368    case GL_BLEND_DST_ALPHA_OES:
1369    case GL_BLEND_EQUATION_RGB_OES:
1370    case GL_BLEND_EQUATION_ALPHA_OES:
1371    case GL_STENCIL_WRITEMASK:
1372    case GL_STENCIL_CLEAR_VALUE:
1373    case GL_SUBPIXEL_BITS:
1374    case GL_MAX_TEXTURE_SIZE:
1375    case GL_MAX_CUBE_MAP_TEXTURE_SIZE_OES:
1376    case GL_SAMPLE_BUFFERS:
1377    case GL_SAMPLES:
1378    case GL_IMPLEMENTATION_COLOR_READ_TYPE_OES:
1379    case GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES:
1380    case GL_TEXTURE_BINDING_2D:
1381    case GL_TEXTURE_BINDING_CUBE_MAP_OES:
1382    case GL_TEXTURE_BINDING_EXTERNAL_OES:
1383    case GL_MAX_VIEWPORT_DIMS:
1384    case GL_VIEWPORT:
1385    case GL_SCISSOR_BOX:
1386	case GL_MAX_LIGHTS:
1387	case GL_MAX_MODELVIEW_STACK_DEPTH:
1388	case GL_MAX_PROJECTION_STACK_DEPTH:
1389	case GL_MAX_TEXTURE_STACK_DEPTH:
1390	case GL_MAX_TEXTURE_UNITS:
1391        return true;
1392	default:
1393		ASSERT(isQueryParameterFloat(pname) || isQueryParameterBool(pname));
1394    }
1395
1396    return false;
1397}
1398
1399bool Context::isQueryParameterFloat(GLenum pname)
1400{
1401    // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1402    // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1403    // to the fact that it is stored internally as a float, and so would require conversion
1404    // if returned from Context::getIntegerv. Since this conversion is already implemented
1405    // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1406    // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1407    // application.
1408    switch(pname)
1409    {
1410    case GL_POLYGON_OFFSET_FACTOR:
1411    case GL_POLYGON_OFFSET_UNITS:
1412    case GL_SAMPLE_COVERAGE_VALUE:
1413    case GL_DEPTH_CLEAR_VALUE:
1414    case GL_LINE_WIDTH:
1415    case GL_ALIASED_LINE_WIDTH_RANGE:
1416    case GL_ALIASED_POINT_SIZE_RANGE:
1417    case GL_DEPTH_RANGE:
1418    case GL_COLOR_CLEAR_VALUE:
1419	case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1420        return true;
1421    default:
1422        ASSERT(isQueryParameterInt(pname) || isQueryParameterBool(pname));
1423    }
1424
1425    return false;
1426}
1427
1428bool Context::isQueryParameterBool(GLenum pname)
1429{
1430    switch(pname)
1431    {
1432    case GL_SAMPLE_COVERAGE_INVERT:
1433    case GL_DEPTH_WRITEMASK:
1434    case GL_CULL_FACE:                // CULL_FACE through DITHER are natural to IsEnabled,
1435    case GL_POLYGON_OFFSET_FILL:      // but can be retrieved through the Get{Type}v queries.
1436    case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1437    case GL_SAMPLE_COVERAGE:
1438    case GL_SCISSOR_TEST:
1439    case GL_STENCIL_TEST:
1440    case GL_DEPTH_TEST:
1441    case GL_BLEND:
1442    case GL_DITHER:
1443    case GL_COLOR_WRITEMASK:
1444        return true;
1445    default:
1446        ASSERT(isQueryParameterInt(pname) || isQueryParameterFloat(pname));
1447    }
1448
1449    return false;
1450}
1451
1452// Applies the render target surface, depth stencil surface, viewport rectangle and scissor rectangle
1453bool Context::applyRenderTarget()
1454{
1455    Framebuffer *framebuffer = getFramebuffer();
1456	int width, height, samples;
1457
1458    if(!framebuffer || framebuffer->completeness(width, height, samples) != GL_FRAMEBUFFER_COMPLETE_OES)
1459    {
1460        return error(GL_INVALID_FRAMEBUFFER_OPERATION_OES, false);
1461    }
1462
1463    egl::Image *renderTarget = framebuffer->getRenderTarget();
1464	device->setRenderTarget(renderTarget);
1465	if(renderTarget) renderTarget->release();
1466
1467    egl::Image *depthStencil = framebuffer->getDepthStencil();
1468    device->setDepthStencilSurface(depthStencil);
1469	if(depthStencil) depthStencil->release();
1470
1471    Viewport viewport;
1472    float zNear = clamp01(mState.zNear);
1473    float zFar = clamp01(mState.zFar);
1474
1475    viewport.x0 = mState.viewportX;
1476    viewport.y0 = mState.viewportY;
1477    viewport.width = mState.viewportWidth;
1478    viewport.height = mState.viewportHeight;
1479    viewport.minZ = zNear;
1480    viewport.maxZ = zFar;
1481
1482    device->setViewport(viewport);
1483
1484    if(mState.scissorTest)
1485    {
1486		sw::Rect scissor = {mState.scissorX, mState.scissorY, mState.scissorX + mState.scissorWidth, mState.scissorY + mState.scissorHeight};
1487		scissor.clip(0, 0, width, height);
1488
1489		device->setScissorRect(scissor);
1490        device->setScissorEnable(true);
1491    }
1492    else
1493    {
1494        device->setScissorEnable(false);
1495    }
1496
1497    return true;
1498}
1499
1500// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc)
1501void Context::applyState(GLenum drawMode)
1502{
1503    Framebuffer *framebuffer = getFramebuffer();
1504
1505    if(mState.cullFace)
1506    {
1507        device->setCullMode(es2sw::ConvertCullMode(mState.cullMode, mState.frontFace));
1508    }
1509    else
1510    {
1511		device->setCullMode(sw::CULL_NONE);
1512    }
1513
1514    if(mDepthStateDirty)
1515    {
1516        if(mState.depthTest)
1517        {
1518			device->setDepthBufferEnable(true);
1519			device->setDepthCompare(es2sw::ConvertDepthComparison(mState.depthFunc));
1520        }
1521        else
1522        {
1523            device->setDepthBufferEnable(false);
1524        }
1525
1526        mDepthStateDirty = false;
1527    }
1528
1529    if(mBlendStateDirty)
1530    {
1531        if(mState.blend)
1532        {
1533			device->setAlphaBlendEnable(true);
1534			device->setSeparateAlphaBlendEnable(true);
1535
1536			device->setSourceBlendFactor(es2sw::ConvertBlendFunc(mState.sourceBlendRGB));
1537			device->setDestBlendFactor(es2sw::ConvertBlendFunc(mState.destBlendRGB));
1538			device->setBlendOperation(es2sw::ConvertBlendOp(mState.blendEquationRGB));
1539
1540            device->setSourceBlendFactorAlpha(es2sw::ConvertBlendFunc(mState.sourceBlendAlpha));
1541			device->setDestBlendFactorAlpha(es2sw::ConvertBlendFunc(mState.destBlendAlpha));
1542			device->setBlendOperationAlpha(es2sw::ConvertBlendOp(mState.blendEquationAlpha));
1543        }
1544        else
1545        {
1546			device->setAlphaBlendEnable(false);
1547        }
1548
1549        mBlendStateDirty = false;
1550    }
1551
1552    if(mStencilStateDirty || mFrontFaceDirty)
1553    {
1554        if(mState.stencilTest && framebuffer->hasStencil())
1555        {
1556			device->setStencilEnable(true);
1557			device->setTwoSidedStencil(true);
1558
1559            // get the maximum size of the stencil ref
1560            Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
1561            GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1562
1563			device->setStencilWriteMask(mState.stencilWritemask);
1564			device->setStencilCompare(es2sw::ConvertStencilComparison(mState.stencilFunc));
1565
1566			device->setStencilReference((mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1567			device->setStencilMask(mState.stencilMask);
1568
1569			device->setStencilFailOperation(es2sw::ConvertStencilOp(mState.stencilFail));
1570			device->setStencilZFailOperation(es2sw::ConvertStencilOp(mState.stencilPassDepthFail));
1571			device->setStencilPassOperation(es2sw::ConvertStencilOp(mState.stencilPassDepthPass));
1572
1573			device->setStencilWriteMaskCCW(mState.stencilWritemask);
1574			device->setStencilCompareCCW(es2sw::ConvertStencilComparison(mState.stencilFunc));
1575
1576			device->setStencilReferenceCCW((mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1577			device->setStencilMaskCCW(mState.stencilMask);
1578
1579			device->setStencilFailOperationCCW(es2sw::ConvertStencilOp(mState.stencilFail));
1580			device->setStencilZFailOperationCCW(es2sw::ConvertStencilOp(mState.stencilPassDepthFail));
1581			device->setStencilPassOperationCCW(es2sw::ConvertStencilOp(mState.stencilPassDepthPass));
1582        }
1583        else
1584        {
1585			device->setStencilEnable(false);
1586        }
1587
1588        mStencilStateDirty = false;
1589        mFrontFaceDirty = false;
1590    }
1591
1592    if(mMaskStateDirty)
1593    {
1594		device->setColorWriteMask(0, es2sw::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
1595		device->setDepthWriteEnable(mState.depthMask);
1596
1597        mMaskStateDirty = false;
1598    }
1599
1600    if(mPolygonOffsetStateDirty)
1601    {
1602        if(mState.polygonOffsetFill)
1603        {
1604            Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
1605            if(depthbuffer)
1606            {
1607				device->setSlopeDepthBias(mState.polygonOffsetFactor);
1608                float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
1609				device->setDepthBias(depthBias);
1610            }
1611        }
1612        else
1613        {
1614            device->setSlopeDepthBias(0);
1615            device->setDepthBias(0);
1616        }
1617
1618        mPolygonOffsetStateDirty = false;
1619    }
1620
1621    if(mSampleStateDirty)
1622    {
1623        if(mState.sampleAlphaToCoverage)
1624        {
1625            device->setTransparencyAntialiasing(sw::TRANSPARENCY_ALPHA_TO_COVERAGE);
1626        }
1627		else
1628		{
1629			device->setTransparencyAntialiasing(sw::TRANSPARENCY_NONE);
1630		}
1631
1632        if(mState.sampleCoverage)
1633        {
1634            unsigned int mask = 0;
1635            if(mState.sampleCoverageValue != 0)
1636            {
1637				int width, height, samples;
1638				framebuffer->completeness(width, height, samples);
1639
1640                float threshold = 0.5f;
1641
1642                for(int i = 0; i < samples; i++)
1643                {
1644                    mask <<= 1;
1645
1646                    if((i + 1) * mState.sampleCoverageValue >= threshold)
1647                    {
1648                        threshold += 1.0f;
1649                        mask |= 1;
1650                    }
1651                }
1652            }
1653
1654            if(mState.sampleCoverageInvert)
1655            {
1656                mask = ~mask;
1657            }
1658
1659			device->setMultiSampleMask(mask);
1660        }
1661        else
1662        {
1663			device->setMultiSampleMask(0xFFFFFFFF);
1664        }
1665
1666        mSampleStateDirty = false;
1667    }
1668
1669    if(mDitherStateDirty)
1670    {
1671    //	UNIMPLEMENTED();   // FIXME
1672
1673        mDitherStateDirty = false;
1674    }
1675
1676	device->setLightingEnable(lighting);
1677	device->setGlobalAmbient(sw::Color<float>(globalAmbient.red, globalAmbient.green, globalAmbient.blue, globalAmbient.alpha));
1678
1679	for(int i = 0; i < MAX_LIGHTS; i++)
1680	{
1681		device->setLightEnable(i, light[i].enable);
1682		device->setLightAmbient(i, sw::Color<float>(light[i].ambient.red, light[i].ambient.green, light[i].ambient.blue, light[i].ambient.alpha));
1683		device->setLightDiffuse(i, sw::Color<float>(light[i].diffuse.red, light[i].diffuse.green, light[i].diffuse.blue, light[i].diffuse.alpha));
1684		device->setLightSpecular(i, sw::Color<float>(light[i].specular.red, light[i].specular.green, light[i].specular.blue, light[i].specular.alpha));
1685		device->setLightAttenuation(i, light[i].attenuation.constant, light[i].attenuation.linear, light[i].attenuation.quadratic);
1686
1687		if(light[i].position.w != 0.0f)
1688		{
1689			device->setLightPosition(i, sw::Point(light[i].position.x / light[i].position.w, light[i].position.y / light[i].position.w, light[i].position.z / light[i].position.w));
1690		}
1691		else   // Hack: set the position far way
1692		{
1693			device->setLightPosition(i, sw::Point(1e10f * light[i].position.x, 1e10f * light[i].position.y, 1e10f * light[i].position.z));
1694		}
1695	}
1696
1697	device->setMaterialAmbient(sw::Color<float>(materialAmbient.red, materialAmbient.green, materialAmbient.blue, materialAmbient.alpha));
1698	device->setMaterialDiffuse(sw::Color<float>(materialDiffuse.red, materialDiffuse.green, materialDiffuse.blue, materialDiffuse.alpha));
1699	device->setMaterialSpecular(sw::Color<float>(materialSpecular.red, materialSpecular.green, materialSpecular.blue, materialSpecular.alpha));
1700	device->setMaterialEmission(sw::Color<float>(materialEmission.red, materialEmission.green, materialEmission.blue, materialEmission.alpha));
1701
1702    device->setDiffuseMaterialSource(sw::MATERIAL_MATERIAL);
1703	device->setSpecularMaterialSource(sw::MATERIAL_MATERIAL);
1704	device->setAmbientMaterialSource(sw::MATERIAL_MATERIAL);
1705	device->setEmissiveMaterialSource(sw::MATERIAL_MATERIAL);
1706
1707    device->setProjectionMatrix(projectionStack.current());
1708    device->setModelMatrix(modelViewStack.current());
1709    device->setTextureMatrix(0, textureStack0.current());
1710	device->setTextureMatrix(1, textureStack1.current());
1711	device->setTextureTransform(0, textureStack0.isIdentity() ? 0 : 4, false);
1712	device->setTextureTransform(1, textureStack1.isIdentity() ? 0 : 4, false);
1713}
1714
1715GLenum Context::applyVertexBuffer(GLint base, GLint first, GLsizei count)
1716{
1717    TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
1718
1719    GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
1720    if(err != GL_NO_ERROR)
1721    {
1722        return err;
1723    }
1724
1725	device->resetInputStreams(false);
1726
1727    for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
1728	{
1729		sw::Resource *resource = attributes[i].vertexBuffer;
1730		const void *buffer = (char*)resource->data() + attributes[i].offset;
1731
1732		int stride = attributes[i].stride;
1733
1734		buffer = (char*)buffer + stride * base;
1735
1736		sw::Stream attribute(resource, buffer, stride);
1737
1738		attribute.type = attributes[i].type;
1739		attribute.count = attributes[i].count;
1740		attribute.normalized = attributes[i].normalized;
1741
1742		device->setInputStream(i, attribute);
1743	}
1744
1745	return GL_NO_ERROR;
1746}
1747
1748// Applies the indices and element array bindings
1749GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
1750{
1751    GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer, indices, indexInfo);
1752
1753    if(err == GL_NO_ERROR)
1754    {
1755        device->setIndexBuffer(indexInfo->indexBuffer);
1756    }
1757
1758    return err;
1759}
1760
1761void Context::applyTextures()
1762{
1763	GLenum texEnvMode = getTextureEnvMode();
1764
1765	for(int samplerIndex = 0; samplerIndex < MAX_TEXTURE_UNITS; samplerIndex++)
1766    {
1767        Texture *texture = getSamplerTexture(samplerIndex, TEXTURE_2D);
1768
1769		if(texture2D && texture->isSamplerComplete())
1770        {
1771            GLenum wrapS = texture->getWrapS();
1772            GLenum wrapT = texture->getWrapT();
1773            GLenum texFilter = texture->getMinFilter();
1774            GLenum magFilter = texture->getMagFilter();
1775			GLfloat maxAnisotropy = texture->getMaxAnisotropy();
1776
1777			device->setAddressingModeU(sw::SAMPLER_PIXEL, samplerIndex, es2sw::ConvertTextureWrap(wrapS));
1778            device->setAddressingModeV(sw::SAMPLER_PIXEL, samplerIndex, es2sw::ConvertTextureWrap(wrapT));
1779
1780			sw::FilterType minFilter;
1781			sw::MipmapType mipFilter;
1782            es2sw::ConvertMinFilter(texFilter, &minFilter, &mipFilter, maxAnisotropy);
1783		//	ASSERT(minFilter == es2sw::ConvertMagFilter(magFilter));
1784
1785			device->setTextureFilter(sw::SAMPLER_PIXEL, samplerIndex, minFilter);
1786		//	device->setTextureFilter(sw::SAMPLER_PIXEL, samplerIndex, es2sw::ConvertMagFilter(magFilter));
1787			device->setMipmapFilter(sw::SAMPLER_PIXEL, samplerIndex, mipFilter);
1788			device->setMaxAnisotropy(sw::SAMPLER_PIXEL, samplerIndex, maxAnisotropy);
1789
1790			applyTexture(samplerIndex, texture);
1791
1792			GLenum texFormat = texture->getFormat(GL_TEXTURE_2D, 0);
1793			sw::TextureStage::StageOperation rgbOperation, alphaOperation;
1794			es2sw::ConvertTextureOperations(texEnvMode, texFormat, &rgbOperation, &alphaOperation);
1795
1796			device->setStageOperation(samplerIndex, rgbOperation);
1797            device->setFirstArgument(samplerIndex, sw::TextureStage::SOURCE_TEXTURE);
1798			device->setFirstModifier(samplerIndex, sw::TextureStage::MODIFIER_COLOR);
1799			device->setSecondArgument(samplerIndex, sw::TextureStage::SOURCE_CURRENT);
1800			device->setSecondModifier(samplerIndex, sw::TextureStage::MODIFIER_COLOR);
1801
1802            device->setStageOperationAlpha(samplerIndex, alphaOperation);
1803			device->setFirstArgumentAlpha(samplerIndex, sw::TextureStage::SOURCE_TEXTURE);
1804			device->setFirstModifierAlpha(samplerIndex, sw::TextureStage::MODIFIER_ALPHA);
1805			device->setSecondArgumentAlpha(samplerIndex, sw::TextureStage::SOURCE_CURRENT);
1806			device->setSecondModifierAlpha(samplerIndex, sw::TextureStage::MODIFIER_ALPHA);
1807        }
1808        else
1809        {
1810            applyTexture(samplerIndex, 0);
1811
1812			device->setStageOperation(samplerIndex, sw::TextureStage::STAGE_SELECTARG1);
1813			device->setFirstArgument(samplerIndex, sw::TextureStage::SOURCE_CURRENT);
1814			device->setFirstModifier(samplerIndex, sw::TextureStage::MODIFIER_COLOR);
1815			device->setSecondArgument(samplerIndex, sw::TextureStage::SOURCE_CURRENT);
1816			device->setSecondModifier(samplerIndex, sw::TextureStage::MODIFIER_COLOR);
1817
1818            device->setStageOperationAlpha(samplerIndex, sw::TextureStage::STAGE_SELECTARG1);
1819			device->setFirstArgumentAlpha(samplerIndex, sw::TextureStage::SOURCE_CURRENT);
1820			device->setFirstModifierAlpha(samplerIndex, sw::TextureStage::MODIFIER_ALPHA);
1821			device->setSecondArgumentAlpha(samplerIndex, sw::TextureStage::SOURCE_CURRENT);
1822			device->setSecondModifierAlpha(samplerIndex, sw::TextureStage::MODIFIER_ALPHA);
1823        }
1824    }
1825}
1826
1827void Context::setTextureEnvMode(GLenum texEnvMode)
1828{
1829	switch(texEnvMode)
1830	{
1831	case GL_MODULATE:
1832	case GL_DECAL:
1833	case GL_BLEND:
1834	case GL_ADD:
1835	case GL_REPLACE:
1836		mState.textureEnvMode = texEnvMode;
1837		break;
1838	default:
1839		UNREACHABLE();
1840	}
1841}
1842
1843void Context::applyTexture(int index, Texture *baseTexture)
1844{
1845	sw::Resource *resource = 0;
1846
1847	if(baseTexture)
1848	{
1849		resource = baseTexture->getResource();
1850	}
1851
1852	device->setTextureResource(index, resource);
1853
1854	if(baseTexture)
1855	{
1856		int levelCount = baseTexture->getLevelCount();
1857
1858		if(baseTexture->getTarget() == GL_TEXTURE_2D || baseTexture->getTarget() == GL_TEXTURE_EXTERNAL_OES)
1859		{
1860			Texture2D *texture = static_cast<Texture2D*>(baseTexture);
1861
1862			for(int mipmapLevel = 0; mipmapLevel < MIPMAP_LEVELS; mipmapLevel++)
1863			{
1864				int surfaceLevel = mipmapLevel;
1865
1866				if(surfaceLevel < 0)
1867				{
1868					surfaceLevel = 0;
1869				}
1870				else if(surfaceLevel >= levelCount)
1871				{
1872					surfaceLevel = levelCount - 1;
1873				}
1874
1875				egl::Image *surface = texture->getImage(surfaceLevel);
1876				device->setTextureLevel(index, 0, mipmapLevel, surface, sw::TEXTURE_2D);
1877			}
1878		}
1879		else UNIMPLEMENTED();
1880	}
1881	else
1882	{
1883		device->setTextureLevel(index, 0, 0, 0, sw::TEXTURE_NULL);
1884	}
1885}
1886
1887void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
1888                         GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
1889{
1890    Framebuffer *framebuffer = getFramebuffer();
1891	int framebufferWidth, framebufferHeight, framebufferSamples;
1892
1893    if(framebuffer->completeness(framebufferWidth, framebufferHeight, framebufferSamples) != GL_FRAMEBUFFER_COMPLETE_OES)
1894    {
1895        return error(GL_INVALID_FRAMEBUFFER_OPERATION_OES);
1896    }
1897
1898    if(getFramebufferName() != 0 && framebufferSamples != 0)
1899    {
1900        return error(GL_INVALID_OPERATION);
1901    }
1902
1903	if(format != GL_RGBA || type != GL_UNSIGNED_BYTE)
1904	{
1905		if(format != framebuffer->getImplementationColorReadFormat() || type != framebuffer->getImplementationColorReadType())
1906		{
1907			return error(GL_INVALID_OPERATION);
1908		}
1909	}
1910
1911	GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
1912
1913	// Sized query sanity check
1914    if(bufSize)
1915    {
1916        int requiredSize = outputPitch * height;
1917        if(requiredSize > *bufSize)
1918        {
1919            return error(GL_INVALID_OPERATION);
1920        }
1921    }
1922
1923    egl::Image *renderTarget = framebuffer->getRenderTarget();
1924
1925    if(!renderTarget)
1926    {
1927        return error(GL_OUT_OF_MEMORY);
1928    }
1929
1930	sw::Rect rect = {x, y, x + width, y + height};
1931	rect.clip(0, 0, renderTarget->getWidth(), renderTarget->getHeight());
1932
1933    unsigned char *source = (unsigned char*)renderTarget->lock(rect.x0, rect.y0, sw::LOCK_READONLY);
1934    unsigned char *dest = (unsigned char*)pixels;
1935    int inputPitch = (int)renderTarget->getPitch();
1936
1937    for(int j = 0; j < rect.y1 - rect.y0; j++)
1938    {
1939		unsigned short *dest16 = (unsigned short*)dest;
1940		unsigned int *dest32 = (unsigned int*)dest;
1941
1942		if(renderTarget->getInternalFormat() == sw::FORMAT_A8R8G8B8 &&
1943           format == GL_RGBA && type == GL_UNSIGNED_BYTE)
1944        {
1945            for(int i = 0; i < rect.x1 - rect.x0; i++)
1946			{
1947				unsigned int argb = *(unsigned int*)(source + 4 * i);
1948
1949				dest32[i] = (argb & 0xFF00FF00) | ((argb & 0x000000FF) << 16) | ((argb & 0x00FF0000) >> 16);
1950			}
1951        }
1952		else if(renderTarget->getInternalFormat() == sw::FORMAT_X8R8G8B8 &&
1953                format == GL_RGBA && type == GL_UNSIGNED_BYTE)
1954        {
1955            for(int i = 0; i < rect.x1 - rect.x0; i++)
1956			{
1957				unsigned int xrgb = *(unsigned int*)(source + 4 * i);
1958
1959				dest32[i] = (xrgb & 0xFF00FF00) | ((xrgb & 0x000000FF) << 16) | ((xrgb & 0x00FF0000) >> 16) | 0xFF000000;
1960			}
1961        }
1962		else if(renderTarget->getInternalFormat() == sw::FORMAT_X8R8G8B8 &&
1963                format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE)
1964        {
1965            for(int i = 0; i < rect.x1 - rect.x0; i++)
1966			{
1967				unsigned int xrgb = *(unsigned int*)(source + 4 * i);
1968
1969				dest32[i] = xrgb | 0xFF000000;
1970			}
1971        }
1972        else if(renderTarget->getInternalFormat() == sw::FORMAT_A8R8G8B8 &&
1973                format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE)
1974        {
1975            memcpy(dest, source, (rect.x1 - rect.x0) * 4);
1976        }
1977		else if(renderTarget->getInternalFormat() == sw::FORMAT_A1R5G5B5 &&
1978                format == GL_BGRA_EXT && type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT)
1979        {
1980            memcpy(dest, source, (rect.x1 - rect.x0) * 2);
1981        }
1982		else if(renderTarget->getInternalFormat() == sw::FORMAT_R5G6B5 &&
1983                format == 0x80E0 && type == GL_UNSIGNED_SHORT_5_6_5)   // GL_BGR_EXT
1984        {
1985            memcpy(dest, source, (rect.x1 - rect.x0) * 2);
1986        }
1987		else
1988		{
1989			for(int i = 0; i < rect.x1 - rect.x0; i++)
1990			{
1991				float r;
1992				float g;
1993				float b;
1994				float a;
1995
1996				switch(renderTarget->getInternalFormat())
1997				{
1998				case sw::FORMAT_R5G6B5:
1999					{
2000						unsigned short rgb = *(unsigned short*)(source + 2 * i);
2001
2002						a = 1.0f;
2003						b = (rgb & 0x001F) * (1.0f / 0x001F);
2004						g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2005						r = (rgb & 0xF800) * (1.0f / 0xF800);
2006					}
2007					break;
2008				case sw::FORMAT_A1R5G5B5:
2009					{
2010						unsigned short argb = *(unsigned short*)(source + 2 * i);
2011
2012						a = (argb & 0x8000) ? 1.0f : 0.0f;
2013						b = (argb & 0x001F) * (1.0f / 0x001F);
2014						g = (argb & 0x03E0) * (1.0f / 0x03E0);
2015						r = (argb & 0x7C00) * (1.0f / 0x7C00);
2016					}
2017					break;
2018				case sw::FORMAT_A8R8G8B8:
2019					{
2020						unsigned int argb = *(unsigned int*)(source + 4 * i);
2021
2022						a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2023						b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2024						g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2025						r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2026					}
2027					break;
2028				case sw::FORMAT_X8R8G8B8:
2029					{
2030						unsigned int xrgb = *(unsigned int*)(source + 4 * i);
2031
2032						a = 1.0f;
2033						b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2034						g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2035						r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2036					}
2037					break;
2038				case sw::FORMAT_A2R10G10B10:
2039					{
2040						unsigned int argb = *(unsigned int*)(source + 4 * i);
2041
2042						a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2043						b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2044						g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2045						r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2046					}
2047					break;
2048				default:
2049					UNIMPLEMENTED();   // FIXME
2050					UNREACHABLE();
2051				}
2052
2053				switch(format)
2054				{
2055				case GL_RGBA:
2056					switch(type)
2057					{
2058					case GL_UNSIGNED_BYTE:
2059						dest[4 * i + 0] = (unsigned char)(255 * r + 0.5f);
2060						dest[4 * i + 1] = (unsigned char)(255 * g + 0.5f);
2061						dest[4 * i + 2] = (unsigned char)(255 * b + 0.5f);
2062						dest[4 * i + 3] = (unsigned char)(255 * a + 0.5f);
2063						break;
2064					default: UNREACHABLE();
2065					}
2066					break;
2067				case GL_BGRA_EXT:
2068					switch(type)
2069					{
2070					case GL_UNSIGNED_BYTE:
2071						dest[4 * i + 0] = (unsigned char)(255 * b + 0.5f);
2072						dest[4 * i + 1] = (unsigned char)(255 * g + 0.5f);
2073						dest[4 * i + 2] = (unsigned char)(255 * r + 0.5f);
2074						dest[4 * i + 3] = (unsigned char)(255 * a + 0.5f);
2075						break;
2076					case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2077						// According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2078						// this type is packed as follows:
2079						//   15   14   13   12   11   10    9    8    7    6    5    4    3    2    1    0
2080						//  --------------------------------------------------------------------------------
2081						// |       4th         |        3rd         |        2nd        |   1st component   |
2082						//  --------------------------------------------------------------------------------
2083						// in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2084						dest16[i] =
2085							((unsigned short)(15 * a + 0.5f) << 12)|
2086							((unsigned short)(15 * r + 0.5f) << 8) |
2087							((unsigned short)(15 * g + 0.5f) << 4) |
2088							((unsigned short)(15 * b + 0.5f) << 0);
2089						break;
2090					case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2091						// According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2092						// this type is packed as follows:
2093						//   15   14   13   12   11   10    9    8    7    6    5    4    3    2    1    0
2094						//  --------------------------------------------------------------------------------
2095						// | 4th |          3rd           |           2nd          |      1st component     |
2096						//  --------------------------------------------------------------------------------
2097						// in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2098						dest16[i] =
2099							((unsigned short)(     a + 0.5f) << 15) |
2100							((unsigned short)(31 * r + 0.5f) << 10) |
2101							((unsigned short)(31 * g + 0.5f) << 5) |
2102							((unsigned short)(31 * b + 0.5f) << 0);
2103						break;
2104					default: UNREACHABLE();
2105					}
2106					break;
2107				case GL_RGB:
2108					switch(type)
2109					{
2110					case GL_UNSIGNED_SHORT_5_6_5:
2111						dest16[i] =
2112							((unsigned short)(31 * b + 0.5f) << 0) |
2113							((unsigned short)(63 * g + 0.5f) << 5) |
2114							((unsigned short)(31 * r + 0.5f) << 11);
2115						break;
2116					default: UNREACHABLE();
2117					}
2118					break;
2119				default: UNREACHABLE();
2120				}
2121			}
2122        }
2123
2124		source += inputPitch;
2125		dest += outputPitch;
2126    }
2127
2128	renderTarget->unlock();
2129	renderTarget->release();
2130}
2131
2132void Context::clear(GLbitfield mask)
2133{
2134    Framebuffer *framebuffer = getFramebuffer();
2135
2136    if(!framebuffer || framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE_OES)
2137    {
2138        return error(GL_INVALID_FRAMEBUFFER_OPERATION_OES);
2139    }
2140
2141    if(!applyRenderTarget())
2142    {
2143        return;
2144    }
2145
2146	unsigned int color = (unorm<8>(mState.colorClearValue.alpha) << 24) |
2147                         (unorm<8>(mState.colorClearValue.red) << 16) |
2148                         (unorm<8>(mState.colorClearValue.green) << 8) |
2149                         (unorm<8>(mState.colorClearValue.blue) << 0);
2150    float depth = clamp01(mState.depthClearValue);
2151    int stencil = mState.stencilClearValue & 0x000000FF;
2152
2153	if(mask & GL_COLOR_BUFFER_BIT)
2154	{
2155		unsigned int rgbaMask = (mState.colorMaskRed ? 0x1 : 0) |
2156		                        (mState.colorMaskGreen ? 0x2 : 0) |
2157		                        (mState.colorMaskBlue ? 0x4 : 0) |
2158		                        (mState.colorMaskAlpha ? 0x8 : 0);
2159
2160		if(rgbaMask != 0)
2161		{
2162			device->clearColor(color, rgbaMask);
2163		}
2164	}
2165
2166	if(mask & GL_DEPTH_BUFFER_BIT)
2167	{
2168		if(mState.depthMask != 0)
2169		{
2170			device->clearDepth(depth);
2171		}
2172	}
2173
2174	if(mask & GL_STENCIL_BUFFER_BIT)
2175	{
2176		if(mState.stencilWritemask != 0)
2177		{
2178			device->clearStencil(stencil, mState.stencilWritemask);
2179		}
2180	}
2181}
2182
2183void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2184{
2185    PrimitiveType primitiveType;
2186    int primitiveCount;
2187
2188    if(!es2sw::ConvertPrimitiveType(mode, count, primitiveType, primitiveCount))
2189        return error(GL_INVALID_ENUM);
2190
2191    if(primitiveCount <= 0)
2192    {
2193        return;
2194    }
2195
2196    if(!applyRenderTarget())
2197    {
2198        return;
2199    }
2200
2201    applyState(mode);
2202
2203    GLenum err = applyVertexBuffer(0, first, count);
2204    if(err != GL_NO_ERROR)
2205    {
2206        return error(err);
2207    }
2208
2209    applyTextures();
2210
2211    if(!cullSkipsDraw(mode))
2212    {
2213        device->drawPrimitive(primitiveType, primitiveCount);
2214    }
2215}
2216
2217void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
2218{
2219    if(!indices && !mState.elementArrayBuffer)
2220    {
2221        return error(GL_INVALID_OPERATION);
2222    }
2223
2224    PrimitiveType primitiveType;
2225    int primitiveCount;
2226
2227    if(!es2sw::ConvertPrimitiveType(mode, count, primitiveType, primitiveCount))
2228        return error(GL_INVALID_ENUM);
2229
2230    if(primitiveCount <= 0)
2231    {
2232        return;
2233    }
2234
2235    if(!applyRenderTarget())
2236    {
2237        return;
2238    }
2239
2240    applyState(mode);
2241
2242    TranslatedIndexData indexInfo;
2243    GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2244    if(err != GL_NO_ERROR)
2245    {
2246        return error(err);
2247    }
2248
2249    GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2250    err = applyVertexBuffer(-(int)indexInfo.minIndex, indexInfo.minIndex, vertexCount);
2251    if(err != GL_NO_ERROR)
2252    {
2253        return error(err);
2254    }
2255
2256    applyTextures();
2257
2258    if(!cullSkipsDraw(mode))
2259    {
2260		device->drawIndexedPrimitive(primitiveType, indexInfo.indexOffset, primitiveCount, IndexDataManager::typeSize(type));
2261    }
2262}
2263
2264void Context::finish()
2265{
2266	device->finish();
2267}
2268
2269void Context::flush()
2270{
2271    // We don't queue anything without processing it as fast as possible
2272}
2273
2274void Context::recordInvalidEnum()
2275{
2276    mInvalidEnum = true;
2277}
2278
2279void Context::recordInvalidValue()
2280{
2281    mInvalidValue = true;
2282}
2283
2284void Context::recordInvalidOperation()
2285{
2286    mInvalidOperation = true;
2287}
2288
2289void Context::recordOutOfMemory()
2290{
2291    mOutOfMemory = true;
2292}
2293
2294void Context::recordInvalidFramebufferOperation()
2295{
2296    mInvalidFramebufferOperation = true;
2297}
2298
2299// Get one of the recorded errors and clear its flag, if any.
2300// [OpenGL ES 2.0.24] section 2.5 page 13.
2301GLenum Context::getError()
2302{
2303    if(mInvalidEnum)
2304    {
2305        mInvalidEnum = false;
2306
2307        return GL_INVALID_ENUM;
2308    }
2309
2310    if(mInvalidValue)
2311    {
2312        mInvalidValue = false;
2313
2314        return GL_INVALID_VALUE;
2315    }
2316
2317    if(mInvalidOperation)
2318    {
2319        mInvalidOperation = false;
2320
2321        return GL_INVALID_OPERATION;
2322    }
2323
2324    if(mOutOfMemory)
2325    {
2326        mOutOfMemory = false;
2327
2328        return GL_OUT_OF_MEMORY;
2329    }
2330
2331    if(mInvalidFramebufferOperation)
2332    {
2333        mInvalidFramebufferOperation = false;
2334
2335        return GL_INVALID_FRAMEBUFFER_OPERATION_OES;
2336    }
2337
2338    return GL_NO_ERROR;
2339}
2340
2341int Context::getSupportedMultiSampleDepth(sw::Format format, int requested)
2342{
2343    if(requested <= 1)
2344    {
2345        return 1;
2346    }
2347
2348	if(requested == 2)
2349	{
2350		return 2;
2351	}
2352
2353	return 4;
2354}
2355
2356void Context::detachBuffer(GLuint buffer)
2357{
2358    // [OpenGL ES 2.0.24] section 2.9 page 22:
2359    // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2360    // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2361
2362    if(mState.arrayBuffer.name() == buffer)
2363    {
2364        mState.arrayBuffer = NULL;
2365    }
2366
2367    if(mState.elementArrayBuffer.name() == buffer)
2368    {
2369        mState.elementArrayBuffer = NULL;
2370    }
2371
2372    for(int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2373    {
2374        if(mState.vertexAttribute[attribute].mBoundBuffer.name() == buffer)
2375        {
2376            mState.vertexAttribute[attribute].mBoundBuffer = NULL;
2377        }
2378    }
2379}
2380
2381void Context::detachTexture(GLuint texture)
2382{
2383    // [OpenGL ES 2.0.24] section 3.8 page 84:
2384    // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2385    // rebound to texture object zero
2386
2387    for(int type = 0; type < TEXTURE_TYPE_COUNT; type++)
2388    {
2389        for(int sampler = 0; sampler < MAX_TEXTURE_UNITS; sampler++)
2390        {
2391            if(mState.samplerTexture[type][sampler].name() == texture)
2392            {
2393                mState.samplerTexture[type][sampler] = NULL;
2394            }
2395        }
2396    }
2397
2398    // [OpenGL ES 2.0.24] section 4.4 page 112:
2399    // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2400    // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2401    // image was attached in the currently bound framebuffer.
2402
2403    Framebuffer *framebuffer = getFramebuffer();
2404
2405    if(framebuffer)
2406    {
2407        framebuffer->detachTexture(texture);
2408    }
2409}
2410
2411void Context::detachFramebuffer(GLuint framebuffer)
2412{
2413    // [OpenGL ES 2.0.24] section 4.4 page 107:
2414    // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2415    // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2416
2417    if(mState.framebuffer == framebuffer)
2418    {
2419        bindFramebuffer(0);
2420    }
2421}
2422
2423void Context::detachRenderbuffer(GLuint renderbuffer)
2424{
2425    // [OpenGL ES 2.0.24] section 4.4 page 109:
2426    // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2427    // had been executed with the target RENDERBUFFER and name of zero.
2428
2429    if(mState.renderbuffer.name() == renderbuffer)
2430    {
2431        bindRenderbuffer(0);
2432    }
2433
2434    // [OpenGL ES 2.0.24] section 4.4 page 111:
2435    // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2436    // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2437    // point to which this image was attached in the currently bound framebuffer.
2438
2439    Framebuffer *framebuffer = getFramebuffer();
2440
2441    if(framebuffer)
2442    {
2443        framebuffer->detachRenderbuffer(renderbuffer);
2444    }
2445}
2446
2447bool Context::cullSkipsDraw(GLenum drawMode)
2448{
2449    return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
2450}
2451
2452bool Context::isTriangleMode(GLenum drawMode)
2453{
2454    switch (drawMode)
2455    {
2456      case GL_TRIANGLES:
2457      case GL_TRIANGLE_FAN:
2458      case GL_TRIANGLE_STRIP:
2459        return true;
2460      case GL_POINTS:
2461      case GL_LINES:
2462      case GL_LINE_LOOP:
2463      case GL_LINE_STRIP:
2464        return false;
2465      default: UNREACHABLE();
2466    }
2467
2468    return false;
2469}
2470
2471void Context::setVertexAttrib(GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
2472{
2473    ASSERT(index < MAX_VERTEX_ATTRIBS);
2474
2475    mState.vertexAttribute[index].mCurrentValue[0] = x;
2476    mState.vertexAttribute[index].mCurrentValue[1] = y;
2477    mState.vertexAttribute[index].mCurrentValue[2] = z;
2478    mState.vertexAttribute[index].mCurrentValue[3] = w;
2479
2480    mVertexDataManager->dirtyCurrentValue(index);
2481}
2482
2483void Context::bindTexImage(egl::Surface *surface)
2484{
2485	es1::Texture2D *textureObject = getTexture2D();
2486
2487    if(textureObject)
2488    {
2489		textureObject->bindTexImage(surface);
2490	}
2491}
2492
2493EGLenum Context::validateSharedImage(EGLenum target, GLuint name, GLuint textureLevel)
2494{
2495    switch(target)
2496    {
2497    case EGL_GL_TEXTURE_2D_KHR:
2498        break;
2499    case EGL_GL_RENDERBUFFER_KHR:
2500        break;
2501    default:
2502        return EGL_BAD_PARAMETER;
2503    }
2504
2505    if(textureLevel >= IMPLEMENTATION_MAX_TEXTURE_LEVELS)
2506    {
2507        return EGL_BAD_MATCH;
2508    }
2509
2510	if(target == EGL_GL_TEXTURE_2D_KHR)
2511    {
2512        Texture *texture = getTexture(name);
2513
2514        if(!texture || texture->getTarget() != GL_TEXTURE_2D)
2515        {
2516            return EGL_BAD_PARAMETER;
2517        }
2518
2519        if(texture->isShared(GL_TEXTURE_2D, textureLevel))   // Bound to an EGLSurface or already an EGLImage sibling
2520        {
2521            return EGL_BAD_ACCESS;
2522        }
2523
2524        if(textureLevel != 0 && !texture->isSamplerComplete())
2525        {
2526            return EGL_BAD_PARAMETER;
2527        }
2528
2529        if(textureLevel == 0 && !(texture->isSamplerComplete() && texture->getLevelCount() == 1))
2530        {
2531            return EGL_BAD_PARAMETER;
2532        }
2533    }
2534    else if(target == EGL_GL_RENDERBUFFER_KHR)
2535    {
2536        Renderbuffer *renderbuffer = getRenderbuffer(name);
2537
2538        if(!renderbuffer)
2539        {
2540            return EGL_BAD_PARAMETER;
2541        }
2542
2543        if(renderbuffer->isShared())   // Already an EGLImage sibling
2544        {
2545            return EGL_BAD_ACCESS;
2546        }
2547    }
2548    else UNREACHABLE();
2549
2550	return EGL_SUCCESS;
2551}
2552
2553egl::Image *Context::createSharedImage(EGLenum target, GLuint name, GLuint textureLevel)
2554{
2555    if(target == EGL_GL_TEXTURE_2D_KHR)
2556    {
2557        es1::Texture *texture = getTexture(name);
2558
2559        return texture->createSharedImage(GL_TEXTURE_2D, textureLevel);
2560    }
2561    else if(target == EGL_GL_RENDERBUFFER_KHR)
2562    {
2563        es1::Renderbuffer *renderbuffer = getRenderbuffer(name);
2564
2565        return renderbuffer->createSharedImage();
2566    }
2567    else UNREACHABLE();
2568
2569	return 0;
2570}
2571
2572Device *Context::getDevice()
2573{
2574	return device;
2575}
2576
2577void Context::setMatrixMode(GLenum mode)
2578{
2579    matrixMode = mode;
2580}
2581
2582sw::MatrixStack &Context::currentMatrixStack()
2583{
2584	switch(matrixMode)
2585	{
2586	case GL_MODELVIEW:
2587		return modelViewStack;
2588	case GL_PROJECTION:
2589		return projectionStack;
2590	case GL_TEXTURE:
2591		switch(mState.activeSampler)
2592		{
2593		case 0: return textureStack0;
2594		case 1: return textureStack1;
2595		}
2596		break;
2597	}
2598
2599	UNREACHABLE();
2600	return textureStack0;
2601}
2602
2603void Context::loadIdentity()
2604{
2605	currentMatrixStack().identity();
2606}
2607
2608void Context::load(const GLfloat *m)
2609{
2610    currentMatrixStack().load(m);
2611}
2612
2613void Context::pushMatrix()
2614{
2615	if(!currentMatrixStack().push())
2616	{
2617		return error(GL_STACK_OVERFLOW);
2618	}
2619}
2620
2621void Context::popMatrix()
2622{
2623    if(!currentMatrixStack().pop())
2624	{
2625		return error(GL_STACK_OVERFLOW);
2626	}
2627}
2628
2629void Context::rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
2630{
2631    currentMatrixStack().rotate(angle, x, y, z);
2632}
2633
2634void Context::translate(GLfloat x, GLfloat y, GLfloat z)
2635{
2636    currentMatrixStack().translate(x, y, z);
2637}
2638
2639void Context::scale(GLfloat x, GLfloat y, GLfloat z)
2640{
2641    currentMatrixStack().scale(x, y, z);
2642}
2643
2644void Context::multiply(const GLfloat *m)
2645{
2646    currentMatrixStack().multiply(m);
2647}
2648
2649void Context::ortho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar)
2650{
2651	currentMatrixStack().ortho(left, right, bottom, top, zNear, zFar);
2652}
2653
2654void Context::clientActiveTexture(GLenum texture)
2655{
2656	clientTexture = texture;
2657}
2658
2659GLenum Context::getClientActiveTexture() const
2660{
2661	return clientTexture;
2662}
2663
2664unsigned int Context::getActiveTexture() const
2665{
2666	return mState.activeSampler;
2667}
2668
2669GLenum Context::getTextureEnvMode()
2670{
2671	return mState.textureEnvMode;
2672}
2673
2674}
2675
2676// Exported functions for use by EGL
2677extern "C"
2678{
2679	es1::Context *glCreateContext(const egl::Config *config, const es1::Context *shareContext)
2680	{
2681		return new es1::Context(config, shareContext);
2682	}
2683}
2684