1 // SwiftShader Software Renderer
\r
3 // Copyright(c) 2005-2013 TransGaming Inc.
\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
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
15 #ifndef LIBGLESV2_CONTEXT_H_
\r
16 #define LIBGLESV2_CONTEXT_H_
\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
26 #include <GLES2/gl2.h>
\r
27 #include <GLES2/gl2ext.h>
\r
28 #include <GLES3/gl3.h>
\r
30 #include <EGL/egl.h>
\r
44 struct TranslatedAttribute;
\r
45 struct TranslatedIndexData;
\r
54 class TextureCubeMap;
\r
55 class TextureExternal;
\r
58 class RenderbufferStorage;
\r
61 class StreamingIndexBuffer;
\r
62 class Stencilbuffer;
\r
63 class DepthStencilbuffer;
\r
64 class VertexDataManager;
\r
65 class IndexDataManager;
\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
82 const GLenum compressedTextureFormats[] =
\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
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
105 const GLint NUM_COMPRESSED_TEXTURE_FORMATS = sizeof(compressedTextureFormats) / sizeof(compressedTextureFormats[0]);
\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
115 QUERY_ANY_SAMPLES_PASSED,
\r
116 QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE,
\r
129 // Helper structure describing a single vertex attribute
\r
130 class VertexAttribute
\r
133 VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mPointer(NULL), mArrayEnabled(false)
\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
141 int typeSize() const
\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
155 GLsizei stride() const
\r
157 return mStride ? mStride : typeSize();
\r
160 // From glVertexAttribPointer
\r
164 GLsizei mStride; // 0 means natural stride
\r
168 const void *mPointer;
\r
172 gl::BindingPointer<Buffer> mBoundBuffer; // Captured when glVertexAttribPointer is called.
\r
174 bool mArrayEnabled; // From glEnable/DisableVertexAttribArray
\r
175 float mCurrentValue[4]; // From glVertexAttrib
\r
178 typedef VertexAttribute VertexAttributeArray[MAX_VERTEX_ATTRIBS];
\r
180 // Helper structure to store all raw state
\r
183 Color colorClearValue;
\r
184 GLclampf depthClearValue;
\r
185 int stencilClearValue;
\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
201 GLenum stencilFunc;
\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
224 bool primitiveRestartFixedIndex;
\r
225 bool rasterizerDiscard;
\r
229 GLenum generateMipmapHint;
\r
230 GLenum fragmentShaderDerivativeHint;
\r
234 GLsizei viewportWidth;
\r
235 GLsizei viewportHeight;
\r
241 GLsizei scissorWidth;
\r
242 GLsizei scissorHeight;
\r
245 bool colorMaskGreen;
\r
246 bool colorMaskBlue;
\r
247 bool colorMaskAlpha;
\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
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
262 GLint unpackAlignment;
\r
263 GLint packAlignment;
\r
266 class Context : public egl::Context
\r
269 Context(const egl::Config *config, const Context *shareContext, EGLint clientVersion);
\r
271 virtual void makeCurrent(egl::Surface *surface);
\r
272 virtual void destroy();
\r
273 virtual EGLint getClientVersion();
\r
275 void markAllStateDirty();
\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
282 void setCullFace(bool enabled);
\r
283 bool isCullFaceEnabled() const;
\r
284 void setCullMode(GLenum mode);
\r
285 void setFrontFace(GLenum front);
\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
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
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
307 void setPolygonOffsetFill(bool enabled);
\r
308 bool isPolygonOffsetFillEnabled() const;
\r
309 void setPolygonOffsetParams(GLfloat factor, GLfloat units);
\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
317 void setDither(bool enabled);
\r
318 bool isDitherEnabled() const;
\r
320 void setPrimitiveRestartFixedIndex(bool enabled);
\r
321 bool isPrimitiveRestartFixedIndexEnabled() const;
\r
323 void setRasterizerDiscard(bool enabled);
\r
324 bool isRasterizerDiscardEnabled() const;
\r
326 void setLineWidth(GLfloat width);
\r
328 void setGenerateMipmapHint(GLenum hint);
\r
329 void setFragmentShaderDerivativeHint(GLenum hint);
\r
331 void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height);
\r
333 void setScissorTest(bool enabled);
\r
334 bool isScissorTestEnabled() const;
\r
335 void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height);
\r
337 void setColorMask(bool red, bool green, bool blue, bool alpha);
\r
338 void setDepthMask(bool mask);
\r
340 void setActiveSampler(unsigned int active);
\r
342 GLuint getReadFramebufferName() const;
\r
343 GLuint getDrawFramebufferName() const;
\r
344 GLuint getRenderbufferName() const;
\r
346 GLuint getActiveQuery(GLenum target) const;
\r
348 GLuint getArrayBufferName() const;
\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
356 const VertexAttributeArray &getVertexAttributes();
\r
358 void setUnpackAlignment(GLint alignment);
\r
359 GLint getUnpackAlignment() const;
\r
361 void setPackAlignment(GLint alignment);
\r
362 GLint getPackAlignment() const;
\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
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
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
382 // Fences are owned by the Context
\r
383 GLuint createFence();
\r
384 void deleteFence(GLuint fence);
\r
386 // Queries are owned by the Context
\r
387 GLuint createQuery();
\r
388 void deleteQuery(GLuint query);
\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
401 void beginQuery(GLenum target, GLuint query);
\r
402 void endQuery(GLenum target);
\r
404 void setFramebufferZero(Framebuffer *framebuffer);
\r
406 void setRenderbufferStorage(RenderbufferStorage *renderbuffer);
\r
408 void setVertexAttrib(GLuint index, const GLfloat *values);
\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
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
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
435 bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams);
\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
444 void recordInvalidEnum();
\r
445 void recordInvalidValue();
\r
446 void recordInvalidOperation();
\r
447 void recordOutOfMemory();
\r
448 void recordInvalidFramebufferOperation();
\r
452 static int getSupportedMultiSampleDepth(sw::Format format, int requested);
\r
454 void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
\r
455 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
\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
462 Device *getDevice();
\r
465 virtual ~Context();
\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
476 void detachBuffer(GLuint buffer);
\r
477 void detachTexture(GLuint texture);
\r
478 void detachFramebuffer(GLuint framebuffer);
\r
479 void detachRenderbuffer(GLuint renderbuffer);
\r
481 bool cullSkipsDraw(GLenum drawMode);
\r
482 bool isTriangleMode(GLenum drawMode);
\r
484 const EGLint clientVersion;
\r
485 const egl::Config *const mConfig;
\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
494 typedef std::map<GLint, Framebuffer*> FramebufferMap;
\r
495 FramebufferMap mFramebufferMap;
\r
496 gl::NameSpace mFramebufferNameSpace;
\r
498 typedef std::map<GLint, Fence*> FenceMap;
\r
499 FenceMap mFenceMap;
\r
500 gl::NameSpace mFenceNameSpace;
\r
502 typedef std::map<GLint, Query*> QueryMap;
\r
503 QueryMap mQueryMap;
\r
504 gl::NameSpace mQueryNameSpace;
\r
506 VertexDataManager *mVertexDataManager;
\r
507 IndexDataManager *mIndexDataManager;
\r
511 bool mInvalidValue;
\r
512 bool mInvalidOperation;
\r
514 bool mInvalidFramebufferOperation;
\r
516 bool mHasBeenCurrent;
\r
518 unsigned int mAppliedProgramSerial;
\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
532 ResourceManager *mResourceManager;
\r
536 #endif // INCLUDE_CONTEXT_H_
\r