diff options
author | Sven Gothel <[email protected]> | 2014-02-23 14:51:06 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-02-23 14:51:06 +0100 |
commit | 3352601e0860584509adf2b76f993d03893ded4b (patch) | |
tree | 974fccc8c0eb2f5ad9d4ffd741dfc35869ed67b5 /src/jogl/classes/com/jogamp/opengl/util/GLArrayDataClient.java | |
parent | f51933f0ebe9ae030c26c066e59a728ce08b8559 (diff) | |
parent | c67de337a8aaf52e36104c3f13e273aa19d21f1f (diff) |
Merge branch 'master' into stash_glyphcache
Conflicts:
make/scripts/tests.sh
src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java
src/jogl/classes/com/jogamp/graph/curve/Region.java
src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java
src/jogl/classes/com/jogamp/graph/curve/opengl/RegionRenderer.java
src/jogl/classes/com/jogamp/graph/curve/opengl/Renderer.java
src/jogl/classes/com/jogamp/graph/curve/opengl/TextRenderer.java
src/jogl/classes/com/jogamp/graph/font/Font.java
src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java
src/jogl/classes/jogamp/graph/curve/text/GlyphShape.java
src/jogl/classes/jogamp/graph/curve/text/GlyphString.java
src/jogl/classes/jogamp/graph/font/typecast/TypecastFont.java
src/jogl/classes/jogamp/graph/font/typecast/TypecastGlyph.java
src/jogl/classes/jogamp/graph/font/typecast/TypecastRenderer.java
Diffstat (limited to 'src/jogl/classes/com/jogamp/opengl/util/GLArrayDataClient.java')
-rw-r--r-- | src/jogl/classes/com/jogamp/opengl/util/GLArrayDataClient.java | 262 |
1 files changed, 183 insertions, 79 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/util/GLArrayDataClient.java b/src/jogl/classes/com/jogamp/opengl/util/GLArrayDataClient.java index 134dd9677..a58eb82cd 100644 --- a/src/jogl/classes/com/jogamp/opengl/util/GLArrayDataClient.java +++ b/src/jogl/classes/com/jogamp/opengl/util/GLArrayDataClient.java @@ -1,6 +1,34 @@ +/** + * Copyright 2010 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ package com.jogamp.opengl.util; +import java.lang.reflect.Constructor; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.FloatBuffer; @@ -26,13 +54,13 @@ public class GLArrayDataClient extends GLArrayDataWrapper implements GLArrayData * and starting with a new created Buffer object with initialElementCount size * * On profiles GL2 and ES1 the fixed function pipeline behavior is as expected. - * On profile ES2 the fixed function emulation will transform these calls to + * On profile ES2 the fixed function emulation will transform these calls to * EnableVertexAttribArray and VertexAttribPointer calls, * and a predefined vertex attribute variable name will be chosen. - * - * The default name mapping will be used, + * + * The default name mapping will be used, * see {@link GLPointerFuncUtil#getPredefinedArrayIndexName(int)}. - * + * * @param index The GL array index * @param comps The array component number * @param dataType The array index GL data type @@ -40,13 +68,13 @@ public class GLArrayDataClient extends GLArrayDataWrapper implements GLArrayData * @param initialElementCount * * @see javax.media.opengl.GLContext#getPredefinedArrayIndexName(int) - */ + */ public static GLArrayDataClient createFixed(int index, int comps, int dataType, boolean normalized, int initialElementCount) throws GLException { GLArrayDataClient adc = new GLArrayDataClient(); GLArrayHandler glArrayHandler = new GLFixedArrayHandler(adc); - adc.init(null, index, comps, dataType, normalized, 0, null, initialElementCount, false, glArrayHandler, 0, 0, 0, 0, false); + adc.init(null, index, comps, dataType, normalized, 0, null, initialElementCount, 0 /* mappedElementCount */, false, glArrayHandler, 0, 0, 0, 0, false); return adc; } @@ -55,13 +83,13 @@ public class GLArrayDataClient extends GLArrayDataWrapper implements GLArrayData * and starting with a given Buffer object incl it's stride * * On profiles GL2 and ES1 the fixed function pipeline behavior is as expected. - * On profile ES2 the fixed function emulation will transform these calls to + * On profile ES2 the fixed function emulation will transform these calls to * EnableVertexAttribArray and VertexAttribPointer calls, * and a predefined vertex attribute variable name will be chosen. - * - * The default name mapping will be used, + * + * The default name mapping will be used, * see {@link GLPointerFuncUtil#getPredefinedArrayIndexName(int)}. - * + * * @param index The GL array index * @param comps The array component number * @param dataType The array index GL data type @@ -70,40 +98,40 @@ public class GLArrayDataClient extends GLArrayDataWrapper implements GLArrayData * @param buffer the user define data * * @see javax.media.opengl.GLContext#getPredefinedArrayIndexName(int) - */ - public static GLArrayDataClient createFixed(int index, int comps, int dataType, boolean normalized, int stride, + */ + public static GLArrayDataClient createFixed(int index, int comps, int dataType, boolean normalized, int stride, Buffer buffer) throws GLException { GLArrayDataClient adc = new GLArrayDataClient(); GLArrayHandler glArrayHandler = new GLFixedArrayHandler(adc); - adc.init(null, index, comps, dataType, normalized, stride, buffer, comps*comps, false, glArrayHandler, 0, 0, 0, 0, false); + adc.init(null, index, comps, dataType, normalized, stride, buffer, comps*comps, 0 /* mappedElementCount */, false, glArrayHandler, 0, 0, 0, 0, false); return adc; } /** * Create a client side buffer object, using a custom GLSL array attribute name * and starting with a new created Buffer object with initialElementCount size - * @param name The custom name for the GL attribute. + * @param name The custom name for the GL attribute. * @param comps The array component number * @param dataType The array index GL data type * @param normalized Whether the data shall be normalized * @param initialElementCount */ - public static GLArrayDataClient createGLSL(String name, int comps, + public static GLArrayDataClient createGLSL(String name, int comps, int dataType, boolean normalized, int initialElementCount) throws GLException { GLArrayDataClient adc = new GLArrayDataClient(); GLArrayHandler glArrayHandler = new GLSLArrayHandler(adc); - adc.init(name, -1, comps, dataType, normalized, 0, null, initialElementCount, true, glArrayHandler, 0, 0, 0, 0, true); + adc.init(name, -1, comps, dataType, normalized, 0, null, initialElementCount, 0 /* mappedElementCount */, true, glArrayHandler, 0, 0, 0, 0, true); return adc; } /** * Create a client side buffer object, using a custom GLSL array attribute name * and starting with a given Buffer object incl it's stride - * @param name The custom name for the GL attribute. + * @param name The custom name for the GL attribute. * @param comps The array component number * @param dataType The array index GL data type * @param normalized Whether the data shall be normalized @@ -116,68 +144,85 @@ public class GLArrayDataClient extends GLArrayDataWrapper implements GLArrayData { GLArrayDataClient adc = new GLArrayDataClient(); GLArrayHandler glArrayHandler = new GLSLArrayHandler(adc); - adc.init(name, -1, comps, dataType, normalized, stride, buffer, comps*comps, true, glArrayHandler, 0, 0, 0, 0, true); + adc.init(name, -1, comps, dataType, normalized, stride, buffer, comps*comps, 0 /* mappedElementCount */, true, glArrayHandler, 0, 0, 0, 0, true); return adc; } - // + @Override + public void associate(Object obj, boolean enable) { + if(obj instanceof ShaderState) { + if(enable) { + shaderState = (ShaderState)obj; + } else { + shaderState = null; + } + } + } + + // // Data read access // + @Override public final boolean isVBOWritten() { return bufferWritten; } + @Override public final boolean sealed() { return sealed; } - + + @Override public final boolean enabled() { return bufferEnabled; } // // Data and GL state modification .. // - public final void setVBOWritten(boolean written) { bufferWritten=written; } + @Override + public final void setVBOWritten(boolean written) { + bufferWritten = ( 0 == mappedElementCount ) ? written : true; + } + @Override public void destroy(GL gl) { reset(gl); super.destroy(gl); } + @Override public void reset(GL gl) { enableBuffer(gl, false); reset(); } + @Override public void seal(GL gl, boolean seal) { seal(seal); enableBuffer(gl, seal); } + @Override public void enableBuffer(GL gl, boolean enable) { - if( enableBufferAlways || bufferEnabled != enable ) { + if( enableBufferAlways || bufferEnabled != enable ) { if(enable) { checkSeal(true); // init/generate VBO name if not done yet init_vbo(gl); } - final Object ext; - if(usesGLSL) { - ext = ShaderState.getShaderState(gl); - if(null == ext) { - throw new GLException("A ShaderState must be bound to the GL context, use 'ShaderState.setShaderState(gl)'"); - } - } else { - ext = null; - } - if(enable) { - glArrayHandler.syncData(gl, true, ext); - glArrayHandler.enableState(gl, true, ext); - } else { - glArrayHandler.enableState(gl, false, ext); - glArrayHandler.syncData(gl, false, ext); - } + glArrayHandler.enableState(gl, enable, usesGLSL ? shaderState : null); bufferEnabled = enable; } } + @Override + public boolean bindBuffer(GL gl, boolean bind) { + if(bind) { + checkSeal(true); + // init/generate VBO name if not done yet + init_vbo(gl); + } + return glArrayHandler.bindBuffer(gl, bind); + } + + @Override public void setEnableAlways(boolean always) { enableBufferAlways = always; } @@ -186,37 +231,41 @@ public class GLArrayDataClient extends GLArrayDataWrapper implements GLArrayData // Data modification .. // + @Override public void reset() { - if(buffer!=null) { + if( buffer != null ) { buffer.clear(); } - this.sealed=false; - this.bufferEnabled=false; - this.bufferWritten=false; + sealed = false; + bufferEnabled = false; + bufferWritten = ( 0 == mappedElementCount ) ? false : true; } + @Override public void seal(boolean seal) { - if(sealed==seal) return; + if( sealed == seal ) return; sealed = seal; - bufferWritten=false; - if(seal) { - if (null!=buffer) { + bufferWritten = ( 0 == mappedElementCount ) ? false : true; + if( seal ) { + if ( null != buffer ) { buffer.flip(); } - } else if (null!=buffer) { + } else if ( null != buffer ) { buffer.position(buffer.limit()); buffer.limit(buffer.capacity()); } } + @Override public void rewind() { if(buffer!=null) { buffer.rewind(); } } + @Override public void padding(int doneInByteSize) { if ( buffer==null || sealed ) return; while(doneInByteSize<strideB) { @@ -231,6 +280,7 @@ public class GLArrayDataClient extends GLArrayDataWrapper implements GLArrayData * This class buffer Class must match the arguments buffer class. * The arguments remaining elements must be a multiple of this arrays element stride. */ + @Override public void put(Buffer v) { if ( sealed ) return; /** FIXME: isn't true for interleaved arrays ! @@ -241,48 +291,57 @@ public class GLArrayDataClient extends GLArrayDataWrapper implements GLArrayData Buffers.put(buffer, v); } + @Override public void putb(byte v) { if ( sealed ) return; growBufferIfNecessary(1); Buffers.putb(buffer, v); } + @Override public void puts(short v) { if ( sealed ) return; growBufferIfNecessary(1); Buffers.puts(buffer, v); } + @Override public void puti(int v) { if ( sealed ) return; growBufferIfNecessary(1); Buffers.puti(buffer, v); } + @Override public void putx(int v) { puti(v); } + @Override public void putf(float v) { if ( sealed ) return; growBufferIfNecessary(1); Buffers.putf(buffer, v); } + @Override public String toString() { return "GLArrayDataClient["+name+ ", index "+index+ ", location "+location+ ", isVertexAttribute "+isVertexAttribute+ - ", dataType 0x"+Integer.toHexString(componentType)+ - ", bufferClazz "+componentClazz+ + ", usesGLSL "+usesGLSL+ + ", usesShaderState "+(null!=shaderState)+ + ", dataType 0x"+Integer.toHexString(componentType)+ + ", bufferClazz "+componentClazz+ ", elements "+getElementCount()+ - ", components "+components+ + ", components "+componentsPerElement+ ", stride "+strideB+"b "+strideL+"c"+ - ", initialElementCount "+initialElementCount+ - ", sealed "+sealed+ - ", bufferEnabled "+bufferEnabled+ - ", bufferWritten "+bufferWritten+ + ", mappedElementCount "+mappedElementCount+ + ", initialElementCount "+initialElementCount+ + ", sealed "+sealed+ + ", bufferEnabled "+bufferEnabled+ + ", bufferWritten "+bufferWritten+ ", buffer "+buffer+ ", alive "+alive+ "]"; @@ -290,48 +349,54 @@ public class GLArrayDataClient extends GLArrayDataWrapper implements GLArrayData // non public matters - protected final boolean growBufferIfNecessary(int spare) { - if(buffer==null || buffer.remaining()<spare) { - growBuffer(Math.max(initialElementCount, spare)); + protected final boolean growBufferIfNecessary(int spareComponents) { + if( buffer==null || buffer.remaining()<spareComponents ) { + if( 0 != mappedElementCount ) { + throw new GLException("Mapped buffer can't grow. Insufficient storage size: Needed "+spareComponents+" components, "+ + "mappedElementCount "+mappedElementCount+ + ", has mapped buffer "+buffer+"; "+this); + } + growBuffer(Math.max(initialElementCount, (spareComponents+componentsPerElement-1)/componentsPerElement)); return true; } return false; } - protected final void growBuffer(int additionalElements) { + protected final void growBuffer(int additionalElements) { if(!alive || sealed) { - throw new GLException("Invalid state: "+this); + throw new GLException("Invalid state: "+this); } // add the stride delta - additionalElements += (additionalElements/components)*(strideL-components); + additionalElements += (additionalElements/componentsPerElement)*(strideL-componentsPerElement); final int osize = (buffer!=null) ? buffer.capacity() : 0; - final int nsize = osize + ( additionalElements * components ); - + final int nsize = osize + ( additionalElements * componentsPerElement ); + final Buffer oldBuffer = buffer; + if(componentClazz==ByteBuffer.class) { - ByteBuffer newBBuffer = Buffers.newDirectByteBuffer( nsize ); + final ByteBuffer newBBuffer = Buffers.newDirectByteBuffer( nsize ); if(buffer!=null) { buffer.flip(); newBBuffer.put((ByteBuffer)buffer); } buffer = newBBuffer; } else if(componentClazz==ShortBuffer.class) { - ShortBuffer newSBuffer = Buffers.newDirectShortBuffer( nsize ); + final ShortBuffer newSBuffer = Buffers.newDirectShortBuffer( nsize ); if(buffer!=null) { buffer.flip(); newSBuffer.put((ShortBuffer)buffer); } buffer = newSBuffer; } else if(componentClazz==IntBuffer.class) { - IntBuffer newIBuffer = Buffers.newDirectIntBuffer( nsize ); + final IntBuffer newIBuffer = Buffers.newDirectIntBuffer( nsize ); if(buffer!=null) { buffer.flip(); newIBuffer.put((IntBuffer)buffer); } buffer = newIBuffer; } else if(componentClazz==FloatBuffer.class) { - FloatBuffer newFBuffer = Buffers.newDirectFloatBuffer( nsize ); + final FloatBuffer newFBuffer = Buffers.newDirectFloatBuffer( nsize ); if(buffer!=null) { buffer.flip(); newFBuffer.put((FloatBuffer)buffer); @@ -341,54 +406,91 @@ public class GLArrayDataClient extends GLArrayDataWrapper implements GLArrayData throw new GLException("Given Buffer Class not supported: "+componentClazz+":\n\t"+this); } if(DEBUG) { - System.err.println("*** Grow: comps: "+components+", "+(osize/components)+"/"+osize+" -> "+(nsize/components)+"/"+nsize+", "+this); + System.err.println("*** Grow: comps: "+componentsPerElement+", "+(osize/componentsPerElement)+"/"+osize+" -> "+(nsize/componentsPerElement)+"/"+nsize+ + "; "+oldBuffer+" -> "+buffer+"; "+this); } } protected final void checkSeal(boolean test) throws GLException { if(!alive) { - throw new GLException("Invalid state: "+this); - } + throw new GLException("Invalid state: "+this); + } if(sealed!=test) { if(test) { - throw new GLException("Not Sealed yet, seal first:\n\t"+this); + throw new GLException("Not Sealed yet, seal first:\n\t"+this); } else { - throw new GLException("Already Sealed, can't modify VBO:\n\t"+this); + throw new GLException("Already Sealed, can't modify VBO:\n\t"+this); } } } - protected void init(String name, int index, int comps, int dataType, boolean normalized, int stride, Buffer data, - int initialElementCount, boolean isVertexAttribute, GLArrayHandler handler, - int vboName, long vboOffset, int vboUsage, int vboTarget, boolean usesGLSL) + protected void init(String name, int index, int comps, int dataType, boolean normalized, int stride, Buffer data, + int initialElementCount, int mappedElementCount, boolean isVertexAttribute, + GLArrayHandler handler, int vboName, long vboOffset, int vboUsage, int vboTarget, boolean usesGLSL) throws GLException { - super.init(name, index, comps, dataType, normalized, stride, data, isVertexAttribute, - vboName, vboOffset, vboUsage, vboTarget); + super.init(name, index, comps, dataType, normalized, stride, data, mappedElementCount, + isVertexAttribute, vboName, vboOffset, vboUsage, vboTarget); + if( 0<mappedElementCount && 0<initialElementCount ) { // null!=buffer case validated in super.init(..) + throw new IllegalArgumentException("mappedElementCount:="+mappedElementCount+" specified, but passing non zero initialElementSize"); + } this.initialElementCount = initialElementCount; this.glArrayHandler = handler; this.usesGLSL = usesGLSL; this.sealed=false; this.bufferEnabled=false; this.enableBufferAlways=false; - this.bufferWritten=false; + this.bufferWritten = ( 0 == mappedElementCount ) ? false : true; + if(null==buffer && initialElementCount>0) { growBuffer(initialElementCount); } } private boolean isValidated = false; - + protected void init_vbo(GL gl) { if(!isValidated ) { isValidated = true; validate(gl.getGLProfile(), true); - } + } } protected GLArrayDataClient() { } + /** + * Copy Constructor + * <p> + * Buffer is {@link Buffers#slice(Buffer) sliced}, i.e. sharing content but using own state. + * </p> + * <p> + * All other values are simply copied. + * </p> + */ + public GLArrayDataClient(GLArrayDataClient src) { + super(src); + this.isValidated = src.isValidated; + this.sealed = src.sealed; + this.bufferEnabled = src.bufferEnabled; + this.bufferWritten = src.bufferWritten; + this.enableBufferAlways = src.enableBufferAlways; + this.initialElementCount = src.initialElementCount; + if( null != src.glArrayHandler ) { + final Class<? extends GLArrayHandler> clazz = src.glArrayHandler.getClass(); + try { + final Constructor<? extends GLArrayHandler> ctor = clazz.getConstructor(GLArrayDataEditable.class); + this.glArrayHandler = ctor.newInstance(this); + } catch (Exception e) { + throw new RuntimeException("Could not ctor "+clazz.getName()+"("+this.getClass().getName()+")", e); + } + } else { + this.glArrayHandler = null; + } + this.usesGLSL = src.usesGLSL; + this.shaderState = src.shaderState; + } + protected boolean sealed; protected boolean bufferEnabled; protected boolean bufferWritten; @@ -398,5 +500,7 @@ public class GLArrayDataClient extends GLArrayDataWrapper implements GLArrayData protected GLArrayHandler glArrayHandler; protected boolean usesGLSL; + protected ShaderState shaderState; + } |