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_POINTS); 213 IGLProperty blendDstAlpha = new GLEnumProperty(GLStateType.BLEND_DST_ALPHA, 214 GLEnum.GL_POINTS); 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 minFilter = new GLEnumProperty(GLStateType.TEXTURE_MIN_FILTER, 264 GLEnum.GL_NEAREST); 265 IGLProperty magFilter = new GLEnumProperty(GLStateType.TEXTURE_MAG_FILTER, 266 GLEnum.GL_NEAREST); 267 IGLProperty wrapS = new GLEnumProperty(GLStateType.TEXTURE_WRAP_S, GLEnum.GL_REPEAT); 268 IGLProperty wrapT = new GLEnumProperty(GLStateType.TEXTURE_WRAP_T, GLEnum.GL_REPEAT); 269 270 IGLProperty width = new GLIntegerProperty(GLStateType.TEXTURE_WIDTH, Integer.valueOf(-1)); 271 IGLProperty height = new GLIntegerProperty(GLStateType.TEXTURE_HEIGHT, 272 Integer.valueOf(-1)); 273 IGLProperty format = new GLEnumProperty(GLStateType.TEXTURE_FORMAT, 274 GLEnum.GL_INVALID_VALUE); 275 IGLProperty imageType = new GLEnumProperty(GLStateType.TEXTURE_IMAGE_TYPE, 276 GLEnum.GL_UNSIGNED_BYTE); 277 IGLProperty image = new GLStringProperty(GLStateType.TEXTURE_IMAGE, null); 278 279 IGLProperty perTextureLevelState = new GLCompositeProperty( 280 GLStateType.PER_TEXTURE_LEVEL_STATE, 281 width, height, format, imageType, image); 282 IGLProperty mipmapState = new GLSparseArrayProperty(GLStateType.TEXTURE_MIPMAPS, 283 perTextureLevelState, true); 284 285 IGLProperty textureDefaultState = new GLCompositeProperty(GLStateType.PER_TEXTURE_STATE, 286 minFilter, magFilter, wrapS, wrapT, mipmapState); 287 GLSparseArrayProperty textures = new GLSparseArrayProperty(GLStateType.TEXTURES, 288 textureDefaultState); 289 textures.add(0); 290 291 return new GLCompositeProperty(GLStateType.TEXTURE_STATE, 292 activeTexture, 293 textureUnitState, 294 textures); 295 } 296 297 private IGLProperty createProgramState() { 298 IGLProperty currentProgram = new GLIntegerProperty(GLStateType.CURRENT_PROGRAM, 299 Integer.valueOf(0)); 300 301 IGLProperty attachedShaderId = new GLIntegerProperty(GLStateType.ATTACHED_SHADER_ID, 302 Integer.valueOf(0)); 303 IGLProperty attachedShaders = new GLSparseArrayProperty(GLStateType.ATTACHED_SHADERS, 304 attachedShaderId); 305 306 IGLProperty attributeName = new GLStringProperty(GLStateType.ATTRIBUTE_NAME, ""); 307 IGLProperty attributeType = new GLEnumProperty(GLStateType.ATTRIBUTE_TYPE, 308 GLEnum.GL_FLOAT_MAT4); 309 IGLProperty attributeSize = new GLIntegerProperty(GLStateType.ATTRIBUTE_SIZE, 310 Integer.valueOf(1)); 311 IGLProperty attributeValue = new GLObjectProperty(GLStateType.ATTRIBUTE_VALUE, 312 Collections.emptyList()); 313 IGLProperty perAttributeProperty = new GLCompositeProperty(GLStateType.PER_ATTRIBUTE_STATE, 314 attributeName, attributeType, attributeSize, attributeValue); 315 IGLProperty attributes = new GLSparseArrayProperty(GLStateType.ACTIVE_ATTRIBUTES, 316 perAttributeProperty); 317 318 IGLProperty uniformName = new GLStringProperty(GLStateType.UNIFORM_NAME, ""); 319 IGLProperty uniformType = new GLEnumProperty(GLStateType.UNIFORM_TYPE, 320 GLEnum.GL_FLOAT_MAT4); 321 IGLProperty uniformSize = new GLIntegerProperty(GLStateType.UNIFORM_SIZE, 322 Integer.valueOf(1)); 323 IGLProperty uniformValue = new GLObjectProperty(GLStateType.UNIFORM_VALUE, 324 Collections.emptyList()); 325 IGLProperty perUniformProperty = new GLCompositeProperty(GLStateType.PER_UNIFORM_STATE, 326 uniformName, uniformType, uniformSize, uniformValue); 327 IGLProperty uniforms = new GLSparseArrayProperty(GLStateType.ACTIVE_UNIFORMS, 328 perUniformProperty); 329 330 IGLProperty perProgramState = new GLCompositeProperty(GLStateType.PER_PROGRAM_STATE, 331 attachedShaders, attributes, uniforms); 332 333 IGLProperty programs = new GLSparseArrayProperty(GLStateType.PROGRAMS, perProgramState); 334 335 return new GLCompositeProperty(GLStateType.PROGRAM_STATE, 336 currentProgram, programs); 337 } 338 339 private IGLProperty createShaderState() { 340 IGLProperty shaderType = new GLEnumProperty(GLStateType.SHADER_TYPE, 341 GLEnum.GL_VERTEX_SHADER); 342 IGLProperty shaderSource = new GLStringProperty(GLStateType.SHADER_SOURCE, 343 ""); //$NON-NLS-1$ 344 IGLProperty perShaderState = new GLCompositeProperty(GLStateType.PER_SHADER_STATE, 345 shaderType, shaderSource); 346 return new GLSparseArrayProperty(GLStateType.SHADERS, perShaderState); 347 } 348 349 public static IGLProperty createDefaultES2State() { 350 GLCompositeProperty glState = new GLCompositeProperty(GLStateType.GL_STATE_ES2, 351 sGLState.createVertexArrayData(), 352 sGLState.createFramebufferState(), 353 sGLState.createTransformationState(), 354 sGLState.createRasterizationState(), 355 sGLState.createPixelOperationsState(), 356 sGLState.createPixelPackState(), 357 sGLState.createTextureState(), 358 sGLState.createProgramState(), 359 sGLState.createShaderState()); 360 return glState; 361 } 362 363 public static IGLProperty createDefaultES1State() { 364 GLCompositeProperty glState = new GLCompositeProperty(GLStateType.GL_STATE_ES1, 365 sGLState.createVertexArrayData(), 366 sGLState.createFramebufferState(), 367 sGLState.createTransformationState(), 368 sGLState.createRasterizationState(), 369 sGLState.createPixelOperationsState(), 370 sGLState.createPixelPackState(), 371 sGLState.createTextureState()); 372 return glState; 373 } 374 375 public static IGLProperty createDefaultState() { 376 return new GLListProperty(GLStateType.GL_STATE, null, 0); 377 } 378 } 379