• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	class Command
45 	{
46 	public:
Command()47 		Command() {};
~Command()48 		virtual ~Command() {};
49 
50 		virtual void call() = 0;
51 	};
52 
53 	class Command0 : public Command
54 	{
55 	public:
Command0(void (APIENTRY * function)())56 		Command0(void (APIENTRY *function)())
57 			: function(function)
58 		{
59 		}
60 
call()61 		virtual void call()
62 		{
63 			function();
64 		}
65 
66 		void (APIENTRY *function)();
67 	};
68 
69 	template<typename A1>
70 	class Command1 : public Command
71 	{
72 	public:
Command1(void (APIENTRY * function)(A1),A1 arg1)73 		Command1(void (APIENTRY *function)(A1), A1 arg1)
74 			: function(function)
75 			, argument1(arg1)
76 		{
77 		}
78 
call()79 		virtual void call()
80 		{
81 			function(argument1);
82 		}
83 
84 		void (APIENTRY *function)(A1);
85 		A1 argument1;
86 	};
87 
88 	template<typename A1, typename A2>
89 	class Command2 : public Command
90 	{
91 	public:
Command2(void (APIENTRY * function)(A1,A2),A1 arg1,A2 arg2)92 		Command2(void (APIENTRY *function)(A1, A2), A1 arg1, A2 arg2)
93 			: function(function)
94 			, argument1(arg1)
95 			, argument2(arg2)
96 		{
97 		}
98 
call()99 		virtual void call()
100 		{
101 			function(argument1, argument2);
102 		}
103 
104 		void (APIENTRY *function)(A1, A2);
105 		A1 argument1;
106 		A2 argument2;
107 	};
108 
109 	template<typename A1, typename A2, typename A3>
110 	class Command3 : public Command
111 	{
112 	public:
Command3(void (APIENTRY * function)(A1,A2,A3),A1 arg1,A2 arg2,A3 arg3)113 		Command3(void (APIENTRY *function)(A1, A2, A3), A1 arg1, A2 arg2, A3 arg3)
114 			: function(function)
115 			, argument1(arg1)
116 			, argument2(arg2)
117 			, argument3(arg3)
118 		{
119 		}
120 
call()121 		virtual void call()
122 		{
123 			function(argument1, argument2, argument3);
124 		}
125 
126 		void (APIENTRY *function)(A1, A2, A3);
127 		A1 argument1;
128 		A2 argument2;
129 		A3 argument3;
130 	};
131 
132 	template<typename A1, typename A2, typename A3, typename A4>
133 	class Command4 : public Command
134 	{
135 	public:
Command4(void (APIENTRY * function)(A1,A2,A3,A4),A1 arg1,A2 arg2,A3 arg3,A4 arg4)136 		Command4(void (APIENTRY *function)(A1, A2, A3, A4), A1 arg1, A2 arg2, A3 arg3, A4 arg4)
137 			: function(function)
138 			, argument1(arg1)
139 			, argument2(arg2)
140 			, argument3(arg3)
141 			, argument4(arg4)
142 		{
143 		}
144 
call()145 		virtual void call()
146 		{
147 			function(argument1, argument2, argument3, argument4);
148 		}
149 
150 		void (APIENTRY *function)(A1, A2, A3, A4);
151 		A1 argument1;
152 		A2 argument2;
153 		A3 argument3;
154 		A4 argument4;
155 	};
156 
157 	template<typename A1, typename A2, typename A3, typename A4, typename A5>
158 	class Command5 : public Command
159 	{
160 	public:
Command5(void (APIENTRY * function)(A1,A2,A3,A4,A5),A1 arg1,A2 arg2,A3 arg3,A4 arg4,A5 arg5)161 		Command5(void (APIENTRY *function)(A1, A2, A3, A4, A5), A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5)
162 			: function(function)
163 			, argument1(arg1)
164 			, argument2(arg2)
165 			, argument3(arg3)
166 			, argument4(arg4)
167 			, argument5(arg5)
168 		{
169 		}
170 
call()171 		virtual void call()
172 		{
173 			function(argument1, argument2, argument3, argument4, argument5);
174 		}
175 
176 		void (APIENTRY *function)(A1, A2, A3, A4, A5);
177 		A1 argument1;
178 		A2 argument2;
179 		A3 argument3;
180 		A4 argument4;
181 		A5 argument5;
182 	};
183 
184 	template<typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
185 	class Command6 : public Command
186 	{
187 	public:
Command6(void (APIENTRY * function)(A1,A2,A3,A4,A5,A6),A1 arg1,A2 arg2,A3 arg3,A4 arg4,A5 arg5,A6 arg6)188 		Command6(void (APIENTRY *function)(A1, A2, A3, A4, A5, A6), A1 arg1, A2 arg2, A3 arg3, A4 arg4, A5 arg5, A6 arg6)
189 			: function(function)
190 			, argument1(arg1)
191 			, argument2(arg2)
192 			, argument3(arg3)
193 			, argument4(arg4)
194 			, argument5(arg5)
195 			, argument6(arg6)
196 		{
197 		}
198 
~Command6()199 		~Command6()
200 		{
201 			if(function == glVertexAttribArray)
202 			{
203 				delete[] argument6;
204 			}
205 		}
206 
call()207 		virtual void call()
208 		{
209 			function(argument1, argument2, argument3, argument4, argument5, argument6);
210 		}
211 
212 		void (APIENTRY *function)(A1, A2, A3, A4, A5, A6);
213 		A1 argument1;
214 		A2 argument2;
215 		A3 argument3;
216 		A4 argument4;
217 		A5 argument5;
218 		A6 argument6;
219 	};
220 
newCommand(void (APIENTRY * function)())221 	inline Command0 *newCommand(void (APIENTRY *function)())
222 	{
223 		return new Command0(function);
224 	}
225 
226 	template<typename A1>
newCommand(void (APIENTRY * function)(A1),A1 arg1)227 	Command1<A1> *newCommand(void (APIENTRY *function)(A1), A1 arg1)
228 	{
229 		return new Command1<A1>(function, arg1);
230 	}
231 
232 	template<typename A1, typename A2>
newCommand(void (APIENTRY * function)(A1,A2),A1 arg1,A2 arg2)233 	Command2<A1, A2> *newCommand(void (APIENTRY *function)(A1, A2), A1 arg1, A2 arg2)
234 	{
235 		return new Command2<A1, A2>(function, arg1, arg2);
236 	}
237 
238 	template<typename A1, typename A2, typename A3>
newCommand(void (APIENTRY * function)(A1,A2,A3),A1 arg1,A2 arg2,A3 arg3)239 	Command3<A1, A2, A3> *newCommand(void (APIENTRY *function)(A1, A2, A3), A1 arg1, A2 arg2, A3 arg3)
240 	{
241 		return new Command3<A1, A2, A3>(function, arg1, arg2, arg3);
242 	}
243 
244 	template<typename A1, typename A2, typename A3, typename A4>
newCommand(void (APIENTRY * function)(A1,A2,A3,A4),A1 arg1,A2 arg2,A3 arg3,A4 arg4)245 	Command4<A1, A2, A3, A4> *newCommand(void (APIENTRY *function)(A1, A2, A3, A4), A1 arg1, A2 arg2, A3 arg3, A4 arg4)
246 	{
247 		return new Command4<A1, A2, A3, A4>(function, arg1, arg2, arg3, arg4);
248 	}
249 
250 	template<typename A1, typename A2, typename A3, typename A4, typename A5>
newCommand(void (APIENTRY * function)(A1,A2,A3,A4,A5),A1 arg1,A2 arg2,A3 arg3,A4 arg4,A5 arg5)251 	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)
252 	{
253 		return new Command5<A1, A2, A3, A4, A5>(function, arg1, arg2, arg3, arg4, arg5);
254 	}
255 
256 	template<typename A1, typename A2, typename A3, typename A4, typename A5, typename A6>
newCommand(void (APIENTRY * function)(A1,A2,A3,A4,A5,A6),A1 arg1,A2 arg2,A3 arg3,A4 arg4,A5 arg5,A6 arg6)257 	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)
258 	{
259 		return new Command6<A1, A2, A3, A4, A5, A6>(function, arg1, arg2, arg3, arg4, arg5, arg6);
260 	}
261 
262 	class DisplayList
263 	{
264 	public:
DisplayList()265 		DisplayList()
266 		{
267 		}
268 
~DisplayList()269 		~DisplayList()
270 		{
271 			while(!list.empty())
272 			{
273 				delete list.back();
274 				list.pop_back();
275 			}
276 		}
277 
call()278 		void call()
279 		{
280 			for(CommandList::iterator command = list.begin(); command != list.end(); command++)
281 			{
282 				(*command)->call();
283 			}
284 		}
285 
286 		typedef std::list<Command*> CommandList;
287 		CommandList list;
288 	};
289 
290 struct TranslatedAttribute;
291 struct TranslatedIndexData;
292 
293 class Device;
294 class Buffer;
295 class Shader;
296 class Program;
297 class Texture;
298 class Texture2D;
299 class TextureCubeMap;
300 class Framebuffer;
301 class Renderbuffer;
302 class RenderbufferStorage;
303 class Colorbuffer;
304 class Depthbuffer;
305 class StreamingIndexBuffer;
306 class Stencilbuffer;
307 class DepthStencilbuffer;
308 class VertexDataManager;
309 class IndexDataManager;
310 class Fence;
311 class Query;
312 
313 enum
314 {
315 	MAX_VERTEX_ATTRIBS = 9,
316 	MAX_UNIFORM_VECTORS = 256,   // Device limit
317 	MAX_VERTEX_UNIFORM_VECTORS = sw::VERTEX_UNIFORM_VECTORS - 3,   // Reserve space for gl_DepthRange
318 	MAX_VARYING_VECTORS = 10,
319 	MAX_TEXTURE_IMAGE_UNITS = 2,
320 	MAX_VERTEX_TEXTURE_IMAGE_UNITS = 1,
321 	MAX_COMBINED_TEXTURE_IMAGE_UNITS = MAX_TEXTURE_IMAGE_UNITS + MAX_VERTEX_TEXTURE_IMAGE_UNITS,
322 	MAX_FRAGMENT_UNIFORM_VECTORS = sw::FRAGMENT_UNIFORM_VECTORS - 3,    // Reserve space for gl_DepthRange
323 	MAX_DRAW_BUFFERS = 1,
324 
325 	IMPLEMENTATION_COLOR_READ_FORMAT = GL_RGB,
326 	IMPLEMENTATION_COLOR_READ_TYPE = GL_UNSIGNED_SHORT_5_6_5
327 };
328 
329 const GLenum compressedTextureFormats[] =
330 {
331 #if (S3TC_SUPPORT)
332 	GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
333 	GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
334 	GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
335 	GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
336 #endif
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:
VertexAttribute()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 
typeSize()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 
stride()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);
getListIndex()711 	GLuint getListIndex() {return listIndex;}
getListMode()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