OSDN Git Service

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