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