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 "GLES1State.h" 14 #include "angle_gl.h" 15 #include "common/angleutils.h" 16 #include "libANGLE/angletypes.h" 17 18 #include <memory> 19 #include <string> 20 #include <unordered_map> 21 22 namespace gl 23 { 24 class Context; 25 class GLES1State; 26 class Program; 27 class State; 28 class Shader; 29 class ShaderProgramManager; 30 31 enum class GLES1StateEnables : uint64_t 32 { 33 Lighting = 0, 34 Fog = 1, 35 ClipPlanes = 2, 36 DrawTexture = 3, 37 PointRasterization = 4, 38 PointSprite = 5, 39 RescaleNormal = 6, 40 Normalize = 7, 41 AlphaTest = 8, 42 ShadeModelFlat = 9, 43 ColorMaterial = 10, 44 LightModelTwoSided = 11, 45 LogicOpThroughFramebufferFetch = 12, 46 47 InvalidEnum = 13, 48 EnumCount = 13, 49 }; 50 51 constexpr int kClipPlaneCount = 6; 52 constexpr int kTexUnitCount = 4; 53 constexpr int kLightCount = 8; 54 55 using GLES1StateEnabledBitSet = angle::PackedEnumBitSet<GLES1StateEnables, uint64_t>; 56 57 struct GLES1ShaderState 58 { 59 GLES1ShaderState(); 60 ~GLES1ShaderState(); 61 GLES1ShaderState(const GLES1ShaderState &other); 62 63 size_t hash() const; 64 65 GLES1StateEnabledBitSet mGLES1StateEnabled; 66 67 using BoolLightArray = bool[kLightCount]; 68 using BoolTexArray = bool[kTexUnitCount]; 69 using BoolClipPlaneArray = bool[kClipPlaneCount]; 70 using UintTexArray = uint16_t[kTexUnitCount]; 71 72 BoolTexArray tex2DEnables = {false, false, false, false}; 73 BoolTexArray texCubeEnables = {false, false, false, false}; 74 75 UintTexArray tex2DFormats = {GL_RGBA, GL_RGBA, GL_RGBA, GL_RGBA}; 76 77 UintTexArray texEnvModes = {}; 78 UintTexArray texCombineRgbs = {}; 79 UintTexArray texCombineAlphas = {}; 80 UintTexArray texCombineSrc0Rgbs = {}; 81 UintTexArray texCombineSrc0Alphas = {}; 82 UintTexArray texCombineSrc1Rgbs = {}; 83 UintTexArray texCombineSrc1Alphas = {}; 84 UintTexArray texCombineSrc2Rgbs = {}; 85 UintTexArray texCombineSrc2Alphas = {}; 86 UintTexArray texCombineOp0Rgbs = {}; 87 UintTexArray texCombineOp0Alphas = {}; 88 UintTexArray texCombineOp1Rgbs = {}; 89 UintTexArray texCombineOp1Alphas = {}; 90 UintTexArray texCombineOp2Rgbs = {}; 91 UintTexArray texCombineOp2Alphas = {}; 92 93 BoolTexArray pointSpriteCoordReplaces = {}; 94 95 BoolLightArray lightEnables = {}; 96 97 BoolClipPlaneArray clipPlaneEnables = {}; 98 99 AlphaTestFunc alphaTestFunc = {}; 100 101 FogMode fogMode = {}; 102 }; 103 104 bool operator==(const GLES1ShaderState &a, const GLES1ShaderState &b); 105 bool operator!=(const GLES1ShaderState &a, const GLES1ShaderState &b); 106 107 } // namespace gl 108 109 namespace std 110 { 111 template <> 112 struct hash<gl::GLES1ShaderState> 113 { 114 size_t operator()(const gl::GLES1ShaderState &key) const { return key.hash(); } 115 }; 116 } // namespace std 117 118 namespace gl 119 { 120 121 class GLES1Renderer final : angle::NonCopyable 122 { 123 public: 124 GLES1Renderer(); 125 ~GLES1Renderer(); 126 127 void onDestroy(Context *context, State *state); 128 129 angle::Result prepareForDraw(PrimitiveMode mode, 130 Context *context, 131 State *glState, 132 GLES1State *gles1State); 133 134 static int VertexArrayIndex(ClientVertexArrayType type, const GLES1State &gles1); 135 static ClientVertexArrayType VertexArrayType(int attribIndex); 136 static int TexCoordArrayIndex(unsigned int unit); 137 138 void drawTexture(Context *context, 139 State *glState, 140 GLES1State *gles1State, 141 float x, 142 float y, 143 float z, 144 float width, 145 float height); 146 147 private: 148 using Mat4Uniform = float[16]; 149 using Vec4Uniform = float[4]; 150 using Vec3Uniform = float[3]; 151 152 Shader *getShader(ShaderProgramID handle) const; 153 Program *getProgram(ShaderProgramID handle) const; 154 155 angle::Result compileShader(Context *context, 156 ShaderType shaderType, 157 const char *src, 158 ShaderProgramID *shaderOut); 159 angle::Result linkProgram(Context *context, 160 State *glState, 161 ShaderProgramID vshader, 162 ShaderProgramID fshader, 163 const angle::HashMap<GLint, std::string> &attribLocs, 164 ShaderProgramID *programOut); 165 angle::Result initializeRendererProgram(Context *context, 166 State *glState, 167 GLES1State *gles1State); 168 169 void setUniform1i(Context *context, 170 Program *programObject, 171 UniformLocation location, 172 GLint value); 173 void setUniform1ui(Program *programObject, UniformLocation location, GLuint value); 174 void setUniform1iv(Context *context, 175 Program *programObject, 176 UniformLocation location, 177 GLint count, 178 const GLint *value); 179 void setUniformMatrix4fv(Program *programObject, 180 UniformLocation location, 181 GLint count, 182 GLboolean transpose, 183 const GLfloat *value); 184 void setUniform4fv(Program *programObject, 185 UniformLocation location, 186 GLint count, 187 const GLfloat *value); 188 void setUniform3fv(Program *programObject, 189 UniformLocation location, 190 GLint count, 191 const GLfloat *value); 192 void setUniform2fv(Program *programObject, 193 UniformLocation location, 194 GLint count, 195 const GLfloat *value); 196 void setUniform1f(Program *programObject, UniformLocation location, GLfloat value); 197 void setUniform1fv(Program *programObject, 198 UniformLocation location, 199 GLint count, 200 const GLfloat *value); 201 202 void setAttributesEnabled(Context *context, 203 State *glState, 204 GLES1State *gles1State, 205 AttributesMask mask); 206 207 static constexpr int kVertexAttribIndex = 0; 208 static constexpr int kNormalAttribIndex = 1; 209 static constexpr int kColorAttribIndex = 2; 210 static constexpr int kPointSizeAttribIndex = 3; 211 static constexpr int kTextureCoordAttribIndexBase = 4; 212 213 bool mRendererProgramInitialized; 214 ShaderProgramManager *mShaderPrograms; 215 216 GLES1ShaderState mShaderState = {}; 217 218 const char *getShaderBool(GLES1StateEnables state); 219 void addShaderDefine(std::stringstream &outStream, 220 GLES1StateEnables state, 221 const char *enableString); 222 void addShaderUint(std::stringstream &outStream, const char *name, uint16_t value); 223 void addShaderUintTexArray(std::stringstream &outStream, 224 const char *texString, 225 GLES1ShaderState::UintTexArray &texState); 226 void addShaderBoolTexArray(std::stringstream &outStream, 227 const char *texString, 228 GLES1ShaderState::BoolTexArray &texState); 229 void addShaderBoolLightArray(std::stringstream &outStream, 230 const char *name, 231 GLES1ShaderState::BoolLightArray &value); 232 void addShaderBoolClipPlaneArray(std::stringstream &outStream, 233 const char *name, 234 GLES1ShaderState::BoolClipPlaneArray &value); 235 void addVertexShaderDefs(std::stringstream &outStream); 236 void addFragmentShaderDefs(std::stringstream &outStream); 237 238 struct GLES1ProgramState 239 { 240 ShaderProgramID program; 241 242 UniformLocation projMatrixLoc; 243 UniformLocation modelviewMatrixLoc; 244 UniformLocation textureMatrixLoc; 245 UniformLocation modelviewInvTrLoc; 246 247 // Texturing 248 std::array<UniformLocation, kTexUnitCount> tex2DSamplerLocs; 249 std::array<UniformLocation, kTexUnitCount> texCubeSamplerLocs; 250 251 UniformLocation textureEnvColorLoc; 252 UniformLocation rgbScaleLoc; 253 UniformLocation alphaScaleLoc; 254 255 // Alpha test 256 UniformLocation alphaTestRefLoc; 257 258 // Shading, materials, and lighting 259 UniformLocation materialAmbientLoc; 260 UniformLocation materialDiffuseLoc; 261 UniformLocation materialSpecularLoc; 262 UniformLocation materialEmissiveLoc; 263 UniformLocation materialSpecularExponentLoc; 264 265 UniformLocation lightModelSceneAmbientLoc; 266 267 UniformLocation lightAmbientsLoc; 268 UniformLocation lightDiffusesLoc; 269 UniformLocation lightSpecularsLoc; 270 UniformLocation lightPositionsLoc; 271 UniformLocation lightDirectionsLoc; 272 UniformLocation lightSpotlightExponentsLoc; 273 UniformLocation lightSpotlightCutoffAnglesLoc; 274 UniformLocation lightAttenuationConstsLoc; 275 UniformLocation lightAttenuationLinearsLoc; 276 UniformLocation lightAttenuationQuadraticsLoc; 277 278 // Fog 279 UniformLocation fogDensityLoc; 280 UniformLocation fogStartLoc; 281 UniformLocation fogEndLoc; 282 UniformLocation fogColorLoc; 283 284 // Clip planes 285 UniformLocation clipPlanesLoc; 286 287 // Logic op 288 UniformLocation logicOpLoc; 289 290 // Point rasterization 291 UniformLocation pointSizeMinLoc; 292 UniformLocation pointSizeMaxLoc; 293 UniformLocation pointDistanceAttenuationLoc; 294 295 // Draw texture 296 UniformLocation drawTextureCoordsLoc; 297 UniformLocation drawTextureDimsLoc; 298 UniformLocation drawTextureNormalizedCropRectLoc; 299 }; 300 301 struct GLES1UniformBuffers 302 { 303 std::array<Mat4Uniform, kTexUnitCount> textureMatrices; 304 305 std::array<Vec4Uniform, kTexUnitCount> texEnvColors; 306 std::array<GLfloat, kTexUnitCount> texEnvRgbScales; 307 std::array<GLfloat, kTexUnitCount> texEnvAlphaScales; 308 309 // Lighting 310 std::array<Vec4Uniform, kLightCount> lightAmbients; 311 std::array<Vec4Uniform, kLightCount> lightDiffuses; 312 std::array<Vec4Uniform, kLightCount> lightSpeculars; 313 std::array<Vec4Uniform, kLightCount> lightPositions; 314 std::array<Vec3Uniform, kLightCount> lightDirections; 315 std::array<GLfloat, kLightCount> spotlightExponents; 316 std::array<GLfloat, kLightCount> spotlightCutoffAngles; 317 std::array<GLfloat, kLightCount> attenuationConsts; 318 std::array<GLfloat, kLightCount> attenuationLinears; 319 std::array<GLfloat, kLightCount> attenuationQuadratics; 320 321 // Clip planes 322 std::array<Vec4Uniform, kClipPlaneCount> clipPlanes; 323 324 // Texture crop rectangles 325 std::array<Vec4Uniform, kTexUnitCount> texCropRects; 326 }; 327 328 struct GLES1UberShaderState 329 { 330 GLES1UniformBuffers uniformBuffers; 331 GLES1ProgramState programState; 332 }; 333 334 GLES1UberShaderState &getUberShaderState() 335 { 336 ASSERT(mUberShaderState.find(mShaderState) != mUberShaderState.end()); 337 return mUberShaderState[mShaderState]; 338 } 339 340 angle::HashMap<GLES1ShaderState, GLES1UberShaderState> mUberShaderState; 341 342 bool mDrawTextureEnabled = false; 343 GLfloat mDrawTextureCoords[4] = {0.0f, 0.0f, 0.0f, 0.0f}; 344 GLfloat mDrawTextureDims[2] = {0.0f, 0.0f}; 345 }; 346 347 } // namespace gl 348 349 #endif // LIBANGLE_GLES1_RENDERER_H_ 350