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