xref: /aosp_15_r20/external/angle/src/libANGLE/GLES1State.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2018 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 // GLES1State.h: Defines the GLES1State class holding the state of
8 // a GLES1 context.
9 
10 #ifndef LIBANGLE_GLES1STATE_H_
11 #define LIBANGLE_GLES1STATE_H_
12 
13 #include <unordered_set>
14 
15 #include "common/FixedVector.h"
16 #include "common/angleutils.h"
17 #include "common/bitset_utils.h"
18 #include "common/matrix_utils.h"
19 #include "common/vector_utils.h"
20 #include "libANGLE/Caps.h"
21 #include "libANGLE/angletypes.h"
22 
23 namespace gl
24 {
25 
26 // State types specific to GLES1 contexts, from the OpenGL ES 1.1 spec "State Tables" section
27 struct TextureCoordF
28 {
29     TextureCoordF();
30     TextureCoordF(float _s, float _t, float _r, float _q);
31     bool operator==(const TextureCoordF &other) const;
32 
33     GLfloat s = 0.0f;
34     GLfloat t = 0.0f;
35     GLfloat r = 0.0f;
36     GLfloat q = 0.0f;
37 };
38 
39 struct MaterialParameters
40 {
41     MaterialParameters();
42 
43     ColorF ambient;
44     ColorF diffuse;
45     ColorF specular;
46     ColorF emissive;
47     GLfloat specularExponent;
48 };
49 
50 struct LightModelParameters
51 {
52     LightModelParameters();
53 
54     ColorF color;
55     bool twoSided;
56 };
57 
58 struct LightParameters
59 {
60     LightParameters();
61     LightParameters(const LightParameters &other);
62 
63     bool enabled                 = false;
64     ColorF ambient               = {0.0f, 0.0f, 0.0f, 1.0f};
65     ColorF diffuse               = {0.0f, 0.0f, 0.0f, 1.0f};
66     ColorF specular              = {0.0f, 0.0f, 0.0f, 1.0f};
67     angle::Vector4 position      = {0.0f, 0.0f, 1.0f, 0.0f};
68     angle::Vector3 direction     = {0.0f, 0.0f, -1.0f};
69     GLfloat spotlightExponent    = 0.0f;
70     GLfloat spotlightCutoffAngle = 180.0f;
71     GLfloat attenuationConst     = 1.0f;
72     GLfloat attenuationLinear    = 0.0f;
73     GLfloat attenuationQuadratic = 0.0f;
74 };
75 
76 struct FogParameters
77 {
78     FogParameters();
79 
80     FogMode mode;
81     GLfloat density;
82     GLfloat start;
83     GLfloat end;
84     ColorF color;
85 };
86 
87 struct AlphaTestParameters
88 {
89     AlphaTestParameters();
90     bool operator!=(const AlphaTestParameters &other) const;
91 
92     AlphaTestFunc func = AlphaTestFunc::AlwaysPass;
93     GLfloat ref        = 0.0f;
94 };
95 
96 struct TextureEnvironmentParameters
97 {
98     TextureEnvironmentParameters();
99     TextureEnvironmentParameters(const TextureEnvironmentParameters &other);
100 
101     TextureEnvMode mode         = TextureEnvMode::Modulate;
102     TextureCombine combineRgb   = TextureCombine::Modulate;
103     TextureCombine combineAlpha = TextureCombine::Modulate;
104 
105     TextureSrc src0Rgb   = TextureSrc::Texture;
106     TextureSrc src0Alpha = TextureSrc::Texture;
107 
108     TextureSrc src1Rgb   = TextureSrc::Previous;
109     TextureSrc src1Alpha = TextureSrc::Previous;
110 
111     TextureSrc src2Rgb   = TextureSrc::Constant;
112     TextureSrc src2Alpha = TextureSrc::Constant;
113 
114     TextureOp op0Rgb   = TextureOp::SrcColor;
115     TextureOp op0Alpha = TextureOp::SrcAlpha;
116 
117     TextureOp op1Rgb   = TextureOp::SrcColor;
118     TextureOp op1Alpha = TextureOp::SrcAlpha;
119 
120     TextureOp op2Rgb   = TextureOp::SrcAlpha;
121     TextureOp op2Alpha = TextureOp::SrcAlpha;
122 
123     ColorF color       = {0.0f, 0.0f, 0.0f, 0.0f};
124     GLfloat rgbScale   = 1.0f;
125     GLfloat alphaScale = 1.0f;
126 
127     bool pointSpriteCoordReplace = false;
128 
tieTextureEnvironmentParameters129     auto tie() const
130     {
131         return std::tie(mode, combineRgb, combineAlpha, src0Rgb, src0Alpha, src1Rgb, src1Alpha,
132                         src2Rgb, src2Alpha, op0Rgb, op0Alpha, op1Rgb, op1Alpha, op2Rgb, op2Alpha,
133                         color, rgbScale, alphaScale, pointSpriteCoordReplace);
134     }
135 };
136 
137 bool operator==(const TextureEnvironmentParameters &a, const TextureEnvironmentParameters &b);
138 bool operator!=(const TextureEnvironmentParameters &a, const TextureEnvironmentParameters &b);
139 
140 struct PointParameters
141 {
142     PointParameters();
143     PointParameters(const PointParameters &other);
144 
145     GLfloat pointSizeMin                    = 0.0f;
146     GLfloat pointSizeMax                    = 1.0f;
147     GLfloat pointFadeThresholdSize          = 1.0f;
148     angle::Vector3 pointDistanceAttenuation = {1.0f, 0.0f, 0.0f};
149     GLfloat pointSize                       = 1.0f;
150 };
151 
152 struct ClipPlaneParameters
153 {
154     ClipPlaneParameters();
155     ClipPlaneParameters(bool enabled, const angle::Vector4 &equation);
156     ClipPlaneParameters(const ClipPlaneParameters &other);
157     ClipPlaneParameters &operator=(const ClipPlaneParameters &other);
158 
159     bool enabled;
160     angle::Vector4 equation;
161 };
162 
163 class Context;
164 class GLES1Renderer;
165 class PrivateState;
166 
167 class GLES1State final : angle::NonCopyable
168 {
169   public:
170     GLES1State();
171     ~GLES1State();
172 
173     void initialize(const Context *context, const PrivateState *state);
174 
175     void setAlphaTestParameters(AlphaTestFunc func, GLfloat ref);
176     const AlphaTestParameters &getAlphaTestParameters() const;
177 
178     void setClientTextureUnit(unsigned int unit);
179     unsigned int getClientTextureUnit() const;
180 
181     void setCurrentColor(const ColorF &color);
182     const ColorF &getCurrentColor() const;
183 
184     void setCurrentNormal(const angle::Vector3 &normal);
185     const angle::Vector3 &getCurrentNormal() const;
186 
187     void setCurrentTextureCoords(unsigned int unit, const TextureCoordF &coords);
188     const TextureCoordF &getCurrentTextureCoords(unsigned int unit) const;
189 
190     void setMatrixMode(MatrixType mode);
191     MatrixType getMatrixMode() const;
192 
193     GLint getCurrentMatrixStackDepth(GLenum param) const;
194 
195     void pushMatrix();
196     void popMatrix();
197 
198     using MatrixStack = angle::FixedVector<angle::Mat4, Caps::GlobalMatrixStackDepth>;
199     MatrixStack &currentMatrixStack();
200     const MatrixStack &currentMatrixStack() const;
201     const MatrixStack &getMatrixStack(MatrixType mode) const;
202 
203     const angle::Mat4 &getModelviewMatrix() const;
204 
205     void loadMatrix(const angle::Mat4 &m);
206     void multMatrix(const angle::Mat4 &m);
207 
208     void setTextureEnabled(GLint activeSampler, TextureType type, bool enabled);
209 
210     void setLogicOpEnabled(bool enabled);
211     void setLogicOp(LogicalOperation opcodePacked);
212 
213     void setClientStateEnabled(ClientVertexArrayType clientState, bool enable);
214     void setTexCoordArrayEnabled(unsigned int unit, bool enable);
215     bool isClientStateEnabled(ClientVertexArrayType clientState) const;
216     bool isTexCoordArrayEnabled(unsigned int unit) const;
217     bool isTextureTargetEnabled(unsigned int unit, const TextureType type) const;
218 
219     LightModelParameters &lightModelParameters();
220     const LightModelParameters &lightModelParameters() const;
221 
222     LightParameters &lightParameters(unsigned int light);
223     const LightParameters &lightParameters(unsigned int light) const;
224 
225     MaterialParameters &materialParameters();
226     const MaterialParameters &materialParameters() const;
227     bool isColorMaterialEnabled() const;
228 
229     void setShadeModel(ShadingModel model);
230 
231     void setClipPlane(unsigned int plane, const GLfloat *equation);
232     void getClipPlane(unsigned int plane, GLfloat *equation) const;
233 
234     FogParameters &fogParameters();
235     const FogParameters &fogParameters() const;
236 
237     TextureEnvironmentParameters &textureEnvironment(unsigned int unit);
238     const TextureEnvironmentParameters &textureEnvironment(unsigned int unit) const;
239 
240     PointParameters &pointParameters();
241     const PointParameters &pointParameters() const;
242 
243     AttributesMask getVertexArraysAttributeMask() const;
244     AttributesMask getActiveAttributesMask() const;
245 
246     bool shouldHandleDirtyProgram();
247 
248     void setHint(GLenum target, GLenum mode);
249     GLenum getHint(GLenum target) const;
250 
251     enum DirtyGles1Type
252     {
253         DIRTY_GLES1_TEXTURE_UNIT_ENABLE = 0,
254         DIRTY_GLES1_CLIENT_STATE_ENABLE,
255         DIRTY_GLES1_FEATURE_ENABLE,
256         DIRTY_GLES1_CURRENT_VECTOR,
257         DIRTY_GLES1_CLIENT_ACTIVE_TEXTURE,
258         DIRTY_GLES1_MATRICES,
259         DIRTY_GLES1_TEXTURE_ENVIRONMENT,
260         DIRTY_GLES1_MATERIAL,
261         DIRTY_GLES1_LIGHTS,
262         DIRTY_GLES1_FOG,
263         DIRTY_GLES1_SHADE_MODEL,
264         DIRTY_GLES1_POINT_PARAMETERS,
265         DIRTY_GLES1_ALPHA_TEST,
266         DIRTY_GLES1_LOGIC_OP,
267         DIRTY_GLES1_CLIP_PLANES,
268         DIRTY_GLES1_HINT_SETTING,
269         DIRTY_GLES1_PROGRAM,
270         DIRTY_GLES1_MAX,
271     };
272 
setAllDirty()273     void setAllDirty() { mDirtyBits.set(); }
274 
275   private:
276     friend class PrivateState;
277     friend class GLES1Renderer;
278 
279     // Back pointer for reading from State.
280     const PrivateState *mGLState;
281 
282     using DirtyBits = angle::BitSet<DIRTY_GLES1_MAX>;
283     DirtyBits mDirtyBits;
284 
setDirty(DirtyGles1Type type)285     void setDirty(DirtyGles1Type type) { mDirtyBits.set(type); }
clearDirty()286     void clearDirty() { mDirtyBits.reset(); }
clearDirtyBits(const DirtyGles1Type & bitset)287     void clearDirtyBits(const DirtyGles1Type &bitset) { mDirtyBits &= ~bitset; }
isDirty(DirtyGles1Type type)288     bool isDirty(DirtyGles1Type type) const { return mDirtyBits.test(type); }
289 
290     // All initial state values come from the
291     // OpenGL ES 1.1 spec.
292     std::vector<angle::PackedEnumBitSet<TextureType>> mTexUnitEnables;
293 
294     // Table 6.4, 6.5 (IsEnabled)
295     bool mVertexArrayEnabled;
296     bool mNormalArrayEnabled;
297     bool mColorArrayEnabled;
298     bool mPointSizeArrayEnabled;
299     std::vector<bool> mTexCoordArrayEnabled;
300 
301     // Table 6.7-6.16 (IsEnabled)
302     bool mLineSmoothEnabled;
303     bool mPointSmoothEnabled;
304     bool mPointSpriteEnabled;
305     bool mAlphaTestEnabled;
306     bool mLogicOpEnabled;
307     bool mLightingEnabled;
308     bool mFogEnabled;
309     bool mRescaleNormalEnabled;
310     bool mNormalizeEnabled;
311     bool mColorMaterialEnabled;
312     bool mReflectionMapEnabled;
313 
314     // Table 6.3
315     ColorF mCurrentColor;
316     angle::Vector3 mCurrentNormal;
317     // Invariant: mCurrentTextureCoords size is == GL_MAX_TEXTURE_UNITS.
318     std::vector<TextureCoordF> mCurrentTextureCoords;
319 
320     // Table 6.4
321     unsigned int mClientActiveTexture;
322 
323     // Table 6.7
324     MatrixType mMatrixMode;
325     MatrixStack mProjectionMatrices;
326     MatrixStack mModelviewMatrices;
327     std::vector<MatrixStack> mTextureMatrices;
328 
329     // Table 6.15
330     using TextureEnvironments = std::vector<TextureEnvironmentParameters>;
331     TextureEnvironments mTextureEnvironments;
332 
333     // Table 6.9, 2.8
334     MaterialParameters mMaterial;
335     LightModelParameters mLightModel;
336 
337     // Table 6.10
338     std::vector<LightParameters> mLights;
339 
340     // Table 6.8
341     FogParameters mFog;
342     ShadingModel mShadeModel;
343 
344     // Table 6.11
345     PointParameters mPointParameters;
346 
347     // Table 6.16
348     AlphaTestParameters mAlphaTestParameters;
349     LogicalOperation mLogicOp;
350 
351     // Table 6.7
352     std::vector<ClipPlaneParameters> mClipPlanes;
353 
354     // Table 6.19
355     HintSetting mLineSmoothHint;
356     HintSetting mPointSmoothHint;
357     HintSetting mPerspectiveCorrectionHint;
358     HintSetting mFogHint;
359 };
360 
361 }  // namespace gl
362 
363 #endif  // LIBANGLE_GLES1STATE_H_
364