1 /* 2 * Copyright (c) 2002-2011 LWJGL Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'LWJGL' nor the names of 17 * its contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package org.lwjgl.opengles; 33 34 import java.nio.Buffer; 35 import java.nio.IntBuffer; 36 37 import static org.lwjgl.opengles.GLES20.*; 38 import static org.lwjgl.opengles.GLES30.*; 39 40 final class StateTracker { 41 42 private static StateTracker tracker = new StateTracker(); 43 44 int elementArrayBuffer; 45 int arrayBuffer; 46 int pixelPackBuffer; 47 int pixelUnpackBuffer; 48 49 Buffer[] glVertexAttribPointer_buffer; 50 51 private final FastIntMap<VAOState> vaoMap = new FastIntMap<VAOState>(); 52 53 int vertexArrayObject; 54 StateTracker()55 StateTracker() { 56 } 57 init()58 void init() { 59 glVertexAttribPointer_buffer = new Buffer[glGetInteger(GL_MAX_VERTEX_ATTRIBS)]; 60 } 61 getTracker()62 static StateTracker getTracker() { 63 return tracker; 64 } 65 bindBuffer(int target, int buffer)66 static void bindBuffer(int target, int buffer) { 67 final StateTracker tracker = getTracker(); 68 69 switch ( target ) { 70 case GL_ARRAY_BUFFER: 71 tracker.arrayBuffer = buffer; 72 break; 73 case GL_ELEMENT_ARRAY_BUFFER: 74 tracker.elementArrayBuffer = buffer; 75 break; 76 case GL_PIXEL_PACK_BUFFER: 77 tracker.pixelPackBuffer = buffer; 78 break; 79 case GL_PIXEL_UNPACK_BUFFER: 80 tracker.pixelUnpackBuffer = buffer; 81 break; 82 } 83 } 84 bindVAO(final int array)85 static void bindVAO(final int array) { 86 final FastIntMap<VAOState> vaoMap = tracker.vaoMap; 87 if ( !vaoMap.containsKey(array) ) 88 vaoMap.put(array, new VAOState()); 89 90 tracker.vertexArrayObject = array; 91 } 92 deleteVAO(final IntBuffer arrays)93 static void deleteVAO(final IntBuffer arrays) { 94 for ( int i = arrays.position(); i < arrays.limit(); i++ ) 95 deleteVAO(arrays.get(i)); 96 } 97 deleteVAO(final int array)98 static void deleteVAO(final int array) { 99 tracker.vaoMap.remove(array); 100 101 if ( tracker.vertexArrayObject == array ) 102 tracker.vertexArrayObject = 0; 103 } 104 105 /** 106 * Simple class to help us track VAO state. Currently 107 * only ELEMENT_ARRAY_BUFFER_BINDING is tracked, since 108 * that's the only state we check from tables 6.6-6.9. 109 */ 110 private static class VAOState { 111 112 int elementArrayBuffer; 113 114 } 115 116 }