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