1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #pragma once
18 
19 #include <GLES/gl.h>
20 #include <GLES/glext.h>
21 
22 #include <glm/mat4x4.hpp>
23 
24 #include <vector>
25 #include <unordered_map>
26 #include <unordered_set>
27 
28 class GLEScmContext;
29 class GLESpointer;
30 
31 class CoreProfileEngine {
32 public:
33     CoreProfileEngine(GLEScmContext* ctx, bool onGles = false);
34     ~CoreProfileEngine();
35 
36     struct GeometryDrawState {
37         GLuint vshader;
38         GLuint fshader;
39         GLuint program;
40 
41         GLuint vshaderFlat;
42         GLuint fshaderFlat;
43         GLuint programFlat;
44 
45         GLuint ibo;
46         GLuint vao;
47 
48         GLint projMatrixLoc;
49         GLint modelviewMatrixLoc;
50         GLint textureMatrixLoc;
51         GLint modelviewInvTrLoc;
52         GLint textureSamplerLoc;
53         GLint textureCubeSamplerLoc;
54 
55         GLint enableTextureLoc;
56         GLint enableLightingLoc;
57         GLint enableRescaleNormalLoc;
58         GLint enableNormalizeLoc;
59         GLint enableColorMaterialLoc;
60         GLint enableFogLoc;
61         GLint enableReflectionMapLoc;
62 
63         GLint textureEnvModeLoc;
64         GLint textureFormatLoc;
65 
66         GLint materialAmbientLoc;
67         GLint materialDiffuseLoc;
68         GLint materialSpecularLoc;
69         GLint materialEmissiveLoc;
70         GLint materialSpecularExponentLoc;
71 
72         GLint lightModelSceneAmbientLoc;
73         GLint lightModelTwoSidedLoc;
74 
75         GLint lightEnablesLoc;
76         GLint lightAmbientsLoc;
77         GLint lightDiffusesLoc;
78         GLint lightSpecularsLoc;
79         GLint lightPositionsLoc;
80         GLint lightDirectionsLoc;
81         GLint lightSpotlightExponentsLoc;
82         GLint lightSpotlightCutoffAnglesLoc;
83         GLint lightAttenuationConstsLoc;
84         GLint lightAttenuationLinearsLoc;
85         GLint lightAttenuationQuadraticsLoc;
86 
87         GLint fogModeLoc;
88         GLint fogDensityLoc;
89         GLint fogStartLoc;
90         GLint fogEndLoc;
91         GLint fogColorLoc;
92 
93         GLuint posVbo;
94         GLuint normalVbo;
95         GLuint colorVbo;
96         GLuint pointsizeVbo;
97         GLuint texcoordVbo;
98     };
99 
100     struct DrawTexOESCoreState {
101         GLuint vshader;
102         GLuint fshader;
103         GLuint program;
104         GLuint vbo;
105         GLuint ibo;
106         GLuint vao;
107     };
108 
109     const DrawTexOESCoreState& getDrawTexOESCoreState();
110     void teardown();
111     const GeometryDrawState& getGeometryDrawState();
112 
getAndClearLastError()113     GLint getAndClearLastError() {
114         GLint err = mCurrError;
115         mCurrError = 0;
116         return err;
117     }
118 
119     // Utility functions
120     void setupArrayForDraw(GLenum arrayType, GLESpointer* p, GLint first, GLsizei count, bool isIndexed, GLenum indicesType, const GLvoid* indices);
121 
122     void preDrawTextureUnitEmulation();
123     void postDrawTextureUnitEmulation();
124 
125     void preDrawVertexSetup();
126     void postDrawVertexSetup();
127 
128     void setupLighting();
129     void setupFog();
130 
131     // GLES 1 API (deprecated + incompatible with core only)
132 
133     void enable(GLenum cap);
134     void disable(GLenum cap);
135 
136     void shadeModel(GLenum mode);
137 
138     void matrixMode(GLenum mode);
139     void loadIdentity();
140     void loadMatrixf(const GLfloat* m);
141     void pushMatrix();
142     void popMatrix();
143     void multMatrixf(const GLfloat* m);
144 
145     void orthof(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar);
146     void frustumf(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat zNear, GLfloat zFar);
147 
148     void texEnvf(GLenum target, GLenum pname, GLfloat param);
149     void texEnvfv(GLenum target, GLenum pname, const GLfloat* params);
150     void texEnvi(GLenum target, GLenum pname, GLint param);
151     void texEnviv(GLenum target, GLenum pname, const GLint* params);
152     void getTexEnvfv(GLenum env, GLenum pname, GLfloat* params);
153     void getTexEnviv(GLenum env, GLenum pname, GLint* params);
154 
155     void texGenf(GLenum coord, GLenum pname, GLfloat param);
156     void texGenfv(GLenum coord, GLenum pname, const GLfloat* params);
157     void texGeni(GLenum coord, GLenum pname, GLint param);
158     void texGeniv(GLenum coord, GLenum pname, const GLint* params);
159     void getTexGeniv(GLenum coord, GLenum pname, GLint* params);
160     void getTexGenfv(GLenum coord, GLenum pname, GLfloat* params);
161 
162     void enableClientState(GLenum clientState);
163     void disableClientState(GLenum clientState);
164 
165     void drawTexOES(float x, float y, float z, float width, float height);
166 
167     void rotatef(float angle, float x, float y, float z);
168     void scalef(float x, float y, float z);
169     void translatef(float x, float y, float z);
170 
171     void color4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
172     void color4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha);
173 
174     void activeTexture(GLenum unit);
175     void clientActiveTexture(GLenum unit);
176     void drawArrays(GLenum type, GLint first, GLsizei count);
177     void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid* indices);
178 
179 private:
180     GLEScmContext* mCtx = nullptr;
181 
182     GLint mCurrError = 0;
setError(GLint err)183     void setError(GLint err) { mCurrError = err; }
184 
185     size_t sizeOfType(GLenum dataType);
186     GLuint getVboFor(GLenum arrayType);
187 
188     DrawTexOESCoreState m_drawTexOESCoreState = {};
189     GeometryDrawState   m_geometryDrawState = {};
190 
191     // If we are on a gles impl.
192     bool mOnGles = false;
193 
194     static constexpr int kMaxLights = 8;
195     struct LightingBuffer {
196         GLint lightEnables[kMaxLights];
197         GLfloat lightAmbients[4 * kMaxLights];
198         GLfloat lightDiffuses[4 * kMaxLights];
199         GLfloat lightSpeculars[4 * kMaxLights];
200         GLfloat lightPositions[4 * kMaxLights];
201         GLfloat lightDirections[3 * kMaxLights];
202         GLfloat spotlightExponents[kMaxLights];
203         GLfloat spotlightCutoffAngles[kMaxLights];
204         GLfloat attenuationConsts[kMaxLights];
205         GLfloat attenuationLinears[kMaxLights];
206         GLfloat attenuationQuadratics[kMaxLights];
207     };
208 
209     LightingBuffer m_lightingBuffer;
210 };
211