OSDN Git Service

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