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 "HandleAllocator.h"
\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
29 #include <EGL/egl.h>
\r
43 struct TranslatedAttribute;
\r
44 struct TranslatedIndexData;
\r
53 class TextureCubeMap;
\r
54 class TextureExternal;
\r
57 class RenderbufferStorage;
\r
60 class StreamingIndexBuffer;
\r
61 class Stencilbuffer;
\r
62 class DepthStencilbuffer;
\r
63 class VertexDataManager;
\r
64 class IndexDataManager;
\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
81 const GLenum compressedTextureFormats[] =
\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
92 const GLint NUM_COMPRESSED_TEXTURE_FORMATS = sizeof(compressedTextureFormats) / sizeof(compressedTextureFormats[0]);
\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
102 QUERY_ANY_SAMPLES_PASSED,
\r
103 QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE,
\r
116 // Helper structure describing a single vertex attribute
\r
117 class VertexAttribute
\r
120 VertexAttribute() : mType(GL_FLOAT), mSize(0), mNormalized(false), mStride(0), mPointer(NULL), mArrayEnabled(false)
\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
128 int typeSize() const
\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
142 GLsizei stride() const
\r
144 return mStride ? mStride : typeSize();
\r
147 // From glVertexAttribPointer
\r
151 GLsizei mStride; // 0 means natural stride
\r
155 const void *mPointer;
\r
159 gl::BindingPointer<Buffer> mBoundBuffer; // Captured when glVertexAttribPointer is called.
\r
161 bool mArrayEnabled; // From glEnable/DisableVertexAttribArray
\r
162 float mCurrentValue[4]; // From glVertexAttrib
\r
165 typedef VertexAttribute VertexAttributeArray[MAX_VERTEX_ATTRIBS];
\r
167 // Helper structure to store all raw state
\r
170 Color colorClearValue;
\r
171 GLclampf depthClearValue;
\r
172 int stencilClearValue;
\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
188 GLenum stencilFunc;
\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
214 GLenum generateMipmapHint;
\r
215 GLenum fragmentShaderDerivativeHint;
\r
219 GLsizei viewportWidth;
\r
220 GLsizei viewportHeight;
\r
226 GLsizei scissorWidth;
\r
227 GLsizei scissorHeight;
\r
230 bool colorMaskGreen;
\r
231 bool colorMaskBlue;
\r
232 bool colorMaskAlpha;
\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
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
247 GLint unpackAlignment;
\r
248 GLint packAlignment;
\r
251 class Context : public egl::Context
\r
254 Context(const egl::Config *config, const Context *shareContext);
\r
256 virtual void makeCurrent(egl::Surface *surface);
\r
257 virtual void destroy();
\r
258 virtual int getClientVersion();
\r
260 void markAllStateDirty();
\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
267 void setCullFace(bool enabled);
\r
268 bool isCullFaceEnabled() const;
\r
269 void setCullMode(GLenum mode);
\r
270 void setFrontFace(GLenum front);
\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
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
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
292 void setPolygonOffsetFill(bool enabled);
\r
293 bool isPolygonOffsetFillEnabled() const;
\r
294 void setPolygonOffsetParams(GLfloat factor, GLfloat units);
\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
302 void setDither(bool enabled);
\r
303 bool isDitherEnabled() const;
\r
305 void setLineWidth(GLfloat width);
\r
307 void setGenerateMipmapHint(GLenum hint);
\r
308 void setFragmentShaderDerivativeHint(GLenum hint);
\r
310 void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height);
\r
312 void setScissorTest(bool enabled);
\r
313 bool isScissorTestEnabled() const;
\r
314 void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height);
\r
316 void setColorMask(bool red, bool green, bool blue, bool alpha);
\r
317 void setDepthMask(bool mask);
\r
319 void setActiveSampler(unsigned int active);
\r
321 GLuint getReadFramebufferHandle() const;
\r
322 GLuint getDrawFramebufferHandle() const;
\r
323 GLuint getRenderbufferHandle() const;
\r
325 GLuint getActiveQuery(GLenum target) const;
\r
327 GLuint getArrayBufferHandle() const;
\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
335 const VertexAttributeArray &getVertexAttributes();
\r
337 void setUnpackAlignment(GLint alignment);
\r
338 GLint getUnpackAlignment() const;
\r
340 void setPackAlignment(GLint alignment);
\r
341 GLint getPackAlignment() const;
\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
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
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
361 // Fences are owned by the Context
\r
362 GLuint createFence();
\r
363 void deleteFence(GLuint fence);
\r
365 // Queries are owned by the Context
\r
366 GLuint createQuery();
\r
367 void deleteQuery(GLuint query);
\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
380 void beginQuery(GLenum target, GLuint query);
\r
381 void endQuery(GLenum target);
\r
383 void setFramebufferZero(Framebuffer *framebuffer);
\r
385 void setRenderbufferStorage(RenderbufferStorage *renderbuffer);
\r
387 void setVertexAttrib(GLuint index, const GLfloat *values);
\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
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
409 bool getFloatv(GLenum pname, GLfloat *params);
\r
410 bool getIntegerv(GLenum pname, GLint *params);
\r
411 bool getBooleanv(GLenum pname, GLboolean *params);
\r
413 bool getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams);
\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
422 void recordInvalidEnum();
\r
423 void recordInvalidValue();
\r
424 void recordInvalidOperation();
\r
425 void recordOutOfMemory();
\r
426 void recordInvalidFramebufferOperation();
\r
430 static int getSupportedMultiSampleDepth(sw::Format format, int requested);
\r
432 void blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
\r
433 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
\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
440 Device *getDevice();
\r
443 virtual ~Context();
\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
454 void detachBuffer(GLuint buffer);
\r
455 void detachTexture(GLuint texture);
\r
456 void detachFramebuffer(GLuint framebuffer);
\r
457 void detachRenderbuffer(GLuint renderbuffer);
\r
459 bool cullSkipsDraw(GLenum drawMode);
\r
460 bool isTriangleMode(GLenum drawMode);
\r
462 const egl::Config *const mConfig;
\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
471 typedef std::map<GLint, Framebuffer*> FramebufferMap;
\r
472 FramebufferMap mFramebufferMap;
\r
473 HandleAllocator mFramebufferHandleAllocator;
\r
475 typedef std::map<GLint, Fence*> FenceMap;
\r
476 FenceMap mFenceMap;
\r
477 HandleAllocator mFenceHandleAllocator;
\r
479 typedef std::map<GLint, Query*> QueryMap;
\r
480 QueryMap mQueryMap;
\r
481 HandleAllocator mQueryHandleAllocator;
\r
483 VertexDataManager *mVertexDataManager;
\r
484 IndexDataManager *mIndexDataManager;
\r
488 bool mInvalidValue;
\r
489 bool mInvalidOperation;
\r
491 bool mInvalidFramebufferOperation;
\r
493 bool mHasBeenCurrent;
\r
495 unsigned int mAppliedProgramSerial;
\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
509 ResourceManager *mResourceManager;
\r
513 #endif // INCLUDE_CONTEXT_H_
\r