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