OSDN Git Service

Replacing numbers by constants
[android-x86/external-swiftshader.git] / src / OpenGL / libGLESv2 / Context.h
1 // SwiftShader Software Renderer\r
2 //\r
3 // Copyright(c) 2005-2013 TransGaming Inc.\r
4 //\r
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,\r
6 // transcribed, stored in a retrieval system, translated into any human or computer\r
7 // language by any means, or disclosed to third parties without the explicit written\r
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express\r
9 // or implied, including but not limited to any patent rights, are granted to you.\r
10 //\r
11 \r
12 // Context.h: Defines the Context class, managing all GL state and performing\r
13 // rendering operations. It is the GLES2 specific implementation of EGLContext.\r
14 \r
15 #ifndef LIBGLESV2_CONTEXT_H_\r
16 #define LIBGLESV2_CONTEXT_H_\r
17 \r
18 #include "libEGL/Context.hpp"\r
19 #include "ResourceManager.h"\r
20 #include "common/NameSpace.hpp"\r
21 #include "common/Object.hpp"\r
22 #include "Image.hpp"\r
23 #include "Renderer/Sampler.hpp"\r
24 \r
25 #define GL_APICALL\r
26 #include <GLES2/gl2.h>\r
27 #include <GLES2/gl2ext.h>\r
28 #include <GLES3/gl3.h>\r
29 #define EGLAPI\r
30 #include <EGL/egl.h>\r
31 \r
32 #include <map>\r
33 #include <string>\r
34 \r
35 namespace egl\r
36 {\r
37 class Display;\r
38 class Surface;\r
39 class Config;\r
40 }\r
41 \r
42 namespace es2\r
43 {\r
44 struct TranslatedAttribute;\r
45 struct TranslatedIndexData;\r
46 \r
47 class Device;\r
48 class Buffer;\r
49 class Shader;\r
50 class Program;\r
51 class Texture;\r
52 class Texture2D;\r
53 class Texture3D;\r
54 class TextureCubeMap;\r
55 class TextureExternal;\r
56 class Framebuffer;\r
57 class Renderbuffer;\r
58 class RenderbufferStorage;\r
59 class Colorbuffer;\r
60 class Depthbuffer;\r
61 class StreamingIndexBuffer;\r
62 class Stencilbuffer;\r
63 class DepthStencilbuffer;\r
64 class VertexDataManager;\r
65 class IndexDataManager;\r
66 class Fence;\r
67 class Query;\r
68 \r
69 enum\r
70 {\r
71     MAX_VERTEX_ATTRIBS = 16,\r
72         MAX_UNIFORM_VECTORS = 256,   // Device limit\r
73     MAX_VERTEX_UNIFORM_VECTORS = VERTEX_UNIFORM_VECTORS - 3,   // Reserve space for gl_DepthRange\r
74     MAX_VARYING_VECTORS = 10,\r
75     MAX_TEXTURE_IMAGE_UNITS = TEXTURE_IMAGE_UNITS,\r
76     MAX_VERTEX_TEXTURE_IMAGE_UNITS = VERTEX_TEXTURE_IMAGE_UNITS,\r
77     MAX_COMBINED_TEXTURE_IMAGE_UNITS = MAX_TEXTURE_IMAGE_UNITS + MAX_VERTEX_TEXTURE_IMAGE_UNITS,\r
78     MAX_FRAGMENT_UNIFORM_VECTORS = FRAGMENT_UNIFORM_VECTORS - 3,    // Reserve space for gl_DepthRange\r
79     MAX_DRAW_BUFFERS = 1,\r
80 };\r
81 \r
82 const GLenum compressedTextureFormats[] =\r
83 {\r
84         GL_ETC1_RGB8_OES,\r
85 #if (S3TC_SUPPORT)\r
86         GL_COMPRESSED_RGB_S3TC_DXT1_EXT,\r
87         GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,\r
88         GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE,\r
89         GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE,\r
90 #endif\r
91 #if (GL_ES_VERSION_3_0)\r
92         GL_COMPRESSED_R11_EAC,
93         GL_COMPRESSED_SIGNED_R11_EAC,
94         GL_COMPRESSED_RG11_EAC,
95         GL_COMPRESSED_SIGNED_RG11_EAC,
96         GL_COMPRESSED_RGB8_ETC2,
97         GL_COMPRESSED_SRGB8_ETC2,
98         GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
99         GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
100         GL_COMPRESSED_RGBA8_ETC2_EAC,
101         GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,\r
102 #endif\r
103 };\r
104 \r
105 const GLint NUM_COMPRESSED_TEXTURE_FORMATS = sizeof(compressedTextureFormats) / sizeof(compressedTextureFormats[0]);\r
106 \r
107 const float ALIASED_LINE_WIDTH_RANGE_MIN = 1.0f;\r
108 const float ALIASED_LINE_WIDTH_RANGE_MAX = 1.0f;\r
109 const float ALIASED_POINT_SIZE_RANGE_MIN = 0.125f;\r
110 const float ALIASED_POINT_SIZE_RANGE_MAX = 8192.0f;\r
111 const float MAX_TEXTURE_MAX_ANISOTROPY = 16.0f;\r
112 \r
113 enum QueryType\r
114 {\r
115     QUERY_ANY_SAMPLES_PASSED,\r
116     QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE,\r
117 \r
118     QUERY_TYPE_COUNT\r
119 };\r
120 \r
121 struct Color\r
122 {\r
123     float red;\r
124     float green;\r
125     float blue;\r
126     float alpha;\r
127 };\r
128 \r
129 // Helper structure describing a single vertex attribute\r
130 class VertexAttribute\r
131 {\r
132   public:\r
133     VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mPointer(NULL), mArrayEnabled(false)\r
134     {\r
135         mCurrentValue[0] = 0.0f;\r
136         mCurrentValue[1] = 0.0f;\r
137         mCurrentValue[2] = 0.0f;\r
138         mCurrentValue[3] = 1.0f;\r
139     }\r
140 \r
141     int typeSize() const\r
142     {\r
143         switch (mType)\r
144         {\r
145         case GL_BYTE:           return mSize * sizeof(GLbyte);\r
146         case GL_UNSIGNED_BYTE:  return mSize * sizeof(GLubyte);\r
147         case GL_SHORT:          return mSize * sizeof(GLshort);\r
148         case GL_UNSIGNED_SHORT: return mSize * sizeof(GLushort);\r
149         case GL_FIXED:          return mSize * sizeof(GLfixed);\r
150         case GL_FLOAT:          return mSize * sizeof(GLfloat);\r
151         default: UNREACHABLE(); return mSize * sizeof(GLfloat);\r
152         }\r
153     }\r
154 \r
155     GLsizei stride() const\r
156     {\r
157         return mStride ? mStride : typeSize();\r
158     }\r
159 \r
160     // From glVertexAttribPointer\r
161     GLenum mType;\r
162     GLint mSize;\r
163     bool mNormalized;\r
164     GLsizei mStride;   // 0 means natural stride\r
165 \r
166     union\r
167     {\r
168         const void *mPointer;\r
169         intptr_t mOffset;\r
170     };\r
171 \r
172     gl::BindingPointer<Buffer> mBoundBuffer;   // Captured when glVertexAttribPointer is called.\r
173 \r
174     bool mArrayEnabled;   // From glEnable/DisableVertexAttribArray\r
175     float mCurrentValue[4];   // From glVertexAttrib\r
176 };\r
177 \r
178 typedef VertexAttribute VertexAttributeArray[MAX_VERTEX_ATTRIBS];\r
179 \r
180 // Helper structure to store all raw state\r
181 struct State\r
182 {\r
183     Color colorClearValue;\r
184     GLclampf depthClearValue;\r
185     int stencilClearValue;\r
186 \r
187     bool cullFace;\r
188     GLenum cullMode;\r
189     GLenum frontFace;\r
190     bool depthTest;\r
191     GLenum depthFunc;\r
192     bool blend;\r
193     GLenum sourceBlendRGB;\r
194     GLenum destBlendRGB;\r
195     GLenum sourceBlendAlpha;\r
196     GLenum destBlendAlpha;\r
197     GLenum blendEquationRGB;\r
198     GLenum blendEquationAlpha;\r
199     Color blendColor;\r
200     bool stencilTest;\r
201     GLenum stencilFunc;\r
202     GLint stencilRef;\r
203     GLuint stencilMask;\r
204     GLenum stencilFail;\r
205     GLenum stencilPassDepthFail;\r
206     GLenum stencilPassDepthPass;\r
207     GLuint stencilWritemask;\r
208     GLenum stencilBackFunc;\r
209     GLint stencilBackRef;\r
210     GLuint stencilBackMask;\r
211     GLenum stencilBackFail;\r
212     GLenum stencilBackPassDepthFail;\r
213     GLenum stencilBackPassDepthPass;\r
214     GLuint stencilBackWritemask;\r
215     bool polygonOffsetFill;\r
216     GLfloat polygonOffsetFactor;\r
217     GLfloat polygonOffsetUnits;\r
218     bool sampleAlphaToCoverage;\r
219     bool sampleCoverage;\r
220     GLclampf sampleCoverageValue;\r
221     bool sampleCoverageInvert;\r
222     bool scissorTest;\r
223     bool dither;\r
224     bool primitiveRestartFixedIndex;\r
225     bool rasterizerDiscard;\r
226 \r
227     GLfloat lineWidth;\r
228 \r
229     GLenum generateMipmapHint;\r
230     GLenum fragmentShaderDerivativeHint;\r
231 \r
232     GLint viewportX;\r
233     GLint viewportY;\r
234     GLsizei viewportWidth;\r
235     GLsizei viewportHeight;\r
236     float zNear;\r
237     float zFar;\r
238 \r
239     GLint scissorX;\r
240     GLint scissorY;\r
241     GLsizei scissorWidth;\r
242     GLsizei scissorHeight;\r
243 \r
244     bool colorMaskRed;\r
245     bool colorMaskGreen;\r
246     bool colorMaskBlue;\r
247     bool colorMaskAlpha;\r
248     bool depthMask;\r
249 \r
250     unsigned int activeSampler;   // Active texture unit selector - GL_TEXTURE0\r
251     gl::BindingPointer<Buffer> arrayBuffer;\r
252     gl::BindingPointer<Buffer> elementArrayBuffer;\r
253     GLuint readFramebuffer;\r
254     GLuint drawFramebuffer;\r
255     gl::BindingPointer<Renderbuffer> renderbuffer;\r
256     GLuint currentProgram;\r
257 \r
258     VertexAttribute vertexAttribute[MAX_VERTEX_ATTRIBS];\r
259     gl::BindingPointer<Texture> samplerTexture[TEXTURE_TYPE_COUNT][MAX_COMBINED_TEXTURE_IMAGE_UNITS];\r
260         gl::BindingPointer<Query> activeQuery[QUERY_TYPE_COUNT];\r
261 \r
262     GLint unpackAlignment;\r
263     GLint packAlignment;\r
264 };\r
265 \r
266 class Context : public egl::Context\r
267 {\r
268 public:\r
269     Context(const egl::Config *config, const Context *shareContext, EGLint clientVersion);\r
270 \r
271         virtual void makeCurrent(egl::Surface *surface);\r
272         virtual void destroy();\r
273         virtual EGLint getClientVersion();\r
274 \r
275     void markAllStateDirty();\r
276 \r
277     // State manipulation\r
278     void setClearColor(float red, float green, float blue, float alpha);\r
279     void setClearDepth(float depth);\r
280     void setClearStencil(int stencil);\r
281 \r
282     void setCullFace(bool enabled);\r
283     bool isCullFaceEnabled() const;\r
284     void setCullMode(GLenum mode);\r
285     void setFrontFace(GLenum front);\r
286 \r
287     void setDepthTest(bool enabled);\r
288     bool isDepthTestEnabled() const;\r
289     void setDepthFunc(GLenum depthFunc);\r
290     void setDepthRange(float zNear, float zFar);\r
291     \r
292     void setBlend(bool enabled);\r
293     bool isBlendEnabled() const;\r
294     void setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha);\r
295     void setBlendColor(float red, float green, float blue, float alpha);\r
296     void setBlendEquation(GLenum rgbEquation, GLenum alphaEquation);\r
297 \r
298     void setStencilTest(bool enabled);\r
299     bool isStencilTestEnabled() const;\r
300     void setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask);\r
301     void setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask);\r
302     void setStencilWritemask(GLuint stencilWritemask);\r
303     void setStencilBackWritemask(GLuint stencilBackWritemask);\r
304     void setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass);\r
305     void setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass);\r
306 \r
307     void setPolygonOffsetFill(bool enabled);\r
308     bool isPolygonOffsetFillEnabled() const;\r
309     void setPolygonOffsetParams(GLfloat factor, GLfloat units);\r
310 \r
311     void setSampleAlphaToCoverage(bool enabled);\r
312     bool isSampleAlphaToCoverageEnabled() const;\r
313     void setSampleCoverage(bool enabled);\r
314     bool isSampleCoverageEnabled() const;\r
315     void setSampleCoverageParams(GLclampf value, bool invert);\r
316 \r
317     void setDither(bool enabled);\r
318     bool isDitherEnabled() const;\r
319 \r
320     void setPrimitiveRestartFixedIndex(bool enabled);\r
321     bool isPrimitiveRestartFixedIndexEnabled() const;\r
322 \r
323     void setRasterizerDiscard(bool enabled);\r
324     bool isRasterizerDiscardEnabled() const;\r
325 \r
326     void setLineWidth(GLfloat width);\r
327 \r
328     void setGenerateMipmapHint(GLenum hint);\r
329     void setFragmentShaderDerivativeHint(GLenum hint);\r
330 \r
331     void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height);\r
332 \r
333         void setScissorTest(bool enabled);\r
334     bool isScissorTestEnabled() const;\r
335     void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height);\r
336 \r
337     void setColorMask(bool red, bool green, bool blue, bool alpha);\r
338     void setDepthMask(bool mask);\r
339 \r
340     void setActiveSampler(unsigned int active);\r
341 \r
342     GLuint getReadFramebufferName() const;\r
343     GLuint getDrawFramebufferName() const;\r
344     GLuint getRenderbufferName() const;\r
345 \r
346         GLuint getActiveQuery(GLenum target) const;\r
347 \r
348     GLuint getArrayBufferName() const;\r
349 \r
350     void setEnableVertexAttribArray(unsigned int attribNum, bool enabled);\r
351     const VertexAttribute &getVertexAttribState(unsigned int attribNum);\r
352     void setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type,\r
353                               bool normalized, GLsizei stride, const void *pointer);\r
354     const void *getVertexAttribPointer(unsigned int attribNum) const;\r
355 \r
356     const VertexAttributeArray &getVertexAttributes();\r
357 \r
358     void setUnpackAlignment(GLint alignment);\r
359     GLint getUnpackAlignment() const;\r
360 \r
361     void setPackAlignment(GLint alignment);\r
362     GLint getPackAlignment() const;\r
363 \r
364     // These create  and destroy methods are merely pass-throughs to \r
365     // ResourceManager, which owns these object types\r
366     GLuint createBuffer();\r
367     GLuint createShader(GLenum type);\r
368     GLuint createProgram();\r
369     GLuint createTexture();\r
370     GLuint createRenderbuffer();\r
371 \r
372     void deleteBuffer(GLuint buffer);\r
373     void deleteShader(GLuint shader);\r
374     void deleteProgram(GLuint program);\r
375     void deleteTexture(GLuint texture);\r
376     void deleteRenderbuffer(GLuint renderbuffer);\r
377 \r
378     // Framebuffers are owned by the Context, so these methods do not pass through\r
379     GLuint createFramebuffer();\r
380     void deleteFramebuffer(GLuint framebuffer);\r
381 \r
382     // Fences are owned by the Context\r
383     GLuint createFence();\r
384     void deleteFence(GLuint fence);\r
385 \r
386         // Queries are owned by the Context\r
387     GLuint createQuery();\r
388     void deleteQuery(GLuint query);\r
389 \r
390     void bindArrayBuffer(GLuint buffer);\r
391     void bindElementArrayBuffer(GLuint buffer);\r
392     void bindTexture2D(GLuint texture);\r
393     void bindTextureCubeMap(GLuint texture);\r
394     void bindTextureExternal(GLuint texture);\r
395         void bindTexture3D(GLuint texture);\r
396     void bindReadFramebuffer(GLuint framebuffer);\r
397     void bindDrawFramebuffer(GLuint framebuffer);\r
398     void bindRenderbuffer(GLuint renderbuffer);\r
399     void useProgram(GLuint program);\r
400 \r
401         void beginQuery(GLenum target, GLuint query);\r
402     void endQuery(GLenum target);\r
403 \r
404     void setFramebufferZero(Framebuffer *framebuffer);\r
405 \r
406     void setRenderbufferStorage(RenderbufferStorage *renderbuffer);\r
407 \r
408     void setVertexAttrib(GLuint index, const GLfloat *values);\r
409 \r
410     Buffer *getBuffer(GLuint handle);\r
411     Fence *getFence(GLuint handle);\r
412     Shader *getShader(GLuint handle);\r
413     Program *getProgram(GLuint handle);\r
414     virtual Texture *getTexture(GLuint handle);\r
415     Framebuffer *getFramebuffer(GLuint handle);\r
416     virtual Renderbuffer *getRenderbuffer(GLuint handle);\r
417         Query *getQuery(GLuint handle, bool create, GLenum type);\r
418 \r
419     Buffer *getArrayBuffer();\r
420     Buffer *getElementArrayBuffer();\r
421     Program *getCurrentProgram();\r
422     Texture2D *getTexture2D();\r
423         Texture3D *getTexture3D();\r
424         TextureCubeMap *getTextureCubeMap();\r
425     TextureExternal *getTextureExternal();\r
426     Texture *getSamplerTexture(unsigned int sampler, TextureType type);\r
427     Framebuffer *getReadFramebuffer();\r
428     Framebuffer *getDrawFramebuffer();\r
429 \r
430     bool getFloatv(GLenum pname, GLfloat *params);\r
431     bool getIntegerv(GLenum pname, GLint *params);\r
432     bool getBooleanv(GLenum pname, GLboolean *params);\r
433         bool getTransformFeedbackiv(GLuint xfb, GLenum pname, GLint *param);\r
434 \r
435     bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams);\r
436 \r
437     void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels);\r
438     void clear(GLbitfield mask);\r
439     void drawArrays(GLenum mode, GLint first, GLsizei count);\r
440     void drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices);\r
441     void finish();\r
442     void flush();\r
443 \r
444     void recordInvalidEnum();\r
445     void recordInvalidValue();\r
446     void recordInvalidOperation();\r
447     void recordOutOfMemory();\r
448     void recordInvalidFramebufferOperation();\r
449 \r
450     GLenum getError();\r
451 \r
452     static int getSupportedMultiSampleDepth(sw::Format format, int requested);\r
453     \r
454     void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, \r
455                          GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,\r
456                          GLbitfield mask);\r
457 \r
458         virtual void bindTexImage(egl::Surface *surface);\r
459         virtual EGLenum validateSharedImage(EGLenum target, GLuint name, GLuint textureLevel);\r
460         virtual egl::Image *createSharedImage(EGLenum target, GLuint name, GLuint textureLevel);\r
461 \r
462         Device *getDevice();\r
463 \r
464 private:\r
465         virtual ~Context();\r
466 \r
467     bool applyRenderTarget();\r
468     void applyState(GLenum drawMode);\r
469     GLenum applyVertexBuffer(GLint base, GLint first, GLsizei count);\r
470     GLenum applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);\r
471     void applyShaders();\r
472     void applyTextures();\r
473     void applyTextures(sw::SamplerType type);\r
474         void applyTexture(sw::SamplerType type, int sampler, Texture *texture);\r
475 \r
476     void detachBuffer(GLuint buffer);\r
477     void detachTexture(GLuint texture);\r
478     void detachFramebuffer(GLuint framebuffer);\r
479     void detachRenderbuffer(GLuint renderbuffer);\r
480 \r
481     bool cullSkipsDraw(GLenum drawMode);\r
482     bool isTriangleMode(GLenum drawMode);\r
483 \r
484         const EGLint clientVersion;\r
485     const egl::Config *const mConfig;\r
486 \r
487     State mState;\r
488 \r
489         gl::BindingPointer<Texture2D> mTexture2DZero;\r
490         gl::BindingPointer<Texture3D> mTexture3DZero;\r
491         gl::BindingPointer<TextureCubeMap> mTextureCubeMapZero;\r
492     gl::BindingPointer<TextureExternal> mTextureExternalZero;\r
493 \r
494     typedef std::map<GLint, Framebuffer*> FramebufferMap;\r
495     FramebufferMap mFramebufferMap;\r
496     gl::NameSpace mFramebufferNameSpace;\r
497 \r
498     typedef std::map<GLint, Fence*> FenceMap;\r
499     FenceMap mFenceMap;\r
500     gl::NameSpace mFenceNameSpace;\r
501 \r
502         typedef std::map<GLint, Query*> QueryMap;\r
503     QueryMap mQueryMap;\r
504     gl::NameSpace mQueryNameSpace;\r
505 \r
506     VertexDataManager *mVertexDataManager;\r
507     IndexDataManager *mIndexDataManager;\r
508 \r
509     // Recorded errors\r
510     bool mInvalidEnum;\r
511     bool mInvalidValue;\r
512     bool mInvalidOperation;\r
513     bool mOutOfMemory;\r
514     bool mInvalidFramebufferOperation;\r
515 \r
516     bool mHasBeenCurrent;\r
517 \r
518     unsigned int mAppliedProgramSerial;\r
519     \r
520     // state caching flags\r
521     bool mDepthStateDirty;\r
522     bool mMaskStateDirty;\r
523     bool mPixelPackingStateDirty;\r
524     bool mBlendStateDirty;\r
525     bool mStencilStateDirty;\r
526     bool mPolygonOffsetStateDirty;\r
527     bool mSampleStateDirty;\r
528     bool mFrontFaceDirty;\r
529     bool mDitherStateDirty;\r
530 \r
531         Device *device;\r
532     ResourceManager *mResourceManager;\r
533 };\r
534 }\r
535 \r
536 #endif   // INCLUDE_CONTEXT_H_\r