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 // GLES1Renderer.h: Defines GLES1 emulation rendering operations on top of a GLES3 8 // context. Used by Context.h. 9 10 #ifndef LIBANGLE_GLES1_RENDERER_H_ 11 #define LIBANGLE_GLES1_RENDERER_H_ 12 13 #include "angle_gl.h" 14 #include "common/angleutils.h" 15 #include "libANGLE/angletypes.h" 16 17 #include <memory> 18 #include <string> 19 #include <unordered_map> 20 21 namespace gl 22 { 23 class Context; 24 class GLES1State; 25 class Program; 26 class State; 27 class Shader; 28 class ShaderProgramManager; 29 30 class GLES1Renderer final : angle::NonCopyable 31 { 32 public: 33 GLES1Renderer(); 34 ~GLES1Renderer(); 35 36 void onDestroy(Context *context, State *state); 37 38 angle::Result prepareForDraw(PrimitiveMode mode, Context *context, State *glState); 39 40 static int VertexArrayIndex(ClientVertexArrayType type, const GLES1State &gles1); 41 static int TexCoordArrayIndex(unsigned int unit); 42 43 void drawTexture(Context *context, 44 State *glState, 45 float x, 46 float y, 47 float z, 48 float width, 49 float height); 50 51 static constexpr int kTexUnitCount = 4; 52 53 private: 54 using Mat4Uniform = float[16]; 55 using Vec4Uniform = float[4]; 56 using Vec3Uniform = float[3]; 57 58 Shader *getShader(ShaderProgramID handle) const; 59 Program *getProgram(ShaderProgramID handle) const; 60 61 angle::Result compileShader(Context *context, 62 ShaderType shaderType, 63 const char *src, 64 ShaderProgramID *shaderOut); 65 angle::Result linkProgram(Context *context, 66 State *glState, 67 ShaderProgramID vshader, 68 ShaderProgramID fshader, 69 const std::unordered_map<GLint, std::string> &attribLocs, 70 ShaderProgramID *programOut); 71 angle::Result initializeRendererProgram(Context *context, State *glState); 72 73 void setUniform1i(Context *context, 74 Program *programObject, 75 UniformLocation location, 76 GLint value); 77 void setUniform1iv(Context *context, 78 Program *programObject, 79 UniformLocation location, 80 GLint count, 81 const GLint *value); 82 void setUniformMatrix4fv(Program *programObject, 83 UniformLocation location, 84 GLint count, 85 GLboolean transpose, 86 const GLfloat *value); 87 void setUniform4fv(Program *programObject, 88 UniformLocation location, 89 GLint count, 90 const GLfloat *value); 91 void setUniform3fv(Program *programObject, 92 UniformLocation location, 93 GLint count, 94 const GLfloat *value); 95 void setUniform2fv(Program *programObject, 96 UniformLocation location, 97 GLint count, 98 const GLfloat *value); 99 void setUniform1f(Program *programObject, UniformLocation location, GLfloat value); 100 void setUniform1fv(Program *programObject, 101 UniformLocation location, 102 GLint count, 103 const GLfloat *value); 104 105 void setAttributesEnabled(Context *context, State *glState, AttributesMask mask); 106 107 static constexpr int kLightCount = 8; 108 static constexpr int kClipPlaneCount = 6; 109 110 static constexpr int kVertexAttribIndex = 0; 111 static constexpr int kNormalAttribIndex = 1; 112 static constexpr int kColorAttribIndex = 2; 113 static constexpr int kPointSizeAttribIndex = 3; 114 static constexpr int kTextureCoordAttribIndexBase = 4; 115 116 bool mRendererProgramInitialized; 117 ShaderProgramManager *mShaderPrograms; 118 119 struct GLES1ProgramState 120 { 121 ShaderProgramID program; 122 123 UniformLocation projMatrixLoc; 124 UniformLocation modelviewMatrixLoc; 125 UniformLocation textureMatrixLoc; 126 UniformLocation modelviewInvTrLoc; 127 128 // Texturing 129 UniformLocation enableTexture2DLoc; 130 UniformLocation enableTextureCubeMapLoc; 131 std::array<UniformLocation, kTexUnitCount> tex2DSamplerLocs; 132 std::array<UniformLocation, kTexUnitCount> texCubeSamplerLocs; 133 134 UniformLocation textureFormatLoc; 135 136 UniformLocation textureEnvModeLoc; 137 UniformLocation combineRgbLoc; 138 UniformLocation combineAlphaLoc; 139 UniformLocation src0rgbLoc; 140 UniformLocation src0alphaLoc; 141 UniformLocation src1rgbLoc; 142 UniformLocation src1alphaLoc; 143 UniformLocation src2rgbLoc; 144 UniformLocation src2alphaLoc; 145 UniformLocation op0rgbLoc; 146 UniformLocation op0alphaLoc; 147 UniformLocation op1rgbLoc; 148 UniformLocation op1alphaLoc; 149 UniformLocation op2rgbLoc; 150 UniformLocation op2alphaLoc; 151 UniformLocation textureEnvColorLoc; 152 UniformLocation rgbScaleLoc; 153 UniformLocation alphaScaleLoc; 154 UniformLocation pointSpriteCoordReplaceLoc; 155 156 // Alpha test 157 UniformLocation enableAlphaTestLoc; 158 UniformLocation alphaFuncLoc; 159 UniformLocation alphaTestRefLoc; 160 161 // Shading, materials, and lighting 162 UniformLocation shadeModelFlatLoc; 163 UniformLocation enableLightingLoc; 164 UniformLocation enableRescaleNormalLoc; 165 UniformLocation enableNormalizeLoc; 166 UniformLocation enableColorMaterialLoc; 167 168 UniformLocation materialAmbientLoc; 169 UniformLocation materialDiffuseLoc; 170 UniformLocation materialSpecularLoc; 171 UniformLocation materialEmissiveLoc; 172 UniformLocation materialSpecularExponentLoc; 173 174 UniformLocation lightModelSceneAmbientLoc; 175 UniformLocation lightModelTwoSidedLoc; 176 177 UniformLocation lightEnablesLoc; 178 UniformLocation lightAmbientsLoc; 179 UniformLocation lightDiffusesLoc; 180 UniformLocation lightSpecularsLoc; 181 UniformLocation lightPositionsLoc; 182 UniformLocation lightDirectionsLoc; 183 UniformLocation lightSpotlightExponentsLoc; 184 UniformLocation lightSpotlightCutoffAnglesLoc; 185 UniformLocation lightAttenuationConstsLoc; 186 UniformLocation lightAttenuationLinearsLoc; 187 UniformLocation lightAttenuationQuadraticsLoc; 188 189 // Fog 190 UniformLocation fogEnableLoc; 191 UniformLocation fogModeLoc; 192 UniformLocation fogDensityLoc; 193 UniformLocation fogStartLoc; 194 UniformLocation fogEndLoc; 195 UniformLocation fogColorLoc; 196 197 // Clip planes 198 UniformLocation enableClipPlanesLoc; 199 UniformLocation clipPlaneEnablesLoc; 200 UniformLocation clipPlanesLoc; 201 202 // Point rasterization 203 UniformLocation pointRasterizationLoc; 204 UniformLocation pointSizeMinLoc; 205 UniformLocation pointSizeMaxLoc; 206 UniformLocation pointDistanceAttenuationLoc; 207 UniformLocation pointSpriteEnabledLoc; 208 209 // Draw texture 210 UniformLocation enableDrawTextureLoc; 211 UniformLocation drawTextureCoordsLoc; 212 UniformLocation drawTextureDimsLoc; 213 UniformLocation drawTextureNormalizedCropRectLoc; 214 }; 215 216 struct GLES1UniformBuffers 217 { 218 std::array<Mat4Uniform, kTexUnitCount> textureMatrices; 219 std::array<GLint, kTexUnitCount> tex2DEnables; 220 std::array<GLint, kTexUnitCount> texCubeEnables; 221 222 std::array<GLint, kTexUnitCount> texEnvModes; 223 std::array<GLint, kTexUnitCount> texCombineRgbs; 224 std::array<GLint, kTexUnitCount> texCombineAlphas; 225 226 std::array<GLint, kTexUnitCount> texCombineSrc0Rgbs; 227 std::array<GLint, kTexUnitCount> texCombineSrc0Alphas; 228 std::array<GLint, kTexUnitCount> texCombineSrc1Rgbs; 229 std::array<GLint, kTexUnitCount> texCombineSrc1Alphas; 230 std::array<GLint, kTexUnitCount> texCombineSrc2Rgbs; 231 std::array<GLint, kTexUnitCount> texCombineSrc2Alphas; 232 std::array<GLint, kTexUnitCount> texCombineOp0Rgbs; 233 std::array<GLint, kTexUnitCount> texCombineOp0Alphas; 234 std::array<GLint, kTexUnitCount> texCombineOp1Rgbs; 235 std::array<GLint, kTexUnitCount> texCombineOp1Alphas; 236 std::array<GLint, kTexUnitCount> texCombineOp2Rgbs; 237 std::array<GLint, kTexUnitCount> texCombineOp2Alphas; 238 std::array<Vec4Uniform, kTexUnitCount> texEnvColors; 239 std::array<GLfloat, kTexUnitCount> texEnvRgbScales; 240 std::array<GLfloat, kTexUnitCount> texEnvAlphaScales; 241 std::array<GLint, kTexUnitCount> pointSpriteCoordReplaces; 242 243 // Lighting 244 std::array<GLint, kLightCount> lightEnables; 245 std::array<Vec4Uniform, kLightCount> lightAmbients; 246 std::array<Vec4Uniform, kLightCount> lightDiffuses; 247 std::array<Vec4Uniform, kLightCount> lightSpeculars; 248 std::array<Vec4Uniform, kLightCount> lightPositions; 249 std::array<Vec3Uniform, kLightCount> lightDirections; 250 std::array<GLfloat, kLightCount> spotlightExponents; 251 std::array<GLfloat, kLightCount> spotlightCutoffAngles; 252 std::array<GLfloat, kLightCount> attenuationConsts; 253 std::array<GLfloat, kLightCount> attenuationLinears; 254 std::array<GLfloat, kLightCount> attenuationQuadratics; 255 256 // Clip planes 257 std::array<GLint, kClipPlaneCount> clipPlaneEnables; 258 std::array<Vec4Uniform, kClipPlaneCount> clipPlanes; 259 260 // Texture crop rectangles 261 std::array<Vec4Uniform, kTexUnitCount> texCropRects; 262 }; 263 264 GLES1UniformBuffers mUniformBuffers; 265 GLES1ProgramState mProgramState; 266 267 bool mDrawTextureEnabled = false; 268 GLfloat mDrawTextureCoords[4] = {0.0f, 0.0f, 0.0f, 0.0f}; 269 GLfloat mDrawTextureDims[2] = {0.0f, 0.0f}; 270 }; 271 272 } // namespace gl 273 274 #endif // LIBANGLE_GLES1_RENDERER_H_ 275