1//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// State.h: Defines the State class, encapsulating raw GL state
8
9#ifndef LIBGLESV2_STATE_H_
10#define LIBGLESV2_STATE_H_
11
12#include "common/angleutils.h"
13#include "common/RefCountObject.h"
14#include "libGLESv2/angletypes.h"
15#include "libGLESv2/VertexAttribute.h"
16#include "libGLESv2/Renderbuffer.h"
17#include "libGLESv2/Texture.h"
18#include "libGLESv2/TransformFeedback.h"
19#include "libGLESv2/Program.h"
20#include "libGLESv2/Sampler.h"
21
22namespace gl
23{
24class Query;
25class VertexArray;
26class Context;
27struct Caps;
28
29class State
30{
31  public:
32    State();
33    ~State();
34
35    void initialize(const Caps& caps, GLuint clientVersion);
36    void reset();
37
38    void setContext(Context *context) { mContext = context; }
39
40    // State chunk getters
41    const RasterizerState &getRasterizerState() const;
42    const BlendState &getBlendState() const;
43    const DepthStencilState &getDepthStencilState() const;
44
45    // Clear behavior setters & state parameter block generation function
46    void setClearColor(float red, float green, float blue, float alpha);
47    void setClearDepth(float depth);
48    void setClearStencil(int stencil);
49    ClearParameters getClearParameters(GLbitfield mask) const;
50
51    // Write mask manipulation
52    void setColorMask(bool red, bool green, bool blue, bool alpha);
53    void setDepthMask(bool mask);
54
55    // Discard toggle & query
56    bool isRasterizerDiscardEnabled() const;
57    void setRasterizerDiscard(bool enabled);
58
59    // Face culling state manipulation
60    bool isCullFaceEnabled() const;
61    void setCullFace(bool enabled);
62    void setCullMode(GLenum mode);
63    void setFrontFace(GLenum front);
64
65    // Depth test state manipulation
66    bool isDepthTestEnabled() const;
67    void setDepthTest(bool enabled);
68    void setDepthFunc(GLenum depthFunc);
69    void setDepthRange(float zNear, float zFar);
70    void getDepthRange(float *zNear, float *zFar) const;
71
72    // Blend state manipulation
73    bool isBlendEnabled() const;
74    void setBlend(bool enabled);
75    void setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha);
76    void setBlendColor(float red, float green, float blue, float alpha);
77    void setBlendEquation(GLenum rgbEquation, GLenum alphaEquation);
78    const ColorF &getBlendColor() const;
79
80    // Stencil state maniupulation
81    bool isStencilTestEnabled() const;
82    void setStencilTest(bool enabled);
83    void setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask);
84    void setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask);
85    void setStencilWritemask(GLuint stencilWritemask);
86    void setStencilBackWritemask(GLuint stencilBackWritemask);
87    void setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass);
88    void setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass);
89    GLint getStencilRef() const;
90    GLint getStencilBackRef() const;
91
92    // Depth bias/polygon offset state manipulation
93    bool isPolygonOffsetFillEnabled() const;
94    void setPolygonOffsetFill(bool enabled);
95    void setPolygonOffsetParams(GLfloat factor, GLfloat units);
96
97    // Multisample coverage state manipulation
98    bool isSampleAlphaToCoverageEnabled() const;
99    void setSampleAlphaToCoverage(bool enabled);
100    bool isSampleCoverageEnabled() const;
101    void setSampleCoverage(bool enabled);
102    void setSampleCoverageParams(GLclampf value, bool invert);
103    void getSampleCoverageParams(GLclampf *value, bool *invert);
104
105    // Scissor test state toggle & query
106    bool isScissorTestEnabled() const;
107    void setScissorTest(bool enabled);
108    void setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height);
109    const Rectangle &getScissor() const;
110
111    // Dither state toggle & query
112    bool isDitherEnabled() const;
113    void setDither(bool enabled);
114
115    // Generic state toggle & query
116    void setEnableFeature(GLenum feature, bool enabled);
117    bool getEnableFeature(GLenum feature);
118
119    // Line width state setter
120    void setLineWidth(GLfloat width);
121
122    // Hint setters
123    void setGenerateMipmapHint(GLenum hint);
124    void setFragmentShaderDerivativeHint(GLenum hint);
125
126    // Viewport state setter/getter
127    void setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height);
128    const Rectangle &getViewport() const;
129
130    // Texture binding & active texture unit manipulation
131    void setActiveSampler(unsigned int active);
132    unsigned int getActiveSampler() const;
133    void setSamplerTexture(GLenum type, Texture *texture);
134    Texture *getSamplerTexture(unsigned int sampler, GLenum type) const;
135    GLuint getSamplerTextureId(unsigned int sampler, GLenum type) const;
136    void detachTexture(GLuint texture);
137
138    // Sampler object binding manipulation
139    void setSamplerBinding(GLuint textureUnit, Sampler *sampler);
140    GLuint getSamplerId(GLuint textureUnit) const;
141    Sampler *getSampler(GLuint textureUnit) const;
142    void detachSampler(GLuint sampler);
143
144    // Renderbuffer binding manipulation
145    void setRenderbufferBinding(Renderbuffer *renderbuffer);
146    GLuint getRenderbufferId() const;
147    Renderbuffer *getCurrentRenderbuffer();
148    void detachRenderbuffer(GLuint renderbuffer);
149
150    // Framebuffer binding manipulation
151    void setReadFramebufferBinding(Framebuffer *framebuffer);
152    void setDrawFramebufferBinding(Framebuffer *framebuffer);
153    Framebuffer *getTargetFramebuffer(GLenum target) const;
154    Framebuffer *getReadFramebuffer();
155    Framebuffer *getDrawFramebuffer();
156    const Framebuffer *getReadFramebuffer() const;
157    const Framebuffer *getDrawFramebuffer() const;
158    bool removeReadFramebufferBinding(GLuint framebuffer);
159    bool removeDrawFramebufferBinding(GLuint framebuffer);
160
161    // Vertex array object binding manipulation
162    void setVertexArrayBinding(VertexArray *vertexArray);
163    GLuint getVertexArrayId() const;
164    VertexArray *getVertexArray() const;
165    bool removeVertexArrayBinding(GLuint vertexArray);
166
167    // Program binding manipulation
168    void setCurrentProgram(GLuint programId, Program *newProgram);
169    void setCurrentProgramBinary(ProgramBinary *binary);
170    GLuint getCurrentProgramId() const;
171    ProgramBinary *getCurrentProgramBinary() const;
172
173    // Transform feedback object (not buffer) binding manipulation
174    void setTransformFeedbackBinding(TransformFeedback *transformFeedback);
175    TransformFeedback *getCurrentTransformFeedback() const;
176    void detachTransformFeedback(GLuint transformFeedback);
177
178    // Query binding manipulation
179    bool isQueryActive() const;
180    void setActiveQuery(GLenum target, Query *query);
181    GLuint getActiveQueryId(GLenum target) const;
182    Query *getActiveQuery(GLenum target) const;
183
184    //// Typed buffer binding point manipulation ////
185    // GL_ARRAY_BUFFER
186    void setArrayBufferBinding(Buffer *buffer);
187    GLuint getArrayBufferId() const;
188    bool removeArrayBufferBinding(GLuint buffer);
189
190    // GL_UNIFORM_BUFFER - Both indexed and generic targets
191    void setGenericUniformBufferBinding(Buffer *buffer);
192    void setIndexedUniformBufferBinding(GLuint index, Buffer *buffer, GLintptr offset, GLsizeiptr size);
193    GLuint getIndexedUniformBufferId(GLuint index) const;
194    Buffer *getIndexedUniformBuffer(GLuint index) const;
195
196    // GL_TRANSFORM_FEEDBACK_BUFFER - Both indexed and generic targets
197    void setGenericTransformFeedbackBufferBinding(Buffer *buffer);
198    void setIndexedTransformFeedbackBufferBinding(GLuint index, Buffer *buffer, GLintptr offset, GLsizeiptr size);
199    GLuint getIndexedTransformFeedbackBufferId(GLuint index) const;
200    Buffer *getIndexedTransformFeedbackBuffer(GLuint index) const;
201    GLuint getIndexedTransformFeedbackBufferOffset(GLuint index) const;
202
203    // GL_COPY_[READ/WRITE]_BUFFER
204    void setCopyReadBufferBinding(Buffer *buffer);
205    void setCopyWriteBufferBinding(Buffer *buffer);
206
207    // GL_PIXEL[PACK/UNPACK]_BUFFER
208    void setPixelPackBufferBinding(Buffer *buffer);
209    void setPixelUnpackBufferBinding(Buffer *buffer);
210
211    // Retrieve typed buffer by target (non-indexed)
212    Buffer *getTargetBuffer(GLenum target) const;
213
214    // Vertex attrib manipulation
215    void setEnableVertexAttribArray(unsigned int attribNum, bool enabled);
216    void setVertexAttribf(GLuint index, const GLfloat values[4]);
217    void setVertexAttribu(GLuint index, const GLuint values[4]);
218    void setVertexAttribi(GLuint index, const GLint values[4]);
219    void setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type,
220                              bool normalized, bool pureInteger, GLsizei stride, const void *pointer);
221    const VertexAttribute &getVertexAttribState(unsigned int attribNum) const;
222    const VertexAttribCurrentValueData &getVertexAttribCurrentValue(unsigned int attribNum) const;
223    const VertexAttribCurrentValueData *getVertexAttribCurrentValues() const;
224    const void *getVertexAttribPointer(unsigned int attribNum) const;
225
226    // Pixel pack state manipulation
227    void setPackAlignment(GLint alignment);
228    GLint getPackAlignment() const;
229    void setPackReverseRowOrder(bool reverseRowOrder);
230    bool getPackReverseRowOrder() const;
231    const PixelPackState &getPackState() const;
232
233    // Pixel unpack state manipulation
234    void setUnpackAlignment(GLint alignment);
235    GLint getUnpackAlignment() const;
236    const PixelUnpackState &getUnpackState() const;
237
238    // State query functions
239    void getBooleanv(GLenum pname, GLboolean *params);
240    void getFloatv(GLenum pname, GLfloat *params);
241    void getIntegerv(GLenum pname, GLint *params);
242    bool getIndexedIntegerv(GLenum target, GLuint index, GLint *data);
243    bool getIndexedInteger64v(GLenum target, GLuint index, GLint64 *data);
244
245    bool hasMappedBuffer(GLenum target) const;
246
247  private:
248    DISALLOW_COPY_AND_ASSIGN(State);
249
250    Context *mContext;
251
252    ColorF mColorClearValue;
253    GLclampf mDepthClearValue;
254    int mStencilClearValue;
255
256    RasterizerState mRasterizer;
257    bool mScissorTest;
258    Rectangle mScissor;
259
260    BlendState mBlend;
261    ColorF mBlendColor;
262    bool mSampleCoverage;
263    GLclampf mSampleCoverageValue;
264    bool mSampleCoverageInvert;
265
266    DepthStencilState mDepthStencil;
267    GLint mStencilRef;
268    GLint mStencilBackRef;
269
270    GLfloat mLineWidth;
271
272    GLenum mGenerateMipmapHint;
273    GLenum mFragmentShaderDerivativeHint;
274
275    Rectangle mViewport;
276    float mNearZ;
277    float mFarZ;
278
279    BindingPointer<Buffer> mArrayBuffer;
280    Framebuffer *mReadFramebuffer;
281    Framebuffer *mDrawFramebuffer;
282    BindingPointer<Renderbuffer> mRenderbuffer;
283    GLuint mCurrentProgramId;
284    BindingPointer<ProgramBinary> mCurrentProgramBinary;
285
286    VertexAttribCurrentValueData mVertexAttribCurrentValues[MAX_VERTEX_ATTRIBS]; // From glVertexAttrib
287    VertexArray *mVertexArray;
288
289    // Texture and sampler bindings
290    size_t mActiveSampler;   // Active texture unit selector - GL_TEXTURE0
291
292    typedef std::vector< BindingPointer<Texture> > TextureBindingVector;
293    typedef std::map<GLenum, TextureBindingVector> TextureBindingMap;
294    TextureBindingMap mSamplerTextures;
295
296    typedef std::vector< BindingPointer<Sampler> > SamplerBindingVector;
297    SamplerBindingVector mSamplers;
298
299    typedef std::map< GLenum, BindingPointer<Query> > ActiveQueryMap;
300    ActiveQueryMap mActiveQueries;
301
302    BindingPointer<Buffer> mGenericUniformBuffer;
303    OffsetBindingPointer<Buffer> mUniformBuffers[IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS];
304
305    BindingPointer<TransformFeedback> mTransformFeedback;
306    BindingPointer<Buffer> mGenericTransformFeedbackBuffer;
307    OffsetBindingPointer<Buffer> mTransformFeedbackBuffers[IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_BUFFERS];
308
309    BindingPointer<Buffer> mCopyReadBuffer;
310    BindingPointer<Buffer> mCopyWriteBuffer;
311
312    PixelUnpackState mUnpack;
313    PixelPackState mPack;
314};
315
316}
317
318#endif // LIBGLESV2_STATE_H_
319
320