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