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