OSDN Git Service

Removing a few UNIMPLEMENTED cases
[android-x86/external-swiftshader.git] / src / OpenGL / libGLESv2 / Context.h
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. It is the GLES2 specific implementation of EGLContext.
17
18 #ifndef LIBGLESV2_CONTEXT_H_
19 #define LIBGLESV2_CONTEXT_H_
20
21 #include "ResourceManager.h"
22 #include "Buffer.h"
23 #include "libEGL/Context.hpp"
24 #include "common/NameSpace.hpp"
25 #include "common/Object.hpp"
26 #include "common/Image.hpp"
27 #include "Renderer/Sampler.hpp"
28
29 #include <GLES2/gl2.h>
30 #include <GLES2/gl2ext.h>
31 #include <GLES3/gl3.h>
32 #include <EGL/egl.h>
33
34 #include <map>
35 #include <string>
36
37 namespace egl
38 {
39 class Display;
40 class Surface;
41 class Config;
42 }
43
44 namespace es2
45 {
46 struct TranslatedAttribute;
47 struct TranslatedIndexData;
48
49 class Device;
50 class Shader;
51 class Program;
52 class Texture;
53 class Texture2D;
54 class Texture3D;
55 class Texture2DArray;
56 class TextureCubeMap;
57 class TextureExternal;
58 class Framebuffer;
59 class Renderbuffer;
60 class RenderbufferStorage;
61 class Colorbuffer;
62 class Depthbuffer;
63 class StreamingIndexBuffer;
64 class Stencilbuffer;
65 class DepthStencilbuffer;
66 class VertexDataManager;
67 class IndexDataManager;
68 class Fence;
69 class FenceSync;
70 class Query;
71 class Sampler;
72 class VertexArray;
73 class TransformFeedback;
74
75 enum
76 {
77         MAX_VERTEX_ATTRIBS = sw::MAX_VERTEX_INPUTS,
78         MAX_UNIFORM_VECTORS = 256,   // Device limit
79         MAX_VERTEX_UNIFORM_VECTORS = sw::VERTEX_UNIFORM_VECTORS - 3,   // Reserve space for gl_DepthRange
80         MAX_VARYING_VECTORS = 10,
81         MAX_TEXTURE_IMAGE_UNITS = sw::TEXTURE_IMAGE_UNITS,
82         MAX_VERTEX_TEXTURE_IMAGE_UNITS = sw::VERTEX_TEXTURE_IMAGE_UNITS,
83         MAX_COMBINED_TEXTURE_IMAGE_UNITS = MAX_TEXTURE_IMAGE_UNITS + MAX_VERTEX_TEXTURE_IMAGE_UNITS,
84         MAX_FRAGMENT_UNIFORM_VECTORS = sw::FRAGMENT_UNIFORM_VECTORS - 3,    // Reserve space for gl_DepthRange
85         MAX_ELEMENT_INDEX = 0x7FFFFFFF,
86         MAX_ELEMENTS_INDICES = 0x7FFFFFFF,
87         MAX_ELEMENTS_VERTICES = 0x7FFFFFFF,
88         MAX_VERTEX_OUTPUT_VECTORS = 16,
89         MAX_FRAGMENT_INPUT_VECTORS = 15,
90         MIN_PROGRAM_TEXEL_OFFSET = sw::MIN_PROGRAM_TEXEL_OFFSET,
91         MAX_PROGRAM_TEXEL_OFFSET = sw::MAX_PROGRAM_TEXEL_OFFSET,
92         MAX_DRAW_BUFFERS = sw::RENDERTARGETS,
93         MAX_COLOR_ATTACHMENTS = MAX(MAX_DRAW_BUFFERS, 8),
94         MAX_FRAGMENT_UNIFORM_BLOCKS = sw::MAX_FRAGMENT_UNIFORM_BLOCKS,
95         MAX_VERTEX_UNIFORM_BLOCKS = sw::MAX_VERTEX_UNIFORM_BLOCKS,
96         MAX_FRAGMENT_UNIFORM_COMPONENTS = sw::FRAGMENT_UNIFORM_VECTORS * 4,
97         MAX_VERTEX_UNIFORM_COMPONENTS = sw::VERTEX_UNIFORM_VECTORS * 4,
98         MAX_UNIFORM_BLOCK_SIZE = sw::MAX_UNIFORM_BLOCK_SIZE,
99         MAX_FRAGMENT_UNIFORM_BLOCKS_COMPONENTS = sw::MAX_FRAGMENT_UNIFORM_BLOCKS * MAX_UNIFORM_BLOCK_SIZE / 4,
100         MAX_VERTEX_UNIFORM_BLOCKS_COMPONENTS = MAX_VERTEX_UNIFORM_BLOCKS * MAX_UNIFORM_BLOCK_SIZE / 4,
101         MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS = MAX_FRAGMENT_UNIFORM_BLOCKS_COMPONENTS + MAX_FRAGMENT_UNIFORM_COMPONENTS,
102         MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS = MAX_VERTEX_UNIFORM_BLOCKS_COMPONENTS + MAX_VERTEX_UNIFORM_COMPONENTS,
103         MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS = 4,
104         MAX_UNIFORM_BUFFER_BINDINGS = sw::MAX_UNIFORM_BUFFER_BINDINGS,
105         UNIFORM_BUFFER_OFFSET_ALIGNMENT = 1,
106         NUM_PROGRAM_BINARY_FORMATS = 0,
107 };
108
109 const GLenum compressedTextureFormats[] =
110 {
111         GL_ETC1_RGB8_OES,
112 #if (S3TC_SUPPORT)
113         GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
114         GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
115         GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE,
116         GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE,
117 #endif
118 #if (GL_ES_VERSION_3_0)
119         GL_COMPRESSED_R11_EAC,
120         GL_COMPRESSED_SIGNED_R11_EAC,
121         GL_COMPRESSED_RG11_EAC,
122         GL_COMPRESSED_SIGNED_RG11_EAC,
123         GL_COMPRESSED_RGB8_ETC2,
124         GL_COMPRESSED_SRGB8_ETC2,
125         GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
126         GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
127         GL_COMPRESSED_RGBA8_ETC2_EAC,
128         GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
129         GL_COMPRESSED_RGBA_ASTC_4x4_KHR,
130         GL_COMPRESSED_RGBA_ASTC_5x4_KHR,
131         GL_COMPRESSED_RGBA_ASTC_5x5_KHR,
132         GL_COMPRESSED_RGBA_ASTC_6x5_KHR,
133         GL_COMPRESSED_RGBA_ASTC_6x6_KHR,
134         GL_COMPRESSED_RGBA_ASTC_8x5_KHR,
135         GL_COMPRESSED_RGBA_ASTC_8x6_KHR,
136         GL_COMPRESSED_RGBA_ASTC_8x8_KHR,
137         GL_COMPRESSED_RGBA_ASTC_10x5_KHR,
138         GL_COMPRESSED_RGBA_ASTC_10x6_KHR,
139         GL_COMPRESSED_RGBA_ASTC_10x8_KHR,
140         GL_COMPRESSED_RGBA_ASTC_10x10_KHR,
141         GL_COMPRESSED_RGBA_ASTC_12x10_KHR,
142         GL_COMPRESSED_RGBA_ASTC_12x12_KHR,
143         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
144         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
145         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
146         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
147         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
148         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
149         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
150         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
151         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
152         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
153         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
154         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
155         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
156         GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
157 #endif
158 };
159
160 const GLint NUM_COMPRESSED_TEXTURE_FORMATS = sizeof(compressedTextureFormats) / sizeof(compressedTextureFormats[0]);
161
162 const GLint multisampleCount[] = {4, 2, 1};
163 const GLint NUM_MULTISAMPLE_COUNTS = sizeof(multisampleCount) / sizeof(multisampleCount[0]);
164 const GLint IMPLEMENTATION_MAX_SAMPLES = multisampleCount[0];
165
166 const float ALIASED_LINE_WIDTH_RANGE_MIN = 1.0f;
167 const float ALIASED_LINE_WIDTH_RANGE_MAX = 1.0f;
168 const float ALIASED_POINT_SIZE_RANGE_MIN = 0.125f;
169 const float ALIASED_POINT_SIZE_RANGE_MAX = 8192.0f;
170 const float MAX_TEXTURE_MAX_ANISOTROPY = 16.0f;
171
172 enum QueryType
173 {
174         QUERY_ANY_SAMPLES_PASSED,
175         QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE,
176         QUERY_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN,
177
178         QUERY_TYPE_COUNT
179 };
180
181 struct Color
182 {
183         float red;
184         float green;
185         float blue;
186         float alpha;
187 };
188
189 // Helper structure describing a single vertex attribute
190 class VertexAttribute
191 {
192 public:
193         VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mDivisor(0), mPointer(nullptr), mArrayEnabled(false)
194         {
195                 mCurrentValue[0].f = 0.0f;
196                 mCurrentValue[1].f = 0.0f;
197                 mCurrentValue[2].f = 0.0f;
198                 mCurrentValue[3].f = 1.0f;
199                 mCurrentValueType = GL_FLOAT;
200         }
201
202         int typeSize() const
203         {
204                 switch(mType)
205                 {
206                 case GL_BYTE:           return mSize * sizeof(GLbyte);
207                 case GL_UNSIGNED_BYTE:  return mSize * sizeof(GLubyte);
208                 case GL_SHORT:          return mSize * sizeof(GLshort);
209                 case GL_UNSIGNED_SHORT: return mSize * sizeof(GLushort);
210                 case GL_INT:            return mSize * sizeof(GLint);
211                 case GL_UNSIGNED_INT:   return mSize * sizeof(GLuint);
212                 case GL_FIXED:          return mSize * sizeof(GLfixed);
213                 case GL_FLOAT:          return mSize * sizeof(GLfloat);
214                 case GL_HALF_FLOAT:     return mSize * sizeof(GLhalf);
215                 case GL_INT_2_10_10_10_REV:          return sizeof(GLint);
216                 case GL_UNSIGNED_INT_2_10_10_10_REV: return sizeof(GLuint);
217                 default: UNREACHABLE(mType); return mSize * sizeof(GLfloat);
218                 }
219         }
220
221         GLenum currentValueType() const
222         {
223                 return mCurrentValueType;
224         }
225
226         GLsizei stride() const
227         {
228                 return mStride ? mStride : typeSize();
229         }
230
231         inline float getCurrentValueBitsAsFloat(int i) const
232         {
233                 return mCurrentValue[i].f;
234         }
235
236         inline float getCurrentValueF(int i) const
237         {
238                 switch(mCurrentValueType)
239                 {
240                 case GL_FLOAT:        return mCurrentValue[i].f;
241                 case GL_INT:          return static_cast<float>(mCurrentValue[i].i);
242                 case GL_UNSIGNED_INT: return static_cast<float>(mCurrentValue[i].ui);
243                 default: UNREACHABLE(mCurrentValueType); return mCurrentValue[i].f;
244                 }
245         }
246
247         inline GLint getCurrentValueI(int i) const
248         {
249                 switch(mCurrentValueType)
250                 {
251                 case GL_FLOAT:        return static_cast<GLint>(mCurrentValue[i].f);
252                 case GL_INT:          return mCurrentValue[i].i;
253                 case GL_UNSIGNED_INT: return static_cast<GLint>(mCurrentValue[i].ui);
254                 default: UNREACHABLE(mCurrentValueType); return mCurrentValue[i].i;
255                 }
256         }
257
258         inline GLuint getCurrentValueUI(int i) const
259         {
260                 switch(mCurrentValueType)
261                 {
262                 case GL_FLOAT:        return static_cast<GLuint>(mCurrentValue[i].f);
263                 case GL_INT:          return static_cast<GLuint>(mCurrentValue[i].i);
264                 case GL_UNSIGNED_INT: return mCurrentValue[i].ui;
265                 default: UNREACHABLE(mCurrentValueType); return mCurrentValue[i].ui;
266                 }
267         }
268
269         inline void setCurrentValue(const GLfloat *values)
270         {
271                 mCurrentValue[0].f = values[0];
272                 mCurrentValue[1].f = values[1];
273                 mCurrentValue[2].f = values[2];
274                 mCurrentValue[3].f = values[3];
275                 mCurrentValueType = GL_FLOAT;
276         }
277
278         inline void setCurrentValue(const GLint *values)
279         {
280                 mCurrentValue[0].i = values[0];
281                 mCurrentValue[1].i = values[1];
282                 mCurrentValue[2].i = values[2];
283                 mCurrentValue[3].i = values[3];
284                 mCurrentValueType = GL_INT;
285         }
286
287         inline void setCurrentValue(const GLuint *values)
288         {
289                 mCurrentValue[0].ui = values[0];
290                 mCurrentValue[1].ui = values[1];
291                 mCurrentValue[2].ui = values[2];
292                 mCurrentValue[3].ui = values[3];
293                 mCurrentValueType = GL_UNSIGNED_INT;
294         }
295
296         // From glVertexAttribPointer
297         GLenum mType;
298         GLint mSize;
299         bool mNormalized;
300         GLsizei mStride;   // 0 means natural stride
301         GLuint mDivisor;   // From glVertexAttribDivisor
302
303         union
304         {
305                 const void *mPointer;
306                 intptr_t mOffset;
307         };
308
309         gl::BindingPointer<Buffer> mBoundBuffer;   // Captured when glVertexAttribPointer is called.
310
311         bool mArrayEnabled;   // From glEnable/DisableVertexAttribArray
312
313 private:
314         union ValueUnion
315         {
316                 float f;
317                 GLint i;
318                 GLuint ui;
319         };
320
321         ValueUnion mCurrentValue[4];   // From glVertexAttrib
322         GLenum mCurrentValueType;
323 };
324
325 typedef VertexAttribute VertexAttributeArray[MAX_VERTEX_ATTRIBS];
326
327 // Helper structure to store all raw state
328 struct State
329 {
330         Color colorClearValue;
331         GLclampf depthClearValue;
332         int stencilClearValue;
333
334         bool cullFaceEnabled;
335         GLenum cullMode;
336         GLenum frontFace;
337         bool depthTestEnabled;
338         GLenum depthFunc;
339         bool blendEnabled;
340         GLenum sourceBlendRGB;
341         GLenum destBlendRGB;
342         GLenum sourceBlendAlpha;
343         GLenum destBlendAlpha;
344         GLenum blendEquationRGB;
345         GLenum blendEquationAlpha;
346         Color blendColor;
347         bool stencilTestEnabled;
348         GLenum stencilFunc;
349         GLint stencilRef;
350         GLuint stencilMask;
351         GLenum stencilFail;
352         GLenum stencilPassDepthFail;
353         GLenum stencilPassDepthPass;
354         GLuint stencilWritemask;
355         GLenum stencilBackFunc;
356         GLint stencilBackRef;
357         GLuint stencilBackMask;
358         GLenum stencilBackFail;
359         GLenum stencilBackPassDepthFail;
360         GLenum stencilBackPassDepthPass;
361         GLuint stencilBackWritemask;
362         bool polygonOffsetFillEnabled;
363         GLfloat polygonOffsetFactor;
364         GLfloat polygonOffsetUnits;
365         bool sampleAlphaToCoverageEnabled;
366         bool sampleCoverageEnabled;
367         GLclampf sampleCoverageValue;
368         bool sampleCoverageInvert;
369         bool scissorTestEnabled;
370         bool ditherEnabled;
371         bool primitiveRestartFixedIndexEnabled;
372         bool rasterizerDiscardEnabled;
373         bool colorLogicOpEnabled;
374         GLenum logicalOperation;
375
376         GLfloat lineWidth;
377
378         GLenum generateMipmapHint;
379         GLenum fragmentShaderDerivativeHint;
380
381         GLint viewportX;
382         GLint viewportY;
383         GLsizei viewportWidth;
384         GLsizei viewportHeight;
385         float zNear;
386         float zFar;
387
388         GLint scissorX;
389         GLint scissorY;
390         GLsizei scissorWidth;
391         GLsizei scissorHeight;
392
393         bool colorMaskRed;
394         bool colorMaskGreen;
395         bool colorMaskBlue;
396         bool colorMaskAlpha;
397         bool depthMask;
398
399         unsigned int activeSampler;   // Active texture unit selector - GL_TEXTURE0
400         gl::BindingPointer<Buffer> arrayBuffer;
401         gl::BindingPointer<Buffer> copyReadBuffer;
402         gl::BindingPointer<Buffer> copyWriteBuffer;
403         gl::BindingPointer<Buffer> pixelPackBuffer;
404         gl::BindingPointer<Buffer> pixelUnpackBuffer;
405         gl::BindingPointer<Buffer> genericUniformBuffer;
406         BufferBinding uniformBuffers[MAX_UNIFORM_BUFFER_BINDINGS];
407
408         GLuint readFramebuffer;
409         GLuint drawFramebuffer;
410         gl::BindingPointer<Renderbuffer> renderbuffer;
411         GLuint currentProgram;
412         GLuint vertexArray;
413         GLuint transformFeedback;
414         gl::BindingPointer<Sampler> sampler[MAX_COMBINED_TEXTURE_IMAGE_UNITS];
415
416         VertexAttribute vertexAttribute[MAX_VERTEX_ATTRIBS];
417         gl::BindingPointer<Texture> samplerTexture[TEXTURE_TYPE_COUNT][MAX_COMBINED_TEXTURE_IMAGE_UNITS];
418         gl::BindingPointer<Query> activeQuery[QUERY_TYPE_COUNT];
419
420         egl::Image::UnpackInfo unpackInfo;
421         GLint packAlignment;
422         GLint packRowLength;
423         GLint packImageHeight;
424         GLint packSkipPixels;
425         GLint packSkipRows;
426         GLint packSkipImages;
427 };
428
429 class Context : public egl::Context
430 {
431 public:
432         Context(egl::Display *display, const Context *shareContext, EGLint clientVersion);
433
434         virtual void makeCurrent(egl::Surface *surface);
435         virtual EGLint getClientVersion() const;
436
437         void markAllStateDirty();
438
439         // State manipulation
440         void setClearColor(float red, float green, float blue, float alpha);
441         void setClearDepth(float depth);
442         void setClearStencil(int stencil);
443
444         void setCullFaceEnabled(bool enabled);
445         bool isCullFaceEnabled() const;
446         void setCullMode(GLenum mode);
447         void setFrontFace(GLenum front);
448
449         void setDepthTestEnabled(bool enabled);
450         bool isDepthTestEnabled() const;
451         void setDepthFunc(GLenum depthFunc);
452         void setDepthRange(float zNear, float zFar);
453
454         void setBlendEnabled(bool enabled);
455         bool isBlendEnabled() const;
456         void setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha);
457         void setBlendColor(float red, float green, float blue, float alpha);
458         void setBlendEquation(GLenum rgbEquation, GLenum alphaEquation);
459
460         void setStencilTestEnabled(bool enabled);
461         bool isStencilTestEnabled() const;
462         void setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask);
463         void setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask);
464         void setStencilWritemask(GLuint stencilWritemask);
465         void setStencilBackWritemask(GLuint stencilBackWritemask);
466         void setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass);
467         void setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass);
468
469         void setPolygonOffsetFillEnabled(bool enabled);
470         bool isPolygonOffsetFillEnabled() const;
471         void setPolygonOffsetParams(GLfloat factor, GLfloat units);
472
473         void setSampleAlphaToCoverageEnabled(bool enabled);
474         bool isSampleAlphaToCoverageEnabled() const;
475         void setSampleCoverageEnabled(bool enabled);
476         bool isSampleCoverageEnabled() const;
477         void setSampleCoverageParams(GLclampf value, bool invert);
478
479         void setDitherEnabled(bool enabled);
480         bool isDitherEnabled() const;
481
482         void setPrimitiveRestartFixedIndexEnabled(bool enabled);
483         bool isPrimitiveRestartFixedIndexEnabled() const;
484
485         void setRasterizerDiscardEnabled(bool enabled);
486         bool isRasterizerDiscardEnabled() const;
487
488         void setLineWidth(GLfloat width);
489
490         void setGenerateMipmapHint(GLenum hint);
491         void setFragmentShaderDerivativeHint(GLenum hint);
492
493         void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height);
494
495         void setScissorTestEnabled(bool enabled);
496         bool isScissorTestEnabled() const;
497         void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height);
498
499         void setColorMask(bool red, bool green, bool blue, bool alpha);
500         unsigned int getColorMask() const;
501         void setDepthMask(bool mask);
502
503         void setActiveSampler(unsigned int active);
504
505         GLuint getReadFramebufferName() const;
506         GLuint getDrawFramebufferName() const;
507         GLuint getRenderbufferName() const;
508
509         void setFramebufferReadBuffer(GLenum buf);
510         void setFramebufferDrawBuffers(GLsizei n, const GLenum *bufs);
511         GLuint getReadFramebufferColorIndex() const;
512
513         GLuint getActiveQuery(GLenum target) const;
514
515         GLuint getArrayBufferName() const;
516         GLuint getElementArrayBufferName() const;
517
518         void setVertexAttribArrayEnabled(unsigned int attribNum, bool enabled);
519         void setVertexAttribDivisor(unsigned int attribNum, GLuint divisor);
520         const VertexAttribute &getVertexAttribState(unsigned int attribNum) const;
521         void setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type,
522                                   bool normalized, GLsizei stride, const void *pointer);
523         const void *getVertexAttribPointer(unsigned int attribNum) const;
524
525         const VertexAttributeArray &getVertexArrayAttributes();
526         // Context attribute current values can be queried independently from VAO current values
527         const VertexAttributeArray &getCurrentVertexAttributes();
528
529         void setUnpackAlignment(GLint alignment);
530         void setUnpackRowLength(GLint rowLength);
531         void setUnpackImageHeight(GLint imageHeight);
532         void setUnpackSkipPixels(GLint skipPixels);
533         void setUnpackSkipRows(GLint skipRows);
534         void setUnpackSkipImages(GLint skipImages);
535         const egl::Image::UnpackInfo& getUnpackInfo() const;
536
537         void setPackAlignment(GLint alignment);
538         void setPackRowLength(GLint rowLength);
539         void setPackImageHeight(GLint imageHeight);
540         void setPackSkipPixels(GLint skipPixels);
541         void setPackSkipRows(GLint skipRows);
542         void setPackSkipImages(GLint skipImages);
543
544         // These create and destroy methods are merely pass-throughs to
545         // ResourceManager, which owns these object types
546         GLuint createBuffer();
547         GLuint createShader(GLenum type);
548         GLuint createProgram();
549         GLuint createTexture();
550         GLuint createRenderbuffer();
551         GLuint createSampler();
552         GLsync createFenceSync(GLenum condition, GLbitfield flags);
553
554         void deleteBuffer(GLuint buffer);
555         void deleteShader(GLuint shader);
556         void deleteProgram(GLuint program);
557         void deleteTexture(GLuint texture);
558         void deleteRenderbuffer(GLuint renderbuffer);
559         void deleteSampler(GLuint sampler);
560         void deleteFenceSync(GLsync fenceSync);
561
562         // Framebuffers are owned by the Context, so these methods do not pass through
563         GLuint createFramebuffer();
564         void deleteFramebuffer(GLuint framebuffer);
565
566         // Fences are owned by the Context
567         GLuint createFence();
568         void deleteFence(GLuint fence);
569
570         // Queries are owned by the Context
571         GLuint createQuery();
572         void deleteQuery(GLuint query);
573
574         // Vertex arrays are owned by the Context
575         GLuint createVertexArray();
576         void deleteVertexArray(GLuint array);
577
578         // Transform feedbacks are owned by the Context
579         GLuint createTransformFeedback();
580         void deleteTransformFeedback(GLuint transformFeedback);
581
582         void bindArrayBuffer(GLuint buffer);
583         void bindElementArrayBuffer(GLuint buffer);
584         void bindCopyReadBuffer(GLuint buffer);
585         void bindCopyWriteBuffer(GLuint buffer);
586         void bindPixelPackBuffer(GLuint buffer);
587         void bindPixelUnpackBuffer(GLuint buffer);
588         void bindTransformFeedbackBuffer(GLuint buffer);
589         void bindTexture2D(GLuint texture);
590         void bindTextureCubeMap(GLuint texture);
591         void bindTextureExternal(GLuint texture);
592         void bindTexture3D(GLuint texture);
593         void bindTexture2DArray(GLuint texture);
594         void bindReadFramebuffer(GLuint framebuffer);
595         void bindDrawFramebuffer(GLuint framebuffer);
596         void bindRenderbuffer(GLuint renderbuffer);
597         void bindVertexArray(GLuint array);
598         void bindGenericUniformBuffer(GLuint buffer);
599         void bindIndexedUniformBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size);
600         void bindGenericTransformFeedbackBuffer(GLuint buffer);
601         void bindIndexedTransformFeedbackBuffer(GLuint buffer, GLuint index, GLintptr offset, GLsizeiptr size);
602         void bindTransformFeedback(GLuint transformFeedback);
603         bool bindSampler(GLuint unit, GLuint sampler);
604         void useProgram(GLuint program);
605
606         void beginQuery(GLenum target, GLuint query);
607         void endQuery(GLenum target);
608
609         void setFramebufferZero(Framebuffer *framebuffer);
610
611         void setRenderbufferStorage(RenderbufferStorage *renderbuffer);
612
613         void setVertexAttrib(GLuint index, const GLfloat *values);
614         void setVertexAttrib(GLuint index, const GLint *values);
615         void setVertexAttrib(GLuint index, const GLuint *values);
616
617         Buffer *getBuffer(GLuint handle) const;
618         Fence *getFence(GLuint handle) const;
619         FenceSync *getFenceSync(GLsync handle) const;
620         Shader *getShader(GLuint handle) const;
621         Program *getProgram(GLuint handle) const;
622         virtual Texture *getTexture(GLuint handle) const;
623         Framebuffer *getFramebuffer(GLuint handle) const;
624         virtual Renderbuffer *getRenderbuffer(GLuint handle) const;
625         Query *getQuery(GLuint handle) const;
626         VertexArray *getVertexArray(GLuint array) const;
627         VertexArray *getCurrentVertexArray() const;
628         bool isVertexArray(GLuint array) const;
629         TransformFeedback *getTransformFeedback(GLuint transformFeedback) const;
630         TransformFeedback *getTransformFeedback() const;
631         Sampler *getSampler(GLuint sampler) const;
632         bool isSampler(GLuint sampler) const;
633
634         Buffer *getArrayBuffer() const;
635         Buffer *getElementArrayBuffer() const;
636         Buffer *getCopyReadBuffer() const;
637         Buffer *getCopyWriteBuffer() const;
638         Buffer *getPixelPackBuffer() const;
639         Buffer *getPixelUnpackBuffer() const;
640         Buffer *getGenericUniformBuffer() const;
641         const GLvoid* getPixels(const GLvoid* data) const;
642         bool getBuffer(GLenum target, es2::Buffer **buffer) const;
643         Program *getCurrentProgram() const;
644         Texture2D *getTexture2D() const;
645         Texture3D *getTexture3D() const;
646         Texture2DArray *getTexture2DArray() const;
647         TextureCubeMap *getTextureCubeMap() const;
648         TextureExternal *getTextureExternal() const;
649         Texture *getSamplerTexture(unsigned int sampler, TextureType type) const;
650         Framebuffer *getReadFramebuffer() const;
651         Framebuffer *getDrawFramebuffer() const;
652
653         bool getFloatv(GLenum pname, GLfloat *params) const;
654         template<typename T> bool getIntegerv(GLenum pname, T *params) const;
655         bool getBooleanv(GLenum pname, GLboolean *params) const;
656         template<typename T> bool getTransformFeedbackiv(GLuint index, GLenum pname, T *param) const;
657         template<typename T> bool getUniformBufferiv(GLuint index, GLenum pname, T *param) const;
658         void samplerParameteri(GLuint sampler, GLenum pname, GLint param);
659         void samplerParameterf(GLuint sampler, GLenum pname, GLfloat param);
660         GLint getSamplerParameteri(GLuint sampler, GLenum pname);
661         GLfloat getSamplerParameterf(GLuint sampler, GLenum pname);
662
663         bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams) const;
664
665         bool hasZeroDivisor() const;
666
667         void readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei *bufSize, void* pixels);
668         void clear(GLbitfield mask);
669         void clearColorBuffer(GLint drawbuffer, const GLint *value);
670         void clearColorBuffer(GLint drawbuffer, const GLuint *value);
671         void clearColorBuffer(GLint drawbuffer, const GLfloat *value);
672         void clearDepthBuffer(const GLfloat value);
673         void clearStencilBuffer(const GLint value);
674         void drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instanceCount = 1);
675         void drawElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const void *indices, GLsizei instanceCount = 1);
676         void finish();
677         void flush();
678
679         void recordInvalidEnum();
680         void recordInvalidValue();
681         void recordInvalidOperation();
682         void recordOutOfMemory();
683         void recordInvalidFramebufferOperation();
684
685         GLenum getError();
686
687         static int getSupportedMultisampleCount(int requested);
688
689         void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
690                              GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
691                              GLbitfield mask, bool filter, bool allowPartialDepthStencilBlit);
692
693         virtual void bindTexImage(egl::Surface *surface);
694         virtual EGLenum validateSharedImage(EGLenum target, GLuint name, GLuint textureLevel);
695         virtual egl::Image *createSharedImage(EGLenum target, GLuint name, GLuint textureLevel);
696         egl::Image *getSharedImage(GLeglImageOES image);
697
698         Device *getDevice();
699
700         const GLubyte* getExtensions(GLuint index, GLuint* numExt = nullptr) const;
701
702 private:
703         virtual ~Context();
704
705         void applyScissor(int width, int height);
706         bool applyRenderTarget();
707         void applyState(GLenum drawMode);
708         GLenum applyVertexBuffer(GLint base, GLint first, GLsizei count, GLsizei instanceId);
709         GLenum applyIndexBuffer(const void *indices, GLuint start, GLuint end, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
710         void applyShaders();
711         void applyTextures();
712         void applyTextures(sw::SamplerType type);
713         void applyTexture(sw::SamplerType type, int sampler, Texture *texture);
714         void clearColorBuffer(GLint drawbuffer, void *value, sw::Format format);
715
716         void detachBuffer(GLuint buffer);
717         void detachTexture(GLuint texture);
718         void detachFramebuffer(GLuint framebuffer);
719         void detachRenderbuffer(GLuint renderbuffer);
720         void detachSampler(GLuint sampler);
721
722         bool cullSkipsDraw(GLenum drawMode);
723         bool isTriangleMode(GLenum drawMode);
724
725         Query *createQuery(GLuint handle, GLenum type);
726
727         const EGLint clientVersion;
728
729         State mState;
730
731         gl::BindingPointer<Texture2D> mTexture2DZero;
732         gl::BindingPointer<Texture3D> mTexture3DZero;
733         gl::BindingPointer<Texture2DArray> mTexture2DArrayZero;
734         gl::BindingPointer<TextureCubeMap> mTextureCubeMapZero;
735         gl::BindingPointer<TextureExternal> mTextureExternalZero;
736
737         gl::NameSpace<Framebuffer> mFramebufferNameSpace;
738         gl::NameSpace<Fence, 0> mFenceNameSpace;
739         gl::NameSpace<Query> mQueryNameSpace;
740         gl::NameSpace<VertexArray> mVertexArrayNameSpace;
741         gl::NameSpace<TransformFeedback> mTransformFeedbackNameSpace;
742
743         VertexDataManager *mVertexDataManager;
744         IndexDataManager *mIndexDataManager;
745
746         // Recorded errors
747         bool mInvalidEnum;
748         bool mInvalidValue;
749         bool mInvalidOperation;
750         bool mOutOfMemory;
751         bool mInvalidFramebufferOperation;
752
753         bool mHasBeenCurrent;
754
755         unsigned int mAppliedProgramSerial;
756
757         // state caching flags
758         bool mDepthStateDirty;
759         bool mMaskStateDirty;
760         bool mBlendStateDirty;
761         bool mStencilStateDirty;
762         bool mPolygonOffsetStateDirty;
763         bool mSampleStateDirty;
764         bool mFrontFaceDirty;
765         bool mDitherStateDirty;
766
767         Device *device;
768         ResourceManager *mResourceManager;
769 };
770 }
771
772 #endif   // INCLUDE_CONTEXT_H_