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