summaryrefslogtreecommitdiffstats
path: root/gfx/angle/checkout/src/libANGLE/GLES1Renderer.h
blob: 5694b79752fd003e9b7cecb275e3f42110a305cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
// Copyright 2018 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

// GLES1Renderer.h: Defines GLES1 emulation rendering operations on top of a GLES3
// context. Used by Context.h.

#ifndef LIBANGLE_GLES1_RENDERER_H_
#define LIBANGLE_GLES1_RENDERER_H_

#include "GLES1State.h"
#include "angle_gl.h"
#include "common/angleutils.h"
#include "libANGLE/angletypes.h"

#include <memory>
#include <string>
#include <unordered_map>

namespace gl
{
class Context;
class GLES1State;
class Program;
class State;
class Shader;
class ShaderProgramManager;

enum class GLES1StateEnables : uint64_t
{
    Lighting                       = 0,
    Fog                            = 1,
    ClipPlanes                     = 2,
    DrawTexture                    = 3,
    PointRasterization             = 4,
    PointSprite                    = 5,
    RescaleNormal                  = 6,
    Normalize                      = 7,
    AlphaTest                      = 8,
    ShadeModelFlat                 = 9,
    ColorMaterial                  = 10,
    LightModelTwoSided             = 11,
    LogicOpThroughFramebufferFetch = 12,

    InvalidEnum = 13,
    EnumCount   = 13,
};

constexpr int kClipPlaneCount = 6;
constexpr int kTexUnitCount   = 4;
constexpr int kLightCount     = 8;

using GLES1StateEnabledBitSet = angle::PackedEnumBitSet<GLES1StateEnables, uint64_t>;

struct GLES1ShaderState
{
    GLES1ShaderState();
    ~GLES1ShaderState();
    GLES1ShaderState(const GLES1ShaderState &other);

    size_t hash() const;

    GLES1StateEnabledBitSet mGLES1StateEnabled;

    using BoolLightArray     = bool[kLightCount];
    using BoolTexArray       = bool[kTexUnitCount];
    using BoolClipPlaneArray = bool[kClipPlaneCount];
    using IntTexArray        = int[kTexUnitCount];

    BoolTexArray tex2DEnables   = {false, false, false, false};
    BoolTexArray texCubeEnables = {false, false, false, false};

    IntTexArray tex2DFormats = {GL_RGBA, GL_RGBA, GL_RGBA, GL_RGBA};

    IntTexArray texEnvModes          = {};
    IntTexArray texCombineRgbs       = {};
    IntTexArray texCombineAlphas     = {};
    IntTexArray texCombineSrc0Rgbs   = {};
    IntTexArray texCombineSrc0Alphas = {};
    IntTexArray texCombineSrc1Rgbs   = {};
    IntTexArray texCombineSrc1Alphas = {};
    IntTexArray texCombineSrc2Rgbs   = {};
    IntTexArray texCombineSrc2Alphas = {};
    IntTexArray texCombineOp0Rgbs    = {};
    IntTexArray texCombineOp0Alphas  = {};
    IntTexArray texCombineOp1Rgbs    = {};
    IntTexArray texCombineOp1Alphas  = {};
    IntTexArray texCombineOp2Rgbs    = {};
    IntTexArray texCombineOp2Alphas  = {};

    BoolTexArray pointSpriteCoordReplaces = {};

    BoolLightArray lightEnables = {};

    BoolClipPlaneArray clipPlaneEnables = {};

    AlphaTestFunc alphaTestFunc = {};

    FogMode fogMode = {};
};

bool operator==(const GLES1ShaderState &a, const GLES1ShaderState &b);
bool operator!=(const GLES1ShaderState &a, const GLES1ShaderState &b);

}  // namespace gl

namespace std
{
template <>
struct hash<gl::GLES1ShaderState>
{
    size_t operator()(const gl::GLES1ShaderState &key) const { return key.hash(); }
};
}  // namespace std

namespace gl
{

class GLES1Renderer final : angle::NonCopyable
{
  public:
    GLES1Renderer();
    ~GLES1Renderer();

    void onDestroy(Context *context, State *state);

    angle::Result prepareForDraw(PrimitiveMode mode, Context *context, State *glState);

    static int VertexArrayIndex(ClientVertexArrayType type, const GLES1State &gles1);
    static ClientVertexArrayType VertexArrayType(int attribIndex);
    static int TexCoordArrayIndex(unsigned int unit);

    void drawTexture(Context *context,
                     State *glState,
                     float x,
                     float y,
                     float z,
                     float width,
                     float height);

  private:
    using Mat4Uniform = float[16];
    using Vec4Uniform = float[4];
    using Vec3Uniform = float[3];

    Shader *getShader(ShaderProgramID handle) const;
    Program *getProgram(ShaderProgramID handle) const;

    angle::Result compileShader(Context *context,
                                ShaderType shaderType,
                                const char *src,
                                ShaderProgramID *shaderOut);
    angle::Result linkProgram(Context *context,
                              State *glState,
                              ShaderProgramID vshader,
                              ShaderProgramID fshader,
                              const angle::HashMap<GLint, std::string> &attribLocs,
                              ShaderProgramID *programOut);
    angle::Result initializeRendererProgram(Context *context, State *glState);

    void setUniform1i(Context *context,
                      Program *programObject,
                      UniformLocation location,
                      GLint value);
    void setUniform1ui(Program *programObject, UniformLocation location, GLuint value);
    void setUniform1iv(Context *context,
                       Program *programObject,
                       UniformLocation location,
                       GLint count,
                       const GLint *value);
    void setUniformMatrix4fv(Program *programObject,
                             UniformLocation location,
                             GLint count,
                             GLboolean transpose,
                             const GLfloat *value);
    void setUniform4fv(Program *programObject,
                       UniformLocation location,
                       GLint count,
                       const GLfloat *value);
    void setUniform3fv(Program *programObject,
                       UniformLocation location,
                       GLint count,
                       const GLfloat *value);
    void setUniform2fv(Program *programObject,
                       UniformLocation location,
                       GLint count,
                       const GLfloat *value);
    void setUniform1f(Program *programObject, UniformLocation location, GLfloat value);
    void setUniform1fv(Program *programObject,
                       UniformLocation location,
                       GLint count,
                       const GLfloat *value);

    void setAttributesEnabled(Context *context, State *glState, AttributesMask mask);

    static constexpr int kVertexAttribIndex           = 0;
    static constexpr int kNormalAttribIndex           = 1;
    static constexpr int kColorAttribIndex            = 2;
    static constexpr int kPointSizeAttribIndex        = 3;
    static constexpr int kTextureCoordAttribIndexBase = 4;

    bool mRendererProgramInitialized;
    ShaderProgramManager *mShaderPrograms;

    GLES1ShaderState mShaderState = {};

    const char *getShaderBool(GLES1StateEnables state);
    void addShaderDefine(std::stringstream &outStream,
                         GLES1StateEnables state,
                         const char *enableString);
    void addShaderInt(std::stringstream &outStream, const char *name, int value);
    void addShaderIntTexArray(std::stringstream &outStream,
                              const char *texString,
                              GLES1ShaderState::IntTexArray &texState);
    void addShaderBoolTexArray(std::stringstream &outStream,
                               const char *texString,
                               GLES1ShaderState::BoolTexArray &texState);
    void addShaderBoolLightArray(std::stringstream &outStream,
                                 const char *name,
                                 GLES1ShaderState::BoolLightArray &value);
    void addShaderBoolClipPlaneArray(std::stringstream &outStream,
                                     const char *name,
                                     GLES1ShaderState::BoolClipPlaneArray &value);
    void addVertexShaderDefs(std::stringstream &outStream);
    void addFragmentShaderDefs(std::stringstream &outStream);

    struct GLES1ProgramState
    {
        ShaderProgramID program;

        UniformLocation projMatrixLoc;
        UniformLocation modelviewMatrixLoc;
        UniformLocation textureMatrixLoc;
        UniformLocation modelviewInvTrLoc;

        // Texturing
        std::array<UniformLocation, kTexUnitCount> tex2DSamplerLocs;
        std::array<UniformLocation, kTexUnitCount> texCubeSamplerLocs;

        UniformLocation textureEnvColorLoc;
        UniformLocation rgbScaleLoc;
        UniformLocation alphaScaleLoc;

        // Alpha test
        UniformLocation alphaTestRefLoc;

        // Shading, materials, and lighting
        UniformLocation materialAmbientLoc;
        UniformLocation materialDiffuseLoc;
        UniformLocation materialSpecularLoc;
        UniformLocation materialEmissiveLoc;
        UniformLocation materialSpecularExponentLoc;

        UniformLocation lightModelSceneAmbientLoc;

        UniformLocation lightAmbientsLoc;
        UniformLocation lightDiffusesLoc;
        UniformLocation lightSpecularsLoc;
        UniformLocation lightPositionsLoc;
        UniformLocation lightDirectionsLoc;
        UniformLocation lightSpotlightExponentsLoc;
        UniformLocation lightSpotlightCutoffAnglesLoc;
        UniformLocation lightAttenuationConstsLoc;
        UniformLocation lightAttenuationLinearsLoc;
        UniformLocation lightAttenuationQuadraticsLoc;

        // Fog
        UniformLocation fogDensityLoc;
        UniformLocation fogStartLoc;
        UniformLocation fogEndLoc;
        UniformLocation fogColorLoc;

        // Clip planes
        UniformLocation clipPlanesLoc;

        // Logic op
        UniformLocation logicOpLoc;

        // Point rasterization
        UniformLocation pointSizeMinLoc;
        UniformLocation pointSizeMaxLoc;
        UniformLocation pointDistanceAttenuationLoc;

        // Draw texture
        UniformLocation drawTextureCoordsLoc;
        UniformLocation drawTextureDimsLoc;
        UniformLocation drawTextureNormalizedCropRectLoc;
    };

    struct GLES1UniformBuffers
    {
        std::array<Mat4Uniform, kTexUnitCount> textureMatrices;

        std::array<Vec4Uniform, kTexUnitCount> texEnvColors;
        std::array<GLfloat, kTexUnitCount> texEnvRgbScales;
        std::array<GLfloat, kTexUnitCount> texEnvAlphaScales;

        // Lighting
        std::array<Vec4Uniform, kLightCount> lightAmbients;
        std::array<Vec4Uniform, kLightCount> lightDiffuses;
        std::array<Vec4Uniform, kLightCount> lightSpeculars;
        std::array<Vec4Uniform, kLightCount> lightPositions;
        std::array<Vec3Uniform, kLightCount> lightDirections;
        std::array<GLfloat, kLightCount> spotlightExponents;
        std::array<GLfloat, kLightCount> spotlightCutoffAngles;
        std::array<GLfloat, kLightCount> attenuationConsts;
        std::array<GLfloat, kLightCount> attenuationLinears;
        std::array<GLfloat, kLightCount> attenuationQuadratics;

        // Clip planes
        std::array<Vec4Uniform, kClipPlaneCount> clipPlanes;

        // Texture crop rectangles
        std::array<Vec4Uniform, kTexUnitCount> texCropRects;
    };

    struct GLES1UberShaderState
    {
        GLES1UniformBuffers uniformBuffers;
        GLES1ProgramState programState;
    };

    GLES1UberShaderState &getUberShaderState()
    {
        ASSERT(mUberShaderState.find(mShaderState) != mUberShaderState.end());
        return mUberShaderState[mShaderState];
    }

    angle::HashMap<GLES1ShaderState, GLES1UberShaderState> mUberShaderState;

    bool mDrawTextureEnabled      = false;
    GLfloat mDrawTextureCoords[4] = {0.0f, 0.0f, 0.0f, 0.0f};
    GLfloat mDrawTextureDims[2]   = {0.0f, 0.0f};
};

}  // namespace gl

#endif  // LIBANGLE_GLES1_RENDERER_H_