1 /* 2 * Copyright (C) 2011 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 package com.android.ide.eclipse.gltrace.state; 18 19 import com.android.ide.eclipse.gltrace.GLEnum; 20 import com.android.ide.eclipse.gltrace.state.GLIntegerProperty.DisplayRadix; 21 22 import java.util.Collections; 23 24 public class GLState { 25 /** # of texture units modelled in the GL State. */ 26 public static final int TEXTURE_UNIT_COUNT = 16; 27 28 /** # of vertex attributes */ 29 private static final int MAX_VERTEX_ATTRIBS = 8; 30 31 private static GLState sGLState = new GLState(); 32 33 private IGLProperty createBufferBindings() { 34 IGLProperty array, eArray, vArray; 35 36 array = new GLIntegerProperty(GLStateType.ARRAY_BUFFER_BINDING, 0); 37 eArray = new GLIntegerProperty(GLStateType.ELEMENT_ARRAY_BUFFER_BINDING, 0); 38 39 vArray = new GLIntegerProperty( 40 GLStateType.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING_PER_INDEX, 0); 41 IGLProperty vArray8 = new GLListProperty(GLStateType.VERTEX_ATTRIB_ARRAY_BUFFER_BINDINGS, 42 vArray, MAX_VERTEX_ATTRIBS); 43 44 return new GLCompositeProperty( 45 GLStateType.BUFFER_BINDINGS, 46 array, 47 eArray, 48 vArray8); 49 } 50 51 private IGLProperty createVertexAttribArrays() { 52 IGLProperty enabled, size, stride, type, normalized, pointer; 53 54 enabled = new GLBooleanProperty(GLStateType.VERTEX_ATTRIB_ARRAY_ENABLED, false); 55 size = new GLIntegerProperty(GLStateType.VERTEX_ATTRIB_ARRAY_SIZE, 4); 56 stride = new GLIntegerProperty(GLStateType.VERTEX_ATTRIB_ARRAY_STRIDE, 0); 57 type = new GLEnumProperty(GLStateType.VERTEX_ATTRIB_ARRAY_TYPE, GLEnum.GL_FLOAT); 58 normalized = new GLBooleanProperty(GLStateType.VERTEX_ATTRIB_ARRAY_NORMALIZED, false); 59 pointer = new GLIntegerProperty(GLStateType.VERTEX_ATTRIB_ARRAY_POINTER, 0); 60 61 IGLProperty perVertexAttribArrayState = new GLCompositeProperty( 62 GLStateType.VERTEX_ATTRIB_ARRAY_COMPOSITE, 63 enabled, 64 size, 65 stride, 66 type, 67 normalized, 68 pointer); 69 70 return new GLListProperty( 71 GLStateType.VERTEX_ATTRIB_ARRAY, 72 perVertexAttribArrayState, 73 MAX_VERTEX_ATTRIBS); 74 } 75 76 private IGLProperty createGenericVertexAttributeState() { 77 IGLProperty v0 = new GLFloatProperty(GLStateType.GENERIC_VERTEX_ATTRIB_V0, 78 Float.valueOf(0)); 79 IGLProperty v1 = new GLFloatProperty(GLStateType.GENERIC_VERTEX_ATTRIB_V1, 80 Float.valueOf(0)); 81 IGLProperty v2 = new GLFloatProperty(GLStateType.GENERIC_VERTEX_ATTRIB_V2, 82 Float.valueOf(0)); 83 IGLProperty v3 = new GLFloatProperty(GLStateType.GENERIC_VERTEX_ATTRIB_V3, 84 Float.valueOf(0)); 85 86 IGLProperty perGenericVertexAttribState = new GLCompositeProperty( 87 GLStateType.GENERIC_VERTEX_ATTRIBUTE_DATA_COMPOSITE, 88 v0, v1, v2, v3); 89 90 return new GLListProperty( 91 GLStateType.GENERIC_VERTEX_ATTRIBUTES, 92 perGenericVertexAttribState, 93 MAX_VERTEX_ATTRIBS); 94 } 95 96 private IGLProperty createVboState() { 97 IGLProperty size = new GLIntegerProperty(GLStateType.BUFFER_SIZE, Integer.valueOf(0)); 98 IGLProperty usage = new GLEnumProperty(GLStateType.BUFFER_USAGE, GLEnum.GL_STATIC_DRAW); 99 IGLProperty data = new GLObjectProperty(GLStateType.BUFFER_DATA, new byte[0]); 100 IGLProperty type = new GLEnumProperty(GLStateType.BUFFER_TYPE, GLEnum.GL_ARRAY_BUFFER); 101 102 IGLProperty perVboState = new GLCompositeProperty(GLStateType.VBO_COMPOSITE, 103 size, usage, data, type); 104 105 return new GLSparseArrayProperty(GLStateType.VBO, perVboState); 106 } 107 108 private IGLProperty createVertexArrayData() { 109 IGLProperty vertexAttribArrays = createVertexAttribArrays(); 110 IGLProperty bufferBindings = createBufferBindings(); 111 IGLProperty genericAttribs = createGenericVertexAttributeState(); 112 IGLProperty vboState = createVboState(); 113 114 return new GLCompositeProperty(GLStateType.VERTEX_ARRAY_DATA, 115 genericAttribs, 116 vertexAttribArrays, 117 bufferBindings, 118 vboState); 119 } 120 121 private IGLProperty createTransformationState() { 122 IGLProperty viewPortX = new GLIntegerProperty(GLStateType.VIEWPORT_X, 0); 123 IGLProperty viewPortY = new GLIntegerProperty(GLStateType.VIEWPORT_Y, 0); 124 IGLProperty viewPortW = new GLIntegerProperty(GLStateType.VIEWPORT_WIDTH, 0); 125 IGLProperty viewPortH = new GLIntegerProperty(GLStateType.VIEWPORT_HEIGHT, 0); 126 IGLProperty viewPort = new GLCompositeProperty(GLStateType.VIEWPORT, 127 viewPortX, viewPortY, viewPortW, viewPortH); 128 129 IGLProperty clampNear = new GLFloatProperty(GLStateType.DEPTH_RANGE_NEAR, 130 Float.valueOf(0.0f)); 131 IGLProperty clampFar = new GLFloatProperty(GLStateType.DEPTH_RANGE_FAR, 132 Float.valueOf(1.0f)); 133 IGLProperty depthRange = new GLCompositeProperty(GLStateType.DEPTH_RANGE, 134 clampNear, 135 clampFar); 136 137 IGLProperty transformationState = new GLCompositeProperty(GLStateType.TRANSFORMATION_STATE, 138 viewPort, 139 depthRange); 140 return transformationState; 141 } 142 143 private IGLProperty createRasterizationState() { 144 IGLProperty lineWidth = new GLFloatProperty(GLStateType.LINE_WIDTH, Float.valueOf(1.0f)); 145 IGLProperty cullFace = new GLBooleanProperty(GLStateType.CULL_FACE, Boolean.FALSE); 146 IGLProperty cullFaceMode = new GLEnumProperty(GLStateType.CULL_FACE_MODE, GLEnum.GL_BACK); 147 IGLProperty frontFace = new GLEnumProperty(GLStateType.FRONT_FACE, GLEnum.GL_CCW); 148 IGLProperty polyOffsetFactor = new GLFloatProperty(GLStateType.POLYGON_OFFSET_FACTOR, 149 Float.valueOf(0f)); 150 IGLProperty polyOffsetUnits = new GLFloatProperty(GLStateType.POLYGON_OFFSET_UNITS, 151 Float.valueOf(0f)); 152 IGLProperty polyOffsetFill = new GLBooleanProperty(GLStateType.POLYGON_OFFSET_FILL, 153 Boolean.FALSE); 154 155 return new GLCompositeProperty(GLStateType.RASTERIZATION_STATE, 156 lineWidth, 157 cullFace, 158 cullFaceMode, 159 frontFace, 160 polyOffsetFactor, 161 polyOffsetUnits, 162 polyOffsetFill); 163 } 164 165 private IGLProperty createPixelOperationsState() { 166 IGLProperty scissorTest = new GLBooleanProperty(GLStateType.SCISSOR_TEST, Boolean.FALSE); 167 IGLProperty scissorBoxX = new GLIntegerProperty(GLStateType.SCISSOR_BOX_X, 0); 168 IGLProperty scissorBoxY = new GLIntegerProperty(GLStateType.SCISSOR_BOX_Y, 0); 169 IGLProperty scissorBoxW = new GLIntegerProperty(GLStateType.SCISSOR_BOX_WIDTH, 0); 170 IGLProperty scissorBoxH = new GLIntegerProperty(GLStateType.SCISSOR_BOX_HEIGHT, 0); 171 IGLProperty scissorBox = new GLCompositeProperty(GLStateType.SCISSOR_BOX, 172 scissorBoxX, scissorBoxY, scissorBoxW, scissorBoxH); 173 174 IGLProperty stencilTest = new GLBooleanProperty(GLStateType.STENCIL_TEST, Boolean.FALSE); 175 IGLProperty stencilFunc = new GLEnumProperty(GLStateType.STENCIL_FUNC, GLEnum.GL_ALWAYS); 176 IGLProperty stencilMask = new GLIntegerProperty(GLStateType.STENCIL_VALUE_MASK, 177 Integer.valueOf(0xffffffff), DisplayRadix.HEX); 178 IGLProperty stencilRef = new GLIntegerProperty(GLStateType.STENCIL_REF, 179 Integer.valueOf(0)); 180 IGLProperty stencilFail = new GLEnumProperty(GLStateType.STENCIL_FAIL, GLEnum.GL_KEEP); 181 IGLProperty stencilPassDepthFail = new GLEnumProperty(GLStateType.STENCIL_PASS_DEPTH_FAIL, 182 GLEnum.GL_KEEP); 183 IGLProperty stencilPassDepthPass = new GLEnumProperty(GLStateType.STENCIL_PASS_DEPTH_PASS, 184 GLEnum.GL_KEEP); 185 IGLProperty stencilBackFunc = new GLEnumProperty(GLStateType.STENCIL_BACK_FUNC, 186 GLEnum.GL_ALWAYS); 187 IGLProperty stencilBackValueMask = new GLIntegerProperty( 188 GLStateType.STENCIL_BACK_VALUE_MASK, Integer.valueOf(0xffffffff), DisplayRadix.HEX); 189 IGLProperty stencilBackRef = new GLIntegerProperty(GLStateType.STENCIL_BACK_REF, 0); 190 IGLProperty stencilBackFail = new GLEnumProperty(GLStateType.STENCIL_BACK_FAIL, 191 GLEnum.GL_KEEP); 192 IGLProperty stencilBackPassDepthFail = new GLEnumProperty( 193 GLStateType.STENCIL_BACK_PASS_DEPTH_FAIL, GLEnum.GL_KEEP); 194 IGLProperty stencilBackPassDepthPass = new GLEnumProperty( 195 GLStateType.STENCIL_BACK_PASS_DEPTH_PASS, GLEnum.GL_KEEP); 196 IGLProperty stencil = new GLCompositeProperty(GLStateType.STENCIL, 197 stencilTest, stencilFunc, 198 stencilMask, stencilRef, stencilFail, 199 stencilPassDepthFail, stencilPassDepthPass, 200 stencilBackFunc, stencilBackValueMask, 201 stencilBackRef, stencilBackFail, 202 stencilBackPassDepthFail, stencilBackPassDepthPass); 203 204 IGLProperty depthTest = new GLBooleanProperty(GLStateType.DEPTH_TEST, Boolean.FALSE); 205 IGLProperty depthFunc = new GLEnumProperty(GLStateType.DEPTH_FUNC, GLEnum.GL_LESS); 206 207 IGLProperty blendEnabled = new GLBooleanProperty(GLStateType.BLEND_ENABLED, Boolean.FALSE); 208 // FIXME: BLEND_SRC_RGB should be set to GL_ONE, but GL_LINES is already 0x1. 209 IGLProperty blendSrcRgb = new GLEnumProperty(GLStateType.BLEND_SRC_RGB, GLEnum.GL_LINES); 210 IGLProperty blendSrcAlpha = new GLEnumProperty(GLStateType.BLEND_SRC_ALPHA, 211 GLEnum.GL_LINES); 212 IGLProperty blendDstRgb = new GLEnumProperty(GLStateType.BLEND_DST_RGB, GLEnum.GL_NONE); 213 IGLProperty blendDstAlpha = new GLEnumProperty(GLStateType.BLEND_DST_ALPHA, 214 GLEnum.GL_NONE); 215 IGLProperty blendEquationRgb = new GLEnumProperty(GLStateType.BLEND_EQUATION_RGB, 216 GLEnum.GL_FUNC_ADD); 217 IGLProperty blendEquationAlpha = new GLEnumProperty(GLStateType.BLEND_EQUATION_ALPHA, 218 GLEnum.GL_FUNC_ADD); 219 IGLProperty blend = new GLCompositeProperty(GLStateType.BLEND, 220 blendEnabled, blendSrcRgb, blendSrcAlpha, blendDstRgb, blendDstAlpha, 221 blendEquationRgb, blendEquationAlpha); 222 223 IGLProperty dither = new GLBooleanProperty(GLStateType.DITHER, Boolean.TRUE); 224 225 return new GLCompositeProperty(GLStateType.PIXEL_OPERATIONS, 226 scissorTest, scissorBox, stencil, 227 depthTest, depthFunc, blend, dither); 228 } 229 230 private IGLProperty createPixelPackState() { 231 IGLProperty packAlignment = new GLIntegerProperty(GLStateType.PACK_ALIGNMENT, 232 Integer.valueOf(4)); 233 IGLProperty unpackAlignment = new GLIntegerProperty(GLStateType.UNPACK_ALIGNMENT, 234 Integer.valueOf(4)); 235 IGLProperty pixelPack = new GLCompositeProperty(GLStateType.PIXEL_PACKING, 236 packAlignment, unpackAlignment); 237 return pixelPack; 238 } 239 240 private IGLProperty createFramebufferState() { 241 IGLProperty binding = new GLIntegerProperty(GLStateType.FRAMEBUFFER_BINDING, 0); 242 GLCompositeProperty framebufferState = new GLCompositeProperty( 243 GLStateType.FRAMEBUFFER_STATE, 244 binding); 245 return framebufferState; 246 } 247 248 private IGLProperty createTextureState() { 249 IGLProperty activeTexture = new GLIntegerProperty(GLStateType.ACTIVE_TEXTURE_UNIT, 250 Integer.valueOf(0)); 251 252 IGLProperty binding2D = new GLIntegerProperty(GLStateType.TEXTURE_BINDING_2D, 253 Integer.valueOf(0)); 254 IGLProperty bindingCubeMap = new GLIntegerProperty(GLStateType.TEXTURE_BINDING_CUBE_MAP, 255 Integer.valueOf(0)); 256 IGLProperty bindingExternal = new GLIntegerProperty(GLStateType.TEXTURE_BINDING_EXTERNAL, 257 Integer.valueOf(0)); 258 IGLProperty perTextureUnitState = new GLCompositeProperty( 259 GLStateType.PER_TEXTURE_UNIT_STATE, binding2D, bindingCubeMap, bindingExternal); 260 IGLProperty textureUnitState = new GLListProperty(GLStateType.TEXTURE_UNITS, 261 perTextureUnitState, TEXTURE_UNIT_COUNT); 262 263 IGLProperty swizzleR = new GLEnumProperty(GLStateType.TEXTURE_SWIZZLE_R, GLEnum.GL_RED); 264 IGLProperty swizzleG = new GLEnumProperty(GLStateType.TEXTURE_SWIZZLE_G, GLEnum.GL_GREEN); 265 IGLProperty swizzleB = new GLEnumProperty(GLStateType.TEXTURE_SWIZZLE_B, GLEnum.GL_BLUE); 266 IGLProperty swizzleA = new GLEnumProperty(GLStateType.TEXTURE_SWIZZLE_A, GLEnum.GL_ALPHA); 267 IGLProperty minFilter = new GLEnumProperty(GLStateType.TEXTURE_MIN_FILTER, 268 GLEnum.GL_NEAREST); 269 IGLProperty magFilter = new GLEnumProperty(GLStateType.TEXTURE_MAG_FILTER, 270 GLEnum.GL_NEAREST); 271 IGLProperty wrapS = new GLEnumProperty(GLStateType.TEXTURE_WRAP_S, GLEnum.GL_REPEAT); 272 IGLProperty wrapT = new GLEnumProperty(GLStateType.TEXTURE_WRAP_T, GLEnum.GL_REPEAT); 273 IGLProperty wrapR = new GLEnumProperty(GLStateType.TEXTURE_WRAP_R, GLEnum.GL_REPEAT); 274 IGLProperty minLod = new GLFloatProperty(GLStateType.TEXTURE_MIN_LOD, Float.valueOf(-1000)); 275 IGLProperty maxLod = new GLFloatProperty(GLStateType.TEXTURE_MAX_LOD, Float.valueOf(1000)); 276 IGLProperty baseLevel = new GLIntegerProperty(GLStateType.TEXTURE_BASE_LEVEL, 0); 277 IGLProperty maxLevel = new GLIntegerProperty(GLStateType.TEXTURE_MAX_LEVEL, 1000); 278 IGLProperty cmpMode = new GLEnumProperty(GLStateType.TEXTURE_COMPARE_MODE, GLEnum.GL_NONE); 279 IGLProperty cmpFunc = new GLEnumProperty(GLStateType.TEXTURE_COMPARE_FUNC, 280 GLEnum.GL_LEQUAL); 281 IGLProperty immutableFormat = new GLBooleanProperty(GLStateType.TEXTURE_IMMUTABLE_FORMAT, 282 Boolean.FALSE); 283 IGLProperty immutableLevels = new GLIntegerProperty(GLStateType.TEXTURE_IMMUTABLE_LEVELS, 284 0); 285 286 IGLProperty width = new GLIntegerProperty(GLStateType.TEXTURE_WIDTH, Integer.valueOf(-1)); 287 IGLProperty height = new GLIntegerProperty(GLStateType.TEXTURE_HEIGHT, 288 Integer.valueOf(-1)); 289 IGLProperty format = new GLEnumProperty(GLStateType.TEXTURE_FORMAT, 290 GLEnum.GL_INVALID_VALUE); 291 IGLProperty imageType = new GLEnumProperty(GLStateType.TEXTURE_IMAGE_TYPE, 292 GLEnum.GL_UNSIGNED_BYTE); 293 IGLProperty image = new GLStringProperty(GLStateType.TEXTURE_IMAGE, null); 294 295 IGLProperty perTextureLevelState = new GLCompositeProperty( 296 GLStateType.PER_TEXTURE_LEVEL_STATE, 297 width, height, format, imageType, image); 298 IGLProperty mipmapState = new GLSparseArrayProperty(GLStateType.TEXTURE_MIPMAPS, 299 perTextureLevelState, true); 300 301 IGLProperty textureDefaultState = new GLCompositeProperty(GLStateType.PER_TEXTURE_STATE, 302 swizzleR, swizzleG, swizzleB, swizzleA, 303 minFilter, magFilter, 304 wrapS, wrapT, wrapR, 305 minLod, maxLod, 306 baseLevel, maxLevel, 307 cmpMode, cmpFunc, 308 immutableFormat, immutableLevels, 309 mipmapState); 310 GLSparseArrayProperty textures = new GLSparseArrayProperty(GLStateType.TEXTURES, 311 textureDefaultState); 312 textures.add(0); 313 314 return new GLCompositeProperty(GLStateType.TEXTURE_STATE, 315 activeTexture, 316 textureUnitState, 317 textures); 318 } 319 320 private IGLProperty createProgramState() { 321 IGLProperty currentProgram = new GLIntegerProperty(GLStateType.CURRENT_PROGRAM, 322 Integer.valueOf(0)); 323 324 IGLProperty attachedShaderId = new GLIntegerProperty(GLStateType.ATTACHED_SHADER_ID, 325 Integer.valueOf(0)); 326 IGLProperty attachedShaders = new GLSparseArrayProperty(GLStateType.ATTACHED_SHADERS, 327 attachedShaderId); 328 329 IGLProperty attributeName = new GLStringProperty(GLStateType.ATTRIBUTE_NAME, ""); 330 IGLProperty attributeType = new GLEnumProperty(GLStateType.ATTRIBUTE_TYPE, 331 GLEnum.GL_FLOAT_MAT4); 332 IGLProperty attributeSize = new GLIntegerProperty(GLStateType.ATTRIBUTE_SIZE, 333 Integer.valueOf(1)); 334 IGLProperty attributeValue = new GLObjectProperty(GLStateType.ATTRIBUTE_VALUE, 335 Collections.emptyList()); 336 IGLProperty perAttributeProperty = new GLCompositeProperty(GLStateType.PER_ATTRIBUTE_STATE, 337 attributeName, attributeType, attributeSize, attributeValue); 338 IGLProperty attributes = new GLSparseArrayProperty(GLStateType.ACTIVE_ATTRIBUTES, 339 perAttributeProperty); 340 341 IGLProperty uniformName = new GLStringProperty(GLStateType.UNIFORM_NAME, ""); 342 IGLProperty uniformType = new GLEnumProperty(GLStateType.UNIFORM_TYPE, 343 GLEnum.GL_FLOAT_MAT4); 344 IGLProperty uniformSize = new GLIntegerProperty(GLStateType.UNIFORM_SIZE, 345 Integer.valueOf(1)); 346 IGLProperty uniformValue = new GLObjectProperty(GLStateType.UNIFORM_VALUE, 347 Collections.emptyList()); 348 IGLProperty perUniformProperty = new GLCompositeProperty(GLStateType.PER_UNIFORM_STATE, 349 uniformName, uniformType, uniformSize, uniformValue); 350 IGLProperty uniforms = new GLSparseArrayProperty(GLStateType.ACTIVE_UNIFORMS, 351 perUniformProperty); 352 353 IGLProperty perProgramState = new GLCompositeProperty(GLStateType.PER_PROGRAM_STATE, 354 attachedShaders, attributes, uniforms); 355 356 IGLProperty programs = new GLSparseArrayProperty(GLStateType.PROGRAMS, perProgramState); 357 358 return new GLCompositeProperty(GLStateType.PROGRAM_STATE, 359 currentProgram, programs); 360 } 361 362 private IGLProperty createShaderState() { 363 IGLProperty shaderType = new GLEnumProperty(GLStateType.SHADER_TYPE, 364 GLEnum.GL_VERTEX_SHADER); 365 IGLProperty shaderSource = new GLStringProperty(GLStateType.SHADER_SOURCE, 366 ""); //$NON-NLS-1$ 367 IGLProperty perShaderState = new GLCompositeProperty(GLStateType.PER_SHADER_STATE, 368 shaderType, shaderSource); 369 return new GLSparseArrayProperty(GLStateType.SHADERS, perShaderState); 370 } 371 372 public static IGLProperty createDefaultES2State() { 373 GLCompositeProperty glState = new GLCompositeProperty(GLStateType.GL_STATE_ES2, 374 sGLState.createVertexArrayData(), 375 sGLState.createFramebufferState(), 376 sGLState.createTransformationState(), 377 sGLState.createRasterizationState(), 378 sGLState.createPixelOperationsState(), 379 sGLState.createPixelPackState(), 380 sGLState.createTextureState(), 381 sGLState.createProgramState(), 382 sGLState.createShaderState()); 383 return glState; 384 } 385 386 public static IGLProperty createDefaultES1State() { 387 GLCompositeProperty glState = new GLCompositeProperty(GLStateType.GL_STATE_ES1, 388 sGLState.createVertexArrayData(), 389 sGLState.createFramebufferState(), 390 sGLState.createTransformationState(), 391 sGLState.createRasterizationState(), 392 sGLState.createPixelOperationsState(), 393 sGLState.createPixelPackState(), 394 sGLState.createTextureState()); 395 return glState; 396 } 397 398 public static IGLProperty createDefaultState() { 399 return new GLListProperty(GLStateType.GL_STATE, null, 0); 400 } 401 } 402