Home | History | Annotate | Download | only in libGL
      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.h: Defines the Context class, managing all GL state and performing
     16 // rendering operations.
     17 
     18 #ifndef LIBGL_CONTEXT_H_
     19 #define LIBGL_CONTEXT_H_
     20 
     21 #include "ResourceManager.h"
     22 #include "common/NameSpace.hpp"
     23 #include "common/Object.hpp"
     24 #include "Image.hpp"
     25 #include "Renderer/Sampler.hpp"
     26 #include "Renderer/Vertex.hpp"
     27 #include "common/MatrixStack.hpp"
     28 
     29 #define _GDI32_
     30 #include <windows.h>
     31 #include <GL/GL.h>
     32 #include <GL/glext.h>
     33 
     34 #include <map>
     35 #include <list>
     36 #include <vector>
     37 
     38 namespace gl
     39 {
     40 	class Display;
     41 	class Surface;
     42 	class Config;
     43 
     44 	void APIENTRY glVertexAttribArray(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* ptr);
     45 
     46 	class Command
     47 	{
     48 	public:
     49 		Command() {};
     50 		virtual ~Command() {};
     51 
     52 		virtual void call() = 0;
     53 	};
     54 
     55 	class Command0 : public Command
     56 	{
     57 	public:
     58 		Command0(void (APIENTRY *function)())
     59 			: function(function)
     60 		{
     61 		}
     62 
     63 		virtual void call()
     64 		{
     65 			function();
     66 		}
     67 
     68 		void (APIENTRY *function)();
     69 	};
     70 
     71 	template<typename A1>
     72 	class Command1 : public Command
     73 	{
     74 	public:
     75 		Command1(void (APIENTRY *function)(A1), A1 arg1)
     76 			: function(function)
     77 			, argument1(arg1)
     78 		{
     79 		}
     80 
     81 		virtual void call()
     82 		{
     83 			function(argument1);
     84 		}
     85 
     86 		void (APIENTRY *function)(A1);
     87 		A1 argument1;
     88 	};
     89 
     90 	template<typename A1, typename A2>
     91 	class Command2 : public Command
     92 	{
     93 	public:
     94 		Command2(void (APIENTRY *function)(A1, A2), A1 arg1, A2 arg2)
     95 			: function(function)
     96 			, argument1(arg1)
     97 			, argument2(arg2)
     98 		{
     99 		}
    100 
    101 		virtual void call()
    102 		{
    103 			function(argument1, argument2);
    104 		}
    105 
    106 		void (APIENTRY *function)(A1, A2);
    107 		A1 argument1;
    108 		A2 argument2;
    109 	};
    110 
    111 	template<typename A1, typename A2, typename A3>
    112 	class Command3 : public Command
    113 	{
    114 	public:
    115 		Command3(void (APIENTRY *function)(A1, A2, A3), A1 arg1, A2 arg2, A3 arg3)
    116 			: function(function)
    117 			, argument1(arg1)
    118 			, argument2(arg2)
    119 			, argument3(arg3)
    120 		{
    121 		}
    122 
    123 		virtual void call()
    124 		{
    125 			function(argument1, argument2, argument3);
    126 		}
    127 
    128 		void (APIENTRY *function)(A1, A2, A3);
    129 		A1 argument1;
    130 		A2 argument2;
    131 		A3 argument3;
    132 	};
    133 
    134 	template<typename A1, typename A2, typename A3, typename A4>
    135 	class Command4 : public Command
    136 	{
    137 	public:
    138 		Command4(void (APIENTRY *function)(A1, A2, A3, A4), A1 arg1, A2 arg2, A3 arg3, A4 arg4)
    139 			: function(function)
    140 			, argument1(arg1)
    141 			, argument2(arg2)
    142 			, argument3(arg3)
    143 			, argument4(arg4)
    144 		{
    145 		}
    146 
    147 		virtual void call()
    148 		{
    149 			function(argument1, argument2, argument3, argument4);
    150 		}
    151 
    152 		void (APIENTRY *function)(A1, A2, A3, A4);
    153 		A1 argument1;
    154 		A2 argument2;
    155 		A3 argument3;
    156 		A4 argument4;
    157 	};
    158 
    159 	template<typename A1, typename A2, typename A3, typename A4, typename A5>
    160 	class Command5 : public Command
    161 	{
    162 	public:
    163 		Command5(void (APIENTRY *function)(A1, A2, A3, A4, A5), A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5)
    164 			: function(function)
    165 			, argument1(arg1)
    166 			, argument2(arg2)
    167 			, argument3(arg3)
    168 			, argument4(arg4)
    169 			, argument5(arg5)
    170 		{
    171 		}
    172 
    173 		virtual void call()
    174 		{
    175 			function(argument1, argument2, argument3, argument4, argument5);
    176 		}
    177 
    178 		void (APIENTRY *function)(A1, A2, A3, A4, A5);
    179 		A1 argument1;
    180 		A2 argument2;
    181 		A3 argument3;
    182 		A4 argument4;
    183 		A5 argument5;
    184 	};
    185 
    186 	template<typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
    187 	class Command6 : public Command
    188 	{
    189 	public:
    190 		Command6(void (APIENTRY *function)(A1, A2, A3, A4, A5, A6), A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5, A6 arg6)
    191 			: function(function)
    192 			, argument1(arg1)
    193 			, argument2(arg2)
    194 			, argument3(arg3)
    195 			, argument4(arg4)
    196 			, argument5(arg5)
    197 			, argument6(arg6)
    198 		{
    199 		}
    200 
    201 		~Command6()
    202 		{
    203 			if(function == glVertexAttribArray)
    204 			{
    205 				delete[] argument6;
    206 			}
    207 		}
    208 
    209 		virtual void call()
    210 		{
    211 			function(argument1, argument2, argument3, argument4, argument5, argument6);
    212 		}
    213 
    214 		void (APIENTRY *function)(A1, A2, A3, A4, A5, A6);
    215 		A1 argument1;
    216 		A2 argument2;
    217 		A3 argument3;
    218 		A4 argument4;
    219 		A5 argument5;
    220 		A6 argument6;
    221 	};
    222 
    223 	inline Command0 *newCommand(void (APIENTRY *function)())
    224 	{
    225 		return new Command0(function);
    226 	}
    227 
    228 	template<typename A1>
    229 	Command1<A1> *newCommand(void (APIENTRY *function)(A1), A1 arg1)
    230 	{
    231 		return new Command1<A1>(function, arg1);
    232 	}
    233 
    234 	template<typename A1, typename A2>
    235 	Command2<A1, A2> *newCommand(void (APIENTRY *function)(A1, A2), A1 arg1, A2 arg2)
    236 	{
    237 		return new Command2<A1, A2>(function, arg1, arg2);
    238 	}
    239 
    240 	template<typename A1, typename A2, typename A3>
    241 	Command3<A1, A2, A3> *newCommand(void (APIENTRY *function)(A1, A2, A3), A1 arg1, A2 arg2, A3 arg3)
    242 	{
    243 		return new Command3<A1, A2, A3>(function, arg1, arg2, arg3);
    244 	}
    245 
    246 	template<typename A1, typename A2, typename A3, typename A4>
    247 	Command4<A1, A2, A3, A4> *newCommand(void (APIENTRY *function)(A1, A2, A3, A4), A1 arg1, A2 arg2, A3 arg3, A4 arg4)
    248 	{
    249 		return new Command4<A1, A2, A3, A4>(function, arg1, arg2, arg3, arg4);
    250 	}
    251 
    252 	template<typename A1, typename A2, typename A3, typename A4, typename A5>
    253 	Command5<A1, A2, A3, A4, A5> *newCommand(void (APIENTRY *function)(A1, A2, A3, A4, A5), A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5)
    254 	{
    255 		return new Command5<A1, A2, A3, A4, A5>(function, arg1, arg2, arg3, arg4, arg5);
    256 	}
    257 
    258 	template<typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
    259 	Command6<A1, A2, A3, A4, A5, A6> *newCommand(void (APIENTRY *function)(A1, A2, A3, A4, A5, A6), A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5, A6 arg6)
    260 	{
    261 		return new Command6<A1, A2, A3, A4, A5, A6>(function, arg1, arg2, arg3, arg4, arg5, arg6);
    262 	}
    263 
    264 	class DisplayList
    265 	{
    266 	public:
    267 		DisplayList()
    268 		{
    269 		}
    270 
    271 		~DisplayList()
    272 		{
    273 			while(!list.empty())
    274 			{
    275 				delete list.back();
    276 				list.pop_back();
    277 			}
    278 		}
    279 
    280 		void call()
    281 		{
    282 			for(CommandList::iterator command = list.begin(); command != list.end(); command++)
    283 			{
    284 				(*command)->call();
    285 			}
    286 		}
    287 
    288 		typedef std::list<Command*> CommandList;
    289 		CommandList list;
    290 	};
    291 
    292 struct TranslatedAttribute;
    293 struct TranslatedIndexData;
    294 
    295 class Device;
    296 class Buffer;
    297 class Shader;
    298 class Program;
    299 class Texture;
    300 class Texture2D;
    301 class TextureCubeMap;
    302 class Framebuffer;
    303 class Renderbuffer;
    304 class RenderbufferStorage;
    305 class Colorbuffer;
    306 class Depthbuffer;
    307 class StreamingIndexBuffer;
    308 class Stencilbuffer;
    309 class DepthStencilbuffer;
    310 class VertexDataManager;
    311 class IndexDataManager;
    312 class Fence;
    313 class Query;
    314 
    315 enum
    316 {
    317 	MAX_VERTEX_ATTRIBS = 9,
    318 	MAX_UNIFORM_VECTORS = 256,   // Device limit
    319 	MAX_VERTEX_UNIFORM_VECTORS = sw::VERTEX_UNIFORM_VECTORS - 3,   // Reserve space for gl_DepthRange
    320 	MAX_VARYING_VECTORS = 10,
    321 	MAX_TEXTURE_IMAGE_UNITS = 2,
    322 	MAX_VERTEX_TEXTURE_IMAGE_UNITS = 1,
    323 	MAX_COMBINED_TEXTURE_IMAGE_UNITS = MAX_TEXTURE_IMAGE_UNITS + MAX_VERTEX_TEXTURE_IMAGE_UNITS,
    324 	MAX_FRAGMENT_UNIFORM_VECTORS = sw::FRAGMENT_UNIFORM_VECTORS - 3,    // Reserve space for gl_DepthRange
    325 	MAX_DRAW_BUFFERS = 1,
    326 
    327 	IMPLEMENTATION_COLOR_READ_FORMAT = GL_RGB,
    328 	IMPLEMENTATION_COLOR_READ_TYPE = GL_UNSIGNED_SHORT_5_6_5
    329 };
    330 
    331 const GLenum compressedTextureFormats[] =
    332 {
    333 	GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
    334 	GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
    335 	GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
    336 	GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
    337 };
    338 
    339 const GLint NUM_COMPRESSED_TEXTURE_FORMATS = sizeof(compressedTextureFormats) / sizeof(compressedTextureFormats[0]);
    340 
    341 const GLint multisampleCount[] = {4, 2, 1};
    342 const GLint NUM_MULTISAMPLE_COUNTS = sizeof(multisampleCount) / sizeof(multisampleCount[0]);
    343 const GLint IMPLEMENTATION_MAX_SAMPLES = multisampleCount[0];
    344 
    345 const float ALIASED_LINE_WIDTH_RANGE_MIN = 1.0f;
    346 const float ALIASED_LINE_WIDTH_RANGE_MAX = 128.0f;
    347 const float ALIASED_POINT_SIZE_RANGE_MIN = 0.125f;
    348 const float ALIASED_POINT_SIZE_RANGE_MAX = 8192.0f;
    349 const float MAX_TEXTURE_MAX_ANISOTROPY = 16.0f;
    350 
    351 enum QueryType
    352 {
    353 	QUERY_ANY_SAMPLES_PASSED,
    354 	QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE,
    355 
    356 	QUERY_TYPE_COUNT
    357 };
    358 
    359 struct Color
    360 {
    361 	float red;
    362 	float green;
    363 	float blue;
    364 	float alpha;
    365 };
    366 
    367 // Helper structure describing a single vertex attribute
    368 class VertexAttribute
    369 {
    370 public:
    371 	VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mPointer(nullptr), mArrayEnabled(false)
    372 	{
    373 		mCurrentValue[0] = 0.0f;
    374 		mCurrentValue[1] = 0.0f;
    375 		mCurrentValue[2] = 0.0f;
    376 		mCurrentValue[3] = 1.0f;
    377 	}
    378 
    379 	int typeSize() const
    380 	{
    381 		switch(mType)
    382 		{
    383 		case GL_BYTE:           return mSize * sizeof(GLbyte);
    384 		case GL_UNSIGNED_BYTE:  return mSize * sizeof(GLubyte);
    385 		case GL_SHORT:          return mSize * sizeof(GLshort);
    386 		case GL_UNSIGNED_SHORT: return mSize * sizeof(GLushort);
    387 		case GL_FIXED:          return mSize * sizeof(GLfixed);
    388 		case GL_FLOAT:          return mSize * sizeof(GLfloat);
    389 		default: UNREACHABLE(mType); return mSize * sizeof(GLfloat);
    390 		}
    391 	}
    392 
    393 	GLsizei stride() const
    394 	{
    395 		return mStride ? mStride : typeSize();
    396 	}
    397 
    398 	// From glVertexAttribPointer
    399 	GLenum mType;
    400 	GLint mSize;
    401 	bool mNormalized;
    402 	GLsizei mStride;   // 0 means natural stride
    403 
    404 	union
    405 	{
    406 		const void *mPointer;
    407 		intptr_t mOffset;
    408 	};
    409 
    410 	BindingPointer<Buffer> mBoundBuffer;   // Captured when glVertexAttribPointer is called.
    411 
    412 	bool mArrayEnabled;   // From glEnable/DisableVertexAttribArray
    413 	float mCurrentValue[4];   // From glVertexAttrib
    414 };
    415 
    416 typedef VertexAttribute VertexAttributeArray[MAX_VERTEX_ATTRIBS];
    417 
    418 // Helper structure to store all raw state
    419 struct State
    420 {
    421 	Color colorClearValue;
    422 	GLclampf depthClearValue;
    423 	int stencilClearValue;
    424 
    425 	bool cullFaceEnabled;
    426 	GLenum cullMode;
    427 	GLenum frontFace;
    428 	bool depthTestEnabled;
    429 	GLenum depthFunc;
    430 	bool blendEnabled;
    431 	GLenum sourceBlendRGB;
    432 	GLenum destBlendRGB;
    433 	GLenum sourceBlendAlpha;
    434 	GLenum destBlendAlpha;
    435 	GLenum blendEquationRGB;
    436 	GLenum blendEquationAlpha;
    437 	Color blendColor;
    438 	bool stencilTestEnabled;
    439 	GLenum stencilFunc;
    440 	GLint stencilRef;
    441 	GLuint stencilMask;
    442 	GLenum stencilFail;
    443 	GLenum stencilPassDepthFail;
    444 	GLenum stencilPassDepthPass;
    445 	GLuint stencilWritemask;
    446 	GLenum stencilBackFunc;
    447 	GLint stencilBackRef;
    448 	GLuint stencilBackMask;
    449 	GLenum stencilBackFail;
    450 	GLenum stencilBackPassDepthFail;
    451 	GLenum stencilBackPassDepthPass;
    452 	GLuint stencilBackWritemask;
    453 	bool polygonOffsetFillEnabled;
    454 	GLfloat polygonOffsetFactor;
    455 	GLfloat polygonOffsetUnits;
    456 	bool sampleAlphaToCoverageEnabled;
    457 	bool sampleCoverageEnabled;
    458 	GLclampf sampleCoverageValue;
    459 	bool sampleCoverageInvert;
    460 	bool scissorTestEnabled;
    461 	bool ditherEnabled;
    462 	bool colorLogicOpEnabled;
    463 	GLenum logicalOperation;
    464 
    465 	GLfloat lineWidth;
    466 
    467 	GLenum generateMipmapHint;
    468 	GLenum fragmentShaderDerivativeHint;
    469 
    470 	GLint viewportX;
    471 	GLint viewportY;
    472 	GLsizei viewportWidth;
    473 	GLsizei viewportHeight;
    474 	float zNear;
    475 	float zFar;
    476 
    477 	GLint scissorX;
    478 	GLint scissorY;
    479 	GLsizei scissorWidth;
    480 	GLsizei scissorHeight;
    481 
    482 	bool colorMaskRed;
    483 	bool colorMaskGreen;
    484 	bool colorMaskBlue;
    485 	bool colorMaskAlpha;
    486 	bool depthMask;
    487 
    488 	unsigned int activeSampler;   // Active texture unit selector - GL_TEXTURE0
    489 	BindingPointer<Buffer> arrayBuffer;
    490 	BindingPointer<Buffer> elementArrayBuffer;
    491 	GLuint readFramebuffer;
    492 	GLuint drawFramebuffer;
    493 	BindingPointer<Renderbuffer> renderbuffer;
    494 	GLuint currentProgram;
    495 
    496 	VertexAttribute vertexAttribute[MAX_VERTEX_ATTRIBS];
    497 	BindingPointer<Texture> samplerTexture[TEXTURE_TYPE_COUNT][MAX_COMBINED_TEXTURE_IMAGE_UNITS];
    498 	BindingPointer<Query> activeQuery[QUERY_TYPE_COUNT];
    499 
    500 	GLint unpackAlignment;
    501 	GLint packAlignment;
    502 };
    503 
    504 class Context
    505 {
    506 public:
    507 	Context(const Context *shareContext);
    508 
    509 	~Context();
    510 
    511 	void makeCurrent(Surface *surface);
    512 
    513 	void markAllStateDirty();
    514 
    515 	// State manipulation
    516 	void setClearColor(float red, float green, float blue, float alpha);
    517 	void setClearDepth(float depth);
    518 	void setClearStencil(int stencil);
    519 
    520 	void setCullFaceEnabled(bool enabled);
    521 	bool isCullFaceEnabled() const;
    522 	void setCullMode(GLenum mode);
    523 	void setFrontFace(GLenum front);
    524 
    525 	void setDepthTestEnabled(bool enabled);
    526 	bool isDepthTestEnabled() const;
    527 	void setDepthFunc(GLenum depthFunc);
    528 	void setDepthRange(float zNear, float zFar);
    529 
    530 	void setBlendEnabled(bool enabled);
    531 	bool isBlendEnabled() const;
    532 	void setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha);
    533 	void setBlendColor(float red, float green, float blue, float alpha);
    534 	void setBlendEquation(GLenum rgbEquation, GLenum alphaEquation);
    535 
    536 	void setStencilTestEnabled(bool enabled);
    537 	bool isStencilTestEnabled() const;
    538 	void setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask);
    539 	void setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask);
    540 	void setStencilWritemask(GLuint stencilWritemask);
    541 	void setStencilBackWritemask(GLuint stencilBackWritemask);
    542 	void setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass);
    543 	void setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass);
    544 
    545 	void setPolygonOffsetFillEnabled(bool enabled);
    546 	bool isPolygonOffsetFillEnabled() const;
    547 	void setPolygonOffsetParams(GLfloat factor, GLfloat units);
    548 
    549 	void setSampleAlphaToCoverageEnabled(bool enabled);
    550 	bool isSampleAlphaToCoverageEnabled() const;
    551 	void setSampleCoverageEnabled(bool enabled);
    552 	bool isSampleCoverageEnabled() const;
    553 	void setSampleCoverageParams(GLclampf value, bool invert);
    554 
    555 	void setDitherEnabled(bool enabled);
    556 	bool isDitherEnabled() const;
    557 
    558 	void setLineWidth(GLfloat width);
    559 
    560 	void setGenerateMipmapHint(GLenum hint);
    561 	void setFragmentShaderDerivativeHint(GLenum hint);
    562 
    563 	void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height);
    564 
    565 	void setScissorTestEnabled(bool enabled);
    566 	bool isScissorTestEnabled() const;
    567 	void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height);
    568 
    569 	void setColorMask(bool red, bool green, bool blue, bool alpha);
    570 	void setDepthMask(bool mask);
    571 
    572 	void setActiveSampler(unsigned int active);
    573 
    574 	GLuint getReadFramebufferName() const;
    575 	GLuint getDrawFramebufferName() const;
    576 	GLuint getRenderbufferName() const;
    577 
    578 	GLuint getActiveQuery(GLenum target) const;
    579 
    580 	GLuint getArrayBufferName() const;
    581 
    582 	void setVertexAttribArrayEnabled(unsigned int attribNum, bool enabled);
    583 	const VertexAttribute &getVertexAttribState(unsigned int attribNum);
    584 	void setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type,
    585 	                          bool normalized, GLsizei stride, const void *pointer);
    586 	const void *getVertexAttribPointer(unsigned int attribNum) const;
    587 
    588 	const VertexAttributeArray &getVertexAttributes();
    589 
    590 	void setUnpackAlignment(GLint alignment);
    591 	GLint getUnpackAlignment() const;
    592 
    593 	void setPackAlignment(GLint alignment);
    594 	GLint getPackAlignment() const;
    595 
    596 	// These create  and destroy methods are merely pass-throughs to
    597 	// ResourceManager, which owns these object types
    598 	GLuint createBuffer();
    599 	GLuint createShader(GLenum type);
    600 	GLuint createProgram();
    601 	GLuint createTexture();
    602 	GLuint createRenderbuffer();
    603 
    604 	void deleteBuffer(GLuint buffer);
    605 	void deleteShader(GLuint shader);
    606 	void deleteProgram(GLuint program);
    607 	void deleteTexture(GLuint texture);
    608 	void deleteRenderbuffer(GLuint renderbuffer);
    609 
    610 	// Framebuffers are owned by the Context, so these methods do not pass through
    611 	GLuint createFramebuffer();
    612 	void deleteFramebuffer(GLuint framebuffer);
    613 
    614 	// Fences are owned by the Context
    615 	GLuint createFence();
    616 	void deleteFence(GLuint fence);
    617 
    618 	// Queries are owned by the Context
    619 	GLuint createQuery();
    620 	void deleteQuery(GLuint query);
    621 
    622 	void bindArrayBuffer(GLuint buffer);
    623 	void bindElementArrayBuffer(GLuint buffer);
    624 	void bindTexture2D(GLuint texture);
    625 	void bindTextureCubeMap(GLuint texture);
    626 	void bindReadFramebuffer(GLuint framebuffer);
    627 	void bindDrawFramebuffer(GLuint framebuffer);
    628 	void bindRenderbuffer(GLuint renderbuffer);
    629 	void useProgram(GLuint program);
    630 
    631 	void beginQuery(GLenum target, GLuint query);
    632 	void endQuery(GLenum target);
    633 
    634 	void setFramebufferZero(Framebuffer *framebuffer);
    635 
    636 	void setRenderbufferStorage(RenderbufferStorage *renderbuffer);
    637 
    638 	void setVertexAttrib(GLuint index, float x, float y, float z, float w);
    639 
    640 	Buffer *getBuffer(GLuint handle);
    641 	Fence *getFence(GLuint handle);
    642 	Shader *getShader(GLuint handle);
    643 	Program *getProgram(GLuint handle);
    644 	Texture *getTexture(GLuint handle);
    645 	Framebuffer *getFramebuffer(GLuint handle);
    646 	Renderbuffer *getRenderbuffer(GLuint handle);
    647 	Query *getQuery(GLuint handle, bool create, GLenum type);
    648 
    649 	Buffer *getArrayBuffer();
    650 	Buffer *getElementArrayBuffer();
    651 	Program *getCurrentProgram();
    652 	Texture2D *getTexture2D(GLenum target);
    653 	TextureCubeMap *getTextureCubeMap();
    654 	Texture *getSamplerTexture(unsigned int sampler, TextureType type);
    655 	Framebuffer *getReadFramebuffer();
    656 	Framebuffer *getDrawFramebuffer();
    657 
    658 	bool getFloatv(GLenum pname, GLfloat *params);
    659 	bool getIntegerv(GLenum pname, GLint *params);
    660 	bool getBooleanv(GLenum pname, GLboolean *params);
    661 
    662 	bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams);
    663 
    664 	void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels);
    665 	void clear(GLbitfield mask);
    666 	void drawArrays(GLenum mode, GLint first, GLsizei count);
    667 	void drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices);
    668 	void finish();
    669 	void flush();
    670 
    671 	void recordInvalidEnum();
    672 	void recordInvalidValue();
    673 	void recordInvalidOperation();
    674 	void recordOutOfMemory();
    675 	void recordInvalidFramebufferOperation();
    676 
    677 	GLenum getError();
    678 
    679 	static int getSupportedMultisampleCount(int requested);
    680 
    681 	void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
    682 	                     GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
    683 	                     GLbitfield mask);
    684 
    685 	void setMatrixMode(GLenum mode);
    686 	void loadIdentity();
    687 	void pushMatrix();
    688 	void popMatrix();
    689 	void rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
    690 	void translate(GLfloat x, GLfloat y, GLfloat z);
    691 	void scale(GLfloat x, GLfloat y, GLfloat z);
    692 	void multiply(const GLdouble *m);
    693 	void multiply(const GLfloat *m);
    694 	void frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar);
    695 	void ortho(double left, double right, double bottom, double top, double zNear, double zFar);   // FIXME: GLdouble
    696 
    697 	void setLightingEnabled(bool enabled);
    698 	void setFogEnabled(bool enabled);
    699 	void setAlphaTestEnabled(bool enabled);
    700 	void alphaFunc(GLenum func, GLclampf ref);
    701 	void setTexture2DEnabled(bool enabled);
    702 	void setShadeModel(GLenum mode);
    703 	void setLightEnabled(int index, bool enable);
    704 	void setNormalizeNormalsEnabled(bool enable);
    705 
    706 	GLuint genLists(GLsizei range);
    707 	void newList(GLuint list, GLenum mode);
    708 	void endList();
    709 	void callList(GLuint list);
    710 	void deleteList(GLuint list);
    711 	GLuint getListIndex() {return listIndex;}
    712 	GLenum getListMode() {return listMode;}
    713 	void listCommand(Command *command);
    714 
    715 	void captureAttribs();
    716 	void captureDrawArrays(GLenum mode, GLint first, GLsizei count);
    717 	void restoreAttribs();
    718 	void clientActiveTexture(GLenum texture);
    719 	GLenum getClientActiveTexture() const;
    720 	unsigned int getActiveTexture() const;
    721 
    722 	void begin(GLenum mode);
    723 	void position(GLfloat x, GLfloat y, GLfloat z, GLfloat w);
    724 	void end();
    725 
    726 	void setColorMaterialEnabled(bool enable);
    727 	void setColorMaterialMode(GLenum mode);
    728 
    729 	void setColorLogicOpEnabled(bool colorLogicOpEnabled);
    730 	bool isColorLogicOpEnabled();
    731 	void setLogicalOperation(GLenum logicalOperation);
    732 
    733 	Device *getDevice();
    734 
    735 private:
    736 	bool applyRenderTarget();
    737 	void applyState(GLenum drawMode);
    738 	GLenum applyVertexBuffer(GLint base, GLint first, GLsizei count);
    739 	GLenum applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
    740 	void applyShaders();
    741 	void applyTextures();
    742 	void applyTextures(sw::SamplerType type);
    743 	void applyTexture(sw::SamplerType type, int sampler, Texture *texture);
    744 
    745 	void detachBuffer(GLuint buffer);
    746 	void detachTexture(GLuint texture);
    747 	void detachFramebuffer(GLuint framebuffer);
    748 	void detachRenderbuffer(GLuint renderbuffer);
    749 
    750 	bool cullSkipsDraw(GLenum drawMode);
    751 	bool isTriangleMode(GLenum drawMode);
    752 
    753 	State mState;
    754 
    755 	BindingPointer<Texture2D> mTexture2DZero;
    756 	BindingPointer<Texture2D> mProxyTexture2DZero;
    757 	BindingPointer<TextureCubeMap> mTextureCubeMapZero;
    758 
    759 	gl::NameSpace<Framebuffer> mFramebufferNameSpace;
    760 	gl::NameSpace<Fence, 0> mFenceNameSpace;
    761 	gl::NameSpace<Query> mQueryNameSpace;
    762 
    763 	VertexDataManager *mVertexDataManager;
    764 	IndexDataManager *mIndexDataManager;
    765 
    766 	// Recorded errors
    767 	bool mInvalidEnum;
    768 	bool mInvalidValue;
    769 	bool mInvalidOperation;
    770 	bool mOutOfMemory;
    771 	bool mInvalidFramebufferOperation;
    772 
    773 	bool mHasBeenCurrent;
    774 
    775 	unsigned int mAppliedProgramSerial;
    776 
    777 	// state caching flags
    778 	bool mDepthStateDirty;
    779 	bool mMaskStateDirty;
    780 	bool mPixelPackingStateDirty;
    781 	bool mBlendStateDirty;
    782 	bool mStencilStateDirty;
    783 	bool mPolygonOffsetStateDirty;
    784 	bool mSampleStateDirty;
    785 	bool mFrontFaceDirty;
    786 	bool mDitherStateDirty;
    787 	bool mColorLogicOperatorDirty;
    788 
    789 	Device *device;
    790 	ResourceManager *mResourceManager;
    791 
    792 	sw::MatrixStack &currentMatrixStack();
    793 	GLenum matrixMode;
    794 	sw::MatrixStack modelView;
    795 	sw::MatrixStack projection;
    796 	sw::MatrixStack texture[8];
    797 
    798 	GLenum listMode;
    799 	//std::map<GLuint, GLuint> listMap;
    800 	std::map<GLuint, DisplayList*> displayList;
    801 	DisplayList *list;
    802 	GLuint listIndex;
    803 	GLuint firstFreeIndex;
    804 
    805 	GLenum clientTexture;
    806 
    807 	bool drawing;
    808 	GLenum drawMode;
    809 
    810 	struct InVertex
    811 	{
    812 		sw::float4 P;    // Position
    813 		sw::float4 N;    // Normal
    814 		sw::float4 C;    // Color
    815 		sw::float4 T0;   // Texture coordinate
    816 		sw::float4 T1;
    817 	};
    818 
    819 	std::vector<InVertex> vertex;
    820 
    821 	VertexAttribute clientAttribute[MAX_VERTEX_ATTRIBS];
    822 
    823 	bool envEnable[8];
    824 };
    825 }
    826 
    827 #endif   // INCLUDE_CONTEXT_H_
    828