Context.cpp revision efe9232cdbe5feb74b970811fddfac3563671286
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						break;
1885					case GL_LUMINANCE_ALPHA:
1886					case GL_RGBA:
1887					case GL_BGRA_EXT:
1888						// Cv = Cs, Av = As
1889						device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG1);
1890						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG1);
1891						break;
1892					default: UNREACHABLE();
1893					}
1894					break;
1895				case GL_MODULATE:
1896					switch(texFormat)
1897					{
1898					case GL_ALPHA:
1899						// Cv = Cp, Av = ApAs
1900						device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG2);
1901						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_MODULATE);
1902						break;
1903					case GL_LUMINANCE:
1904					case GL_RGB:
1905						// Cv = CpCs, Av = Ap
1906						device->setStageOperation(unit, sw::TextureStage::STAGE_MODULATE);
1907						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2);
1908						break;
1909					case GL_LUMINANCE_ALPHA:
1910					case GL_RGBA:
1911					case GL_BGRA_EXT:
1912						// Cv = CpCs, Av = ApAs
1913						device->setStageOperation(unit, sw::TextureStage::STAGE_MODULATE);
1914						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_MODULATE);
1915						break;
1916					default: UNREACHABLE();
1917					}
1918					break;
1919				case GL_DECAL:
1920					switch(texFormat)
1921					{
1922					case GL_ALPHA:
1923					case GL_LUMINANCE:
1924					case GL_LUMINANCE_ALPHA:
1925						// undefined
1926						device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG2);
1927						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2);
1928						break;
1929					case GL_RGB:
1930						// Cv = Cs, Av = Ap
1931						device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG1);
1932						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2);
1933						break;
1934					case GL_RGBA:
1935					case GL_BGRA_EXT:
1936						// Cv = Cp(1 ? As) + CsAs, Av = Ap
1937						device->setStageOperation(unit, sw::TextureStage::STAGE_BLENDTEXTUREALPHA);   // Alpha * (Arg1 - Arg2) + Arg2
1938						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2);
1939						break;
1940					default: UNREACHABLE();
1941					}
1942					break;
1943				case GL_BLEND:
1944					switch(texFormat)
1945					{
1946					case GL_ALPHA:
1947						// Cv = Cp, Av = ApAs
1948						device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG2);
1949						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_MODULATE);
1950						break;
1951					case GL_LUMINANCE:
1952					case GL_RGB:
1953						// Cv = Cp(1 ? Cs) + CcCs, Av = Ap
1954						device->setStageOperation(unit, sw::TextureStage::STAGE_LERP);   // Arg3 * (Arg1 - Arg2) + Arg2
1955						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2);
1956						break;
1957					case GL_LUMINANCE_ALPHA:
1958					case GL_RGBA:
1959					case GL_BGRA_EXT:
1960						// Cv = Cp(1 ? Cs) + CcCs, Av = ApAs
1961						device->setStageOperation(unit, sw::TextureStage::STAGE_LERP);   // Arg3 * (Arg1 - Arg2) + Arg2
1962						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_MODULATE);
1963						break;
1964					default: UNREACHABLE();
1965					}
1966					break;
1967				case GL_ADD:
1968					switch(texFormat)
1969					{
1970					case GL_ALPHA:
1971						// Cv = Cp, Av = ApAs
1972						device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG2);
1973						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_MODULATE);
1974						break;
1975					case GL_LUMINANCE:
1976					case GL_RGB:
1977						// Cv = Cp + Cs, Av = Ap
1978						device->setStageOperation(unit, sw::TextureStage::STAGE_ADD);
1979						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG2);
1980						break;
1981					case GL_LUMINANCE_ALPHA:
1982					case GL_RGBA:
1983					case GL_BGRA_EXT:
1984						// Cv = Cp + Cs, Av = ApAs
1985						device->setStageOperation(unit, sw::TextureStage::STAGE_ADD);
1986						device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_MODULATE);
1987						break;
1988					default: UNREACHABLE();
1989					}
1990					break;
1991				default:
1992					UNREACHABLE();
1993				}
1994			}
1995			else   // GL_COMBINE
1996			{
1997				device->setFirstArgument(unit, es2sw::ConvertSourceArgument(mState.textureUnit[unit].src0RGB));
1998				device->setFirstModifier(unit, es2sw::ConvertSourceOperand(mState.textureUnit[unit].operand0RGB));
1999				device->setSecondArgument(unit, es2sw::ConvertSourceArgument(mState.textureUnit[unit].src1RGB));
2000				device->setSecondModifier(unit, es2sw::ConvertSourceOperand(mState.textureUnit[unit].operand1RGB));
2001				device->setThirdArgument(unit, es2sw::ConvertSourceArgument(mState.textureUnit[unit].src2RGB));
2002				device->setThirdModifier(unit, es2sw::ConvertSourceOperand(mState.textureUnit[unit].operand2RGB));
2003
2004				device->setStageOperation(unit, es2sw::ConvertCombineOperation(mState.textureUnit[unit].combineRGB));
2005
2006				device->setFirstArgumentAlpha(unit, es2sw::ConvertSourceArgument(mState.textureUnit[unit].src0Alpha));
2007				device->setFirstModifierAlpha(unit, es2sw::ConvertSourceOperand(mState.textureUnit[unit].operand0Alpha));
2008				device->setSecondArgumentAlpha(unit, es2sw::ConvertSourceArgument(mState.textureUnit[unit].src1Alpha));
2009				device->setSecondModifierAlpha(unit, es2sw::ConvertSourceOperand(mState.textureUnit[unit].operand1Alpha));
2010				device->setThirdArgumentAlpha(unit, es2sw::ConvertSourceArgument(mState.textureUnit[unit].src2Alpha));
2011				device->setThirdModifierAlpha(unit, es2sw::ConvertSourceOperand(mState.textureUnit[unit].operand2Alpha));
2012
2013				device->setStageOperationAlpha(unit, es2sw::ConvertCombineOperation(mState.textureUnit[unit].combineAlpha));
2014			}
2015        }
2016        else
2017        {
2018            applyTexture(unit, 0);
2019
2020			device->setFirstArgument(unit, sw::TextureStage::SOURCE_CURRENT);
2021			device->setFirstModifier(unit, sw::TextureStage::MODIFIER_COLOR);
2022			device->setStageOperation(unit, sw::TextureStage::STAGE_SELECTARG1);
2023
2024			device->setFirstArgumentAlpha(unit, sw::TextureStage::SOURCE_CURRENT);
2025			device->setFirstModifierAlpha(unit, sw::TextureStage::MODIFIER_ALPHA);
2026			device->setStageOperationAlpha(unit, sw::TextureStage::STAGE_SELECTARG1);
2027        }
2028    }
2029}
2030
2031void Context::setTextureEnvMode(GLenum texEnvMode)
2032{
2033	mState.textureUnit[mState.activeSampler].environmentMode = texEnvMode;
2034}
2035
2036void Context::setCombineRGB(GLenum combineRGB)
2037{
2038	mState.textureUnit[mState.activeSampler].combineRGB = combineRGB;
2039}
2040
2041void Context::setCombineAlpha(GLenum combineAlpha)
2042{
2043	mState.textureUnit[mState.activeSampler].combineAlpha = combineAlpha;
2044}
2045
2046void Context::applyTexture(int index, Texture *baseTexture)
2047{
2048	sw::Resource *resource = 0;
2049
2050	if(baseTexture)
2051	{
2052		resource = baseTexture->getResource();
2053	}
2054
2055	device->setTextureResource(index, resource);
2056
2057	if(baseTexture)
2058	{
2059		int levelCount = baseTexture->getLevelCount();
2060
2061		if(baseTexture->getTarget() == GL_TEXTURE_2D || baseTexture->getTarget() == GL_TEXTURE_EXTERNAL_OES)
2062		{
2063			Texture2D *texture = static_cast<Texture2D*>(baseTexture);
2064
2065			for(int mipmapLevel = 0; mipmapLevel < MIPMAP_LEVELS; mipmapLevel++)
2066			{
2067				int surfaceLevel = mipmapLevel;
2068
2069				if(surfaceLevel < 0)
2070				{
2071					surfaceLevel = 0;
2072				}
2073				else if(surfaceLevel >= levelCount)
2074				{
2075					surfaceLevel = levelCount - 1;
2076				}
2077
2078				egl::Image *surface = texture->getImage(surfaceLevel);
2079				device->setTextureLevel(index, 0, mipmapLevel, surface, sw::TEXTURE_2D);
2080			}
2081		}
2082		else UNIMPLEMENTED();
2083	}
2084	else
2085	{
2086		device->setTextureLevel(index, 0, 0, 0, sw::TEXTURE_NULL);
2087	}
2088}
2089
2090void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2091                         GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
2092{
2093    Framebuffer *framebuffer = getFramebuffer();
2094	int framebufferWidth, framebufferHeight, framebufferSamples;
2095
2096    if(framebuffer->completeness(framebufferWidth, framebufferHeight, framebufferSamples) != GL_FRAMEBUFFER_COMPLETE_OES)
2097    {
2098        return error(GL_INVALID_FRAMEBUFFER_OPERATION_OES);
2099    }
2100
2101    if(getFramebufferName() != 0 && framebufferSamples != 0)
2102    {
2103        return error(GL_INVALID_OPERATION);
2104    }
2105
2106	if(format != GL_RGBA || type != GL_UNSIGNED_BYTE)
2107	{
2108		if(format != framebuffer->getImplementationColorReadFormat() || type != framebuffer->getImplementationColorReadType())
2109		{
2110			return error(GL_INVALID_OPERATION);
2111		}
2112	}
2113
2114	GLsizei outputPitch = egl::ComputePitch(width, format, type, mState.packAlignment);
2115
2116	// Sized query sanity check
2117    if(bufSize)
2118    {
2119        int requiredSize = outputPitch * height;
2120        if(requiredSize > *bufSize)
2121        {
2122            return error(GL_INVALID_OPERATION);
2123        }
2124    }
2125
2126    egl::Image *renderTarget = framebuffer->getRenderTarget();
2127
2128    if(!renderTarget)
2129    {
2130        return error(GL_OUT_OF_MEMORY);
2131    }
2132
2133	sw::Rect rect = {x, y, x + width, y + height};
2134	rect.clip(0, 0, renderTarget->getWidth(), renderTarget->getHeight());
2135
2136    unsigned char *source = (unsigned char*)renderTarget->lock(rect.x0, rect.y0, sw::LOCK_READONLY);
2137    unsigned char *dest = (unsigned char*)pixels;
2138    int inputPitch = (int)renderTarget->getPitch();
2139
2140    for(int j = 0; j < rect.y1 - rect.y0; j++)
2141    {
2142		unsigned short *dest16 = (unsigned short*)dest;
2143		unsigned int *dest32 = (unsigned int*)dest;
2144
2145		if(renderTarget->getInternalFormat() == sw::FORMAT_A8B8G8R8 &&
2146           format == GL_RGBA && type == GL_UNSIGNED_BYTE)
2147        {
2148            memcpy(dest, source, (rect.x1 - rect.x0) * 4);
2149        }
2150		else if(renderTarget->getInternalFormat() == sw::FORMAT_A8R8G8B8 &&
2151                format == GL_RGBA && type == GL_UNSIGNED_BYTE)
2152        {
2153            for(int i = 0; i < rect.x1 - rect.x0; i++)
2154			{
2155				unsigned int argb = *(unsigned int*)(source + 4 * i);
2156
2157				dest32[i] = (argb & 0xFF00FF00) | ((argb & 0x000000FF) << 16) | ((argb & 0x00FF0000) >> 16);
2158			}
2159        }
2160		else if(renderTarget->getInternalFormat() == sw::FORMAT_X8R8G8B8 &&
2161                format == GL_RGBA && type == GL_UNSIGNED_BYTE)
2162        {
2163            for(int i = 0; i < rect.x1 - rect.x0; i++)
2164			{
2165				unsigned int xrgb = *(unsigned int*)(source + 4 * i);
2166
2167				dest32[i] = (xrgb & 0xFF00FF00) | ((xrgb & 0x000000FF) << 16) | ((xrgb & 0x00FF0000) >> 16) | 0xFF000000;
2168			}
2169        }
2170		else if(renderTarget->getInternalFormat() == sw::FORMAT_X8R8G8B8 &&
2171                format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE)
2172        {
2173            for(int i = 0; i < rect.x1 - rect.x0; i++)
2174			{
2175				unsigned int xrgb = *(unsigned int*)(source + 4 * i);
2176
2177				dest32[i] = xrgb | 0xFF000000;
2178			}
2179        }
2180        else if(renderTarget->getInternalFormat() == sw::FORMAT_A8R8G8B8 &&
2181                format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE)
2182        {
2183            memcpy(dest, source, (rect.x1 - rect.x0) * 4);
2184        }
2185		else if(renderTarget->getInternalFormat() == sw::FORMAT_A1R5G5B5 &&
2186                format == GL_BGRA_EXT && type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT)
2187        {
2188            memcpy(dest, source, (rect.x1 - rect.x0) * 2);
2189        }
2190		else if(renderTarget->getInternalFormat() == sw::FORMAT_R5G6B5 &&
2191                format == 0x80E0 && type == GL_UNSIGNED_SHORT_5_6_5)   // GL_BGR_EXT
2192        {
2193            memcpy(dest, source, (rect.x1 - rect.x0) * 2);
2194        }
2195		else
2196		{
2197			for(int i = 0; i < rect.x1 - rect.x0; i++)
2198			{
2199				float r;
2200				float g;
2201				float b;
2202				float a;
2203
2204				switch(renderTarget->getInternalFormat())
2205				{
2206				case sw::FORMAT_R5G6B5:
2207					{
2208						unsigned short rgb = *(unsigned short*)(source + 2 * i);
2209
2210						a = 1.0f;
2211						b = (rgb & 0x001F) * (1.0f / 0x001F);
2212						g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2213						r = (rgb & 0xF800) * (1.0f / 0xF800);
2214					}
2215					break;
2216				case sw::FORMAT_A1R5G5B5:
2217					{
2218						unsigned short argb = *(unsigned short*)(source + 2 * i);
2219
2220						a = (argb & 0x8000) ? 1.0f : 0.0f;
2221						b = (argb & 0x001F) * (1.0f / 0x001F);
2222						g = (argb & 0x03E0) * (1.0f / 0x03E0);
2223						r = (argb & 0x7C00) * (1.0f / 0x7C00);
2224					}
2225					break;
2226				case sw::FORMAT_A8R8G8B8:
2227					{
2228						unsigned int argb = *(unsigned int*)(source + 4 * i);
2229
2230						a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2231						b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2232						g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2233						r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2234					}
2235					break;
2236				case sw::FORMAT_A8B8G8R8:
2237					{
2238						unsigned int abgr = *(unsigned int*)(source + 4 * i);
2239
2240						a = (abgr & 0xFF000000) * (1.0f / 0xFF000000);
2241						b = (abgr & 0x00FF0000) * (1.0f / 0x00FF0000);
2242						g = (abgr & 0x0000FF00) * (1.0f / 0x0000FF00);
2243						r = (abgr & 0x000000FF) * (1.0f / 0x000000FF);
2244					}
2245					break;
2246				case sw::FORMAT_X8R8G8B8:
2247					{
2248						unsigned int xrgb = *(unsigned int*)(source + 4 * i);
2249
2250						a = 1.0f;
2251						b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2252						g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2253						r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2254					}
2255					break;
2256				case sw::FORMAT_X8B8G8R8:
2257					{
2258						unsigned int xbgr = *(unsigned int*)(source + 4 * i);
2259
2260						a = 1.0f;
2261						b = (xbgr & 0x00FF0000) * (1.0f / 0x00FF0000);
2262						g = (xbgr & 0x0000FF00) * (1.0f / 0x0000FF00);
2263						r = (xbgr & 0x000000FF) * (1.0f / 0x000000FF);
2264					}
2265					break;
2266				case sw::FORMAT_A2R10G10B10:
2267					{
2268						unsigned int argb = *(unsigned int*)(source + 4 * i);
2269
2270						a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2271						b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2272						g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2273						r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2274					}
2275					break;
2276				default:
2277					UNIMPLEMENTED();   // FIXME
2278					UNREACHABLE();
2279				}
2280
2281				switch(format)
2282				{
2283				case GL_RGBA:
2284					switch(type)
2285					{
2286					case GL_UNSIGNED_BYTE:
2287						dest[4 * i + 0] = (unsigned char)(255 * r + 0.5f);
2288						dest[4 * i + 1] = (unsigned char)(255 * g + 0.5f);
2289						dest[4 * i + 2] = (unsigned char)(255 * b + 0.5f);
2290						dest[4 * i + 3] = (unsigned char)(255 * a + 0.5f);
2291						break;
2292					default: UNREACHABLE();
2293					}
2294					break;
2295				case GL_BGRA_EXT:
2296					switch(type)
2297					{
2298					case GL_UNSIGNED_BYTE:
2299						dest[4 * i + 0] = (unsigned char)(255 * b + 0.5f);
2300						dest[4 * i + 1] = (unsigned char)(255 * g + 0.5f);
2301						dest[4 * i + 2] = (unsigned char)(255 * r + 0.5f);
2302						dest[4 * i + 3] = (unsigned char)(255 * a + 0.5f);
2303						break;
2304					case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2305						// According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2306						// this type is packed as follows:
2307						//   15   14   13   12   11   10    9    8    7    6    5    4    3    2    1    0
2308						//  --------------------------------------------------------------------------------
2309						// |       4th         |        3rd         |        2nd        |   1st component   |
2310						//  --------------------------------------------------------------------------------
2311						// in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2312						dest16[i] =
2313							((unsigned short)(15 * a + 0.5f) << 12)|
2314							((unsigned short)(15 * r + 0.5f) << 8) |
2315							((unsigned short)(15 * g + 0.5f) << 4) |
2316							((unsigned short)(15 * b + 0.5f) << 0);
2317						break;
2318					case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2319						// According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2320						// this type is packed as follows:
2321						//   15   14   13   12   11   10    9    8    7    6    5    4    3    2    1    0
2322						//  --------------------------------------------------------------------------------
2323						// | 4th |          3rd           |           2nd          |      1st component     |
2324						//  --------------------------------------------------------------------------------
2325						// in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2326						dest16[i] =
2327							((unsigned short)(     a + 0.5f) << 15) |
2328							((unsigned short)(31 * r + 0.5f) << 10) |
2329							((unsigned short)(31 * g + 0.5f) << 5) |
2330							((unsigned short)(31 * b + 0.5f) << 0);
2331						break;
2332					default: UNREACHABLE();
2333					}
2334					break;
2335				case GL_RGB:
2336					switch(type)
2337					{
2338					case GL_UNSIGNED_SHORT_5_6_5:
2339						dest16[i] =
2340							((unsigned short)(31 * b + 0.5f) << 0) |
2341							((unsigned short)(63 * g + 0.5f) << 5) |
2342							((unsigned short)(31 * r + 0.5f) << 11);
2343						break;
2344					default: UNREACHABLE();
2345					}
2346					break;
2347				default: UNREACHABLE();
2348				}
2349			}
2350        }
2351
2352		source += inputPitch;
2353		dest += outputPitch;
2354    }
2355
2356	renderTarget->unlock();
2357	renderTarget->release();
2358}
2359
2360void Context::clear(GLbitfield mask)
2361{
2362    Framebuffer *framebuffer = getFramebuffer();
2363
2364    if(!framebuffer || framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE_OES)
2365    {
2366        return error(GL_INVALID_FRAMEBUFFER_OPERATION_OES);
2367    }
2368
2369    if(!applyRenderTarget())
2370    {
2371        return;
2372    }
2373
2374	unsigned int color = (unorm<8>(mState.colorClearValue.alpha) << 24) |
2375                         (unorm<8>(mState.colorClearValue.red) << 16) |
2376                         (unorm<8>(mState.colorClearValue.green) << 8) |
2377                         (unorm<8>(mState.colorClearValue.blue) << 0);
2378    float depth = clamp01(mState.depthClearValue);
2379    int stencil = mState.stencilClearValue & 0x000000FF;
2380
2381	if(mask & GL_COLOR_BUFFER_BIT)
2382	{
2383		unsigned int rgbaMask = (mState.colorMaskRed ? 0x1 : 0) |
2384		                        (mState.colorMaskGreen ? 0x2 : 0) |
2385		                        (mState.colorMaskBlue ? 0x4 : 0) |
2386		                        (mState.colorMaskAlpha ? 0x8 : 0);
2387
2388		if(rgbaMask != 0)
2389		{
2390			device->clearColor(color, rgbaMask);
2391		}
2392	}
2393
2394	if(mask & GL_DEPTH_BUFFER_BIT)
2395	{
2396		if(mState.depthMask != 0)
2397		{
2398			device->clearDepth(depth);
2399		}
2400	}
2401
2402	if(mask & GL_STENCIL_BUFFER_BIT)
2403	{
2404		if(mState.stencilWritemask != 0)
2405		{
2406			device->clearStencil(stencil, mState.stencilWritemask);
2407		}
2408	}
2409}
2410
2411void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2412{
2413    PrimitiveType primitiveType;
2414    int primitiveCount;
2415
2416    if(!es2sw::ConvertPrimitiveType(mode, count, primitiveType, primitiveCount))
2417        return error(GL_INVALID_ENUM);
2418
2419    if(primitiveCount <= 0)
2420    {
2421        return;
2422    }
2423
2424    if(!applyRenderTarget())
2425    {
2426        return;
2427    }
2428
2429    applyState(mode);
2430
2431    GLenum err = applyVertexBuffer(0, first, count);
2432    if(err != GL_NO_ERROR)
2433    {
2434        return error(err);
2435    }
2436
2437    applyTextures();
2438
2439    if(!cullSkipsDraw(mode))
2440    {
2441        device->drawPrimitive(primitiveType, primitiveCount);
2442    }
2443}
2444
2445void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
2446{
2447    if(!indices && !mState.elementArrayBuffer)
2448    {
2449        return error(GL_INVALID_OPERATION);
2450    }
2451
2452    PrimitiveType primitiveType;
2453    int primitiveCount;
2454
2455    if(!es2sw::ConvertPrimitiveType(mode, count, primitiveType, primitiveCount))
2456        return error(GL_INVALID_ENUM);
2457
2458    if(primitiveCount <= 0)
2459    {
2460        return;
2461    }
2462
2463    if(!applyRenderTarget())
2464    {
2465        return;
2466    }
2467
2468    applyState(mode);
2469
2470    TranslatedIndexData indexInfo;
2471    GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2472    if(err != GL_NO_ERROR)
2473    {
2474        return error(err);
2475    }
2476
2477    GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2478    err = applyVertexBuffer(-(int)indexInfo.minIndex, indexInfo.minIndex, vertexCount);
2479    if(err != GL_NO_ERROR)
2480    {
2481        return error(err);
2482    }
2483
2484    applyTextures();
2485
2486    if(!cullSkipsDraw(mode))
2487    {
2488		device->drawIndexedPrimitive(primitiveType, indexInfo.indexOffset, primitiveCount, IndexDataManager::typeSize(type));
2489    }
2490}
2491
2492void Context::drawTexture(GLfloat x, GLfloat y, GLfloat z, GLfloat width, GLfloat height)
2493{
2494	es1::Framebuffer *framebuffer = getFramebuffer();
2495	es1::Renderbuffer *renderbuffer = framebuffer->getColorbuffer();
2496	float targetWidth = renderbuffer->getWidth();
2497	float targetHeight = renderbuffer->getHeight();
2498	float x0 = 2.0f * x / targetWidth - 1.0f;
2499	float y0 = 2.0f * y / targetHeight - 1.0f;
2500	float x1 = 2.0f * (x + width) / targetWidth - 1.0f;
2501	float y1 = 2.0f * (y + height) / targetHeight - 1.0f;
2502	float Zw = sw::clamp(mState.zNear + z * (mState.zFar - mState.zNear), mState.zNear, mState.zFar);
2503
2504	float vertices[][3] = {{x0, y0, Zw},
2505						   {x0, y1, Zw},
2506						   {x1, y0, Zw},
2507						   {x1, y1, Zw}};
2508
2509	ASSERT(mState.samplerTexture[TEXTURE_2D][1].name() == 0);   // Multi-texturing unimplemented
2510	es1::Texture *texture = getSamplerTexture(0, TEXTURE_2D);
2511	float textureWidth = texture->getWidth(GL_TEXTURE_2D, 0);
2512	float textureHeight = texture->getHeight(GL_TEXTURE_2D, 0);
2513	int Ucr = texture->getCropRectU();
2514	int Vcr = texture->getCropRectV();
2515	int Wcr = texture->getCropRectW();
2516	int Hcr = texture->getCropRectH();
2517
2518	float texCoords[][2] = {{Ucr / textureWidth, Vcr / textureHeight},
2519							{Ucr / textureWidth, (Vcr + Hcr) / textureHeight},
2520							{(Ucr + Wcr) / textureWidth, Vcr / textureHeight},
2521							{(Ucr + Wcr) / textureWidth, (Vcr + Hcr) / textureHeight}};
2522
2523	VertexAttribute oldPositionAttribute = mState.vertexAttribute[sw::Position];
2524	VertexAttribute oldTexCoord0Attribute = mState.vertexAttribute[sw::TexCoord0];
2525
2526	glVertexPointer(3, GL_FLOAT, 3 * sizeof(float), vertices);
2527	glEnableClientState(GL_VERTEX_ARRAY);
2528	glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(float), texCoords);
2529	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
2530
2531	textureStack0.push();
2532	textureStack0.identity();   // Disable texture coordinate transformation
2533
2534	drawArrays(GL_TRIANGLE_STRIP, 0, 4);
2535
2536	// Restore state
2537	mState.vertexAttribute[sw::Position] = oldPositionAttribute;
2538	mState.vertexAttribute[sw::TexCoord0] = oldTexCoord0Attribute;
2539	textureStack0.pop();
2540}
2541
2542void Context::finish()
2543{
2544	device->finish();
2545}
2546
2547void Context::flush()
2548{
2549    // We don't queue anything without processing it as fast as possible
2550}
2551
2552void Context::recordInvalidEnum()
2553{
2554    mInvalidEnum = true;
2555}
2556
2557void Context::recordInvalidValue()
2558{
2559    mInvalidValue = true;
2560}
2561
2562void Context::recordInvalidOperation()
2563{
2564    mInvalidOperation = true;
2565}
2566
2567void Context::recordOutOfMemory()
2568{
2569    mOutOfMemory = true;
2570}
2571
2572void Context::recordInvalidFramebufferOperation()
2573{
2574    mInvalidFramebufferOperation = true;
2575}
2576
2577// Get one of the recorded errors and clear its flag, if any.
2578// [OpenGL ES 2.0.24] section 2.5 page 13.
2579GLenum Context::getError()
2580{
2581    if(mInvalidEnum)
2582    {
2583        mInvalidEnum = false;
2584
2585        return GL_INVALID_ENUM;
2586    }
2587
2588    if(mInvalidValue)
2589    {
2590        mInvalidValue = false;
2591
2592        return GL_INVALID_VALUE;
2593    }
2594
2595    if(mInvalidOperation)
2596    {
2597        mInvalidOperation = false;
2598
2599        return GL_INVALID_OPERATION;
2600    }
2601
2602    if(mOutOfMemory)
2603    {
2604        mOutOfMemory = false;
2605
2606        return GL_OUT_OF_MEMORY;
2607    }
2608
2609    if(mInvalidFramebufferOperation)
2610    {
2611        mInvalidFramebufferOperation = false;
2612
2613        return GL_INVALID_FRAMEBUFFER_OPERATION_OES;
2614    }
2615
2616    return GL_NO_ERROR;
2617}
2618
2619int Context::getSupportedMultiSampleDepth(sw::Format format, int requested)
2620{
2621    if(requested <= 1)
2622    {
2623        return 1;
2624    }
2625
2626	if(requested == 2)
2627	{
2628		return 2;
2629	}
2630
2631	return 4;
2632}
2633
2634void Context::detachBuffer(GLuint buffer)
2635{
2636    // [OpenGL ES 2.0.24] section 2.9 page 22:
2637    // If a buffer object is deleted while it is bound, all bindings to that object in the current context
2638    // (i.e. in the thread that called Delete-Buffers) are reset to zero.
2639
2640    if(mState.arrayBuffer.name() == buffer)
2641    {
2642        mState.arrayBuffer = NULL;
2643    }
2644
2645    if(mState.elementArrayBuffer.name() == buffer)
2646    {
2647        mState.elementArrayBuffer = NULL;
2648    }
2649
2650    for(int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
2651    {
2652        if(mState.vertexAttribute[attribute].mBoundBuffer.name() == buffer)
2653        {
2654            mState.vertexAttribute[attribute].mBoundBuffer = NULL;
2655        }
2656    }
2657}
2658
2659void Context::detachTexture(GLuint texture)
2660{
2661    // [OpenGL ES 2.0.24] section 3.8 page 84:
2662    // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
2663    // rebound to texture object zero
2664
2665    for(int type = 0; type < TEXTURE_TYPE_COUNT; type++)
2666    {
2667        for(int sampler = 0; sampler < MAX_TEXTURE_UNITS; sampler++)
2668        {
2669            if(mState.samplerTexture[type][sampler].name() == texture)
2670            {
2671                mState.samplerTexture[type][sampler] = NULL;
2672            }
2673        }
2674    }
2675
2676    // [OpenGL ES 2.0.24] section 4.4 page 112:
2677    // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
2678    // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
2679    // image was attached in the currently bound framebuffer.
2680
2681    Framebuffer *framebuffer = getFramebuffer();
2682
2683    if(framebuffer)
2684    {
2685        framebuffer->detachTexture(texture);
2686    }
2687}
2688
2689void Context::detachFramebuffer(GLuint framebuffer)
2690{
2691    // [OpenGL ES 2.0.24] section 4.4 page 107:
2692    // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
2693    // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
2694
2695    if(mState.framebuffer == framebuffer)
2696    {
2697        bindFramebuffer(0);
2698    }
2699}
2700
2701void Context::detachRenderbuffer(GLuint renderbuffer)
2702{
2703    // [OpenGL ES 2.0.24] section 4.4 page 109:
2704    // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
2705    // had been executed with the target RENDERBUFFER and name of zero.
2706
2707    if(mState.renderbuffer.name() == renderbuffer)
2708    {
2709        bindRenderbuffer(0);
2710    }
2711
2712    // [OpenGL ES 2.0.24] section 4.4 page 111:
2713    // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
2714    // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
2715    // point to which this image was attached in the currently bound framebuffer.
2716
2717    Framebuffer *framebuffer = getFramebuffer();
2718
2719    if(framebuffer)
2720    {
2721        framebuffer->detachRenderbuffer(renderbuffer);
2722    }
2723}
2724
2725bool Context::cullSkipsDraw(GLenum drawMode)
2726{
2727    return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
2728}
2729
2730bool Context::isTriangleMode(GLenum drawMode)
2731{
2732    switch (drawMode)
2733    {
2734      case GL_TRIANGLES:
2735      case GL_TRIANGLE_FAN:
2736      case GL_TRIANGLE_STRIP:
2737        return true;
2738      case GL_POINTS:
2739      case GL_LINES:
2740      case GL_LINE_LOOP:
2741      case GL_LINE_STRIP:
2742        return false;
2743      default: UNREACHABLE();
2744    }
2745
2746    return false;
2747}
2748
2749void Context::setVertexAttrib(GLuint index, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
2750{
2751    ASSERT(index < MAX_VERTEX_ATTRIBS);
2752
2753    mState.vertexAttribute[index].mCurrentValue[0] = x;
2754    mState.vertexAttribute[index].mCurrentValue[1] = y;
2755    mState.vertexAttribute[index].mCurrentValue[2] = z;
2756    mState.vertexAttribute[index].mCurrentValue[3] = w;
2757
2758    mVertexDataManager->dirtyCurrentValue(index);
2759}
2760
2761void Context::bindTexImage(egl::Surface *surface)
2762{
2763	es1::Texture2D *textureObject = getTexture2D();
2764
2765    if(textureObject)
2766    {
2767		textureObject->bindTexImage(surface);
2768	}
2769}
2770
2771EGLenum Context::validateSharedImage(EGLenum target, GLuint name, GLuint textureLevel)
2772{
2773    switch(target)
2774    {
2775    case EGL_GL_TEXTURE_2D_KHR:
2776        break;
2777    case EGL_GL_RENDERBUFFER_KHR:
2778        break;
2779    default:
2780        return EGL_BAD_PARAMETER;
2781    }
2782
2783    if(textureLevel >= IMPLEMENTATION_MAX_TEXTURE_LEVELS)
2784    {
2785        return EGL_BAD_MATCH;
2786    }
2787
2788	if(target == EGL_GL_TEXTURE_2D_KHR)
2789    {
2790        Texture *texture = getTexture(name);
2791
2792        if(!texture || texture->getTarget() != GL_TEXTURE_2D)
2793        {
2794            return EGL_BAD_PARAMETER;
2795        }
2796
2797        if(texture->isShared(GL_TEXTURE_2D, textureLevel))   // Bound to an EGLSurface or already an EGLImage sibling
2798        {
2799            return EGL_BAD_ACCESS;
2800        }
2801
2802        if(textureLevel != 0 && !texture->isSamplerComplete())
2803        {
2804            return EGL_BAD_PARAMETER;
2805        }
2806
2807        if(textureLevel == 0 && !(texture->isSamplerComplete() && texture->getLevelCount() == 1))
2808        {
2809            return EGL_BAD_PARAMETER;
2810        }
2811    }
2812    else if(target == EGL_GL_RENDERBUFFER_KHR)
2813    {
2814        Renderbuffer *renderbuffer = getRenderbuffer(name);
2815
2816        if(!renderbuffer)
2817        {
2818            return EGL_BAD_PARAMETER;
2819        }
2820
2821        if(renderbuffer->isShared())   // Already an EGLImage sibling
2822        {
2823            return EGL_BAD_ACCESS;
2824        }
2825    }
2826    else UNREACHABLE();
2827
2828	return EGL_SUCCESS;
2829}
2830
2831egl::Image *Context::createSharedImage(EGLenum target, GLuint name, GLuint textureLevel)
2832{
2833    if(target == EGL_GL_TEXTURE_2D_KHR)
2834    {
2835        es1::Texture *texture = getTexture(name);
2836
2837        return texture->createSharedImage(GL_TEXTURE_2D, textureLevel);
2838    }
2839    else if(target == EGL_GL_RENDERBUFFER_KHR)
2840    {
2841        es1::Renderbuffer *renderbuffer = getRenderbuffer(name);
2842
2843        return renderbuffer->createSharedImage();
2844    }
2845    else UNREACHABLE();
2846
2847	return 0;
2848}
2849
2850Device *Context::getDevice()
2851{
2852	return device;
2853}
2854
2855void Context::setMatrixMode(GLenum mode)
2856{
2857    matrixMode = mode;
2858}
2859
2860sw::MatrixStack &Context::currentMatrixStack()
2861{
2862	switch(matrixMode)
2863	{
2864	case GL_MODELVIEW:
2865		return modelViewStack;
2866	case GL_PROJECTION:
2867		return projectionStack;
2868	case GL_TEXTURE:
2869		switch(mState.activeSampler)
2870		{
2871		case 0: return textureStack0;
2872		case 1: return textureStack1;
2873		}
2874		break;
2875	}
2876
2877	UNREACHABLE();
2878	return textureStack0;
2879}
2880
2881void Context::loadIdentity()
2882{
2883	currentMatrixStack().identity();
2884}
2885
2886void Context::load(const GLfloat *m)
2887{
2888    currentMatrixStack().load(m);
2889}
2890
2891void Context::pushMatrix()
2892{
2893	if(!currentMatrixStack().push())
2894	{
2895		return error(GL_STACK_OVERFLOW);
2896	}
2897}
2898
2899void Context::popMatrix()
2900{
2901    if(!currentMatrixStack().pop())
2902	{
2903		return error(GL_STACK_OVERFLOW);
2904	}
2905}
2906
2907void Context::rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
2908{
2909    currentMatrixStack().rotate(angle, x, y, z);
2910}
2911
2912void Context::translate(GLfloat x, GLfloat y, GLfloat z)
2913{
2914    currentMatrixStack().translate(x, y, z);
2915}
2916
2917void Context::scale(GLfloat x, GLfloat y, GLfloat z)
2918{
2919    currentMatrixStack().scale(x, y, z);
2920}
2921
2922void Context::multiply(const GLfloat *m)
2923{
2924    currentMatrixStack().multiply(m);
2925}
2926
2927void Context::frustum(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar)
2928{
2929	currentMatrixStack().frustum(left, right, bottom, top, zNear, zFar);
2930}
2931
2932void Context::ortho(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar)
2933{
2934	currentMatrixStack().ortho(left, right, bottom, top, zNear, zFar);
2935}
2936
2937void Context::clientActiveTexture(GLenum texture)
2938{
2939	clientTexture = texture;
2940}
2941
2942GLenum Context::getClientActiveTexture() const
2943{
2944	return clientTexture;
2945}
2946
2947unsigned int Context::getActiveTexture() const
2948{
2949	return mState.activeSampler;
2950}
2951
2952}
2953
2954egl::Context *es1CreateContext(const egl::Config *config, const egl::Context *shareContext)
2955{
2956	ASSERT(!shareContext || shareContext->getClientVersion() == 1);   // Should be checked by eglCreateContext
2957	return new es1::Context(config, static_cast<const es1::Context*>(shareContext));
2958}
2959