From 3d537d4239e3772ffdb33592e5e66844d94ac9f3 Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Fri, 24 Feb 2023 15:33:39 +0100
Subject: GLArrayData*: Shorten methods (*API Change*), use proper constructor
 and finalize immutables, add growthFactor (default golden ratio 1.618), add
 getCapacity*() and printStats(..)

The growthFactor becomes essential for better growth behavior and can be set via setGrowthFactor().

The other changes were merely to clean up the GLArrayData interface and its 4 implementations.

Not great to change its API, but one name was misleading ['getComponentCount' -> 'getCompsPerEleme'],
so overall .. readability is enhanced.

Motivation for this change was the performance analysis and improvement of our Graph Curve Renderer.
---
 .../com/jogamp/opengl/util/GLArrayDataWrapper.java | 154 +++++++++++----------
 1 file changed, 83 insertions(+), 71 deletions(-)

(limited to 'src/jogl/classes/com/jogamp/opengl/util/GLArrayDataWrapper.java')

diff --git a/src/jogl/classes/com/jogamp/opengl/util/GLArrayDataWrapper.java b/src/jogl/classes/com/jogamp/opengl/util/GLArrayDataWrapper.java
index 3f759c6d8..ae19d788f 100644
--- a/src/jogl/classes/com/jogamp/opengl/util/GLArrayDataWrapper.java
+++ b/src/jogl/classes/com/jogamp/opengl/util/GLArrayDataWrapper.java
@@ -28,6 +28,7 @@
 
 package com.jogamp.opengl.util;
 
+import java.io.PrintStream;
 import java.nio.Buffer;
 import java.nio.ByteBuffer;
 import java.nio.FloatBuffer;
@@ -35,7 +36,6 @@ import java.nio.IntBuffer;
 import java.nio.ShortBuffer;
 
 import com.jogamp.opengl.GL;
-import com.jogamp.opengl.GL2ES1;
 import com.jogamp.opengl.GL2ES2;
 import com.jogamp.opengl.GLArrayData;
 import com.jogamp.opengl.GLException;
@@ -70,10 +70,8 @@ public class GLArrayDataWrapper implements GLArrayData {
                                                final Buffer buffer, final int vboName, final long vboOffset, final int vboUsage, final int vboTarget)
     throws GLException
   {
-      final GLArrayDataWrapper adc = new GLArrayDataWrapper();
-      adc.init(null, index, comps, dataType, normalized, stride, buffer, 0 /* mappedElementCount */,
-               false, vboName, vboOffset, vboUsage, vboTarget);
-      return adc;
+      return new GLArrayDataWrapper(null, index, comps, dataType, normalized, stride, buffer, 0 /* mappedElementCount */,
+                                    false, vboName, vboOffset, vboUsage, vboTarget);
   }
 
   /**
@@ -97,10 +95,8 @@ public class GLArrayDataWrapper implements GLArrayData {
                                                final int mappedElementCount, final int vboName, final long vboOffset, final int vboUsage, final int vboTarget)
     throws GLException
   {
-      final GLArrayDataWrapper adc = new GLArrayDataWrapper();
-      adc.init(null, index, comps, dataType, normalized, stride, null, mappedElementCount,
-               false, vboName, vboOffset, vboUsage, vboTarget);
-      return adc;
+      return new GLArrayDataWrapper(null, index, comps, dataType, normalized, stride, null, mappedElementCount,
+                                    false, vboName, vboOffset, vboUsage, vboTarget);
   }
 
   /**
@@ -123,10 +119,8 @@ public class GLArrayDataWrapper implements GLArrayData {
                                              final Buffer buffer, final int vboName, final long vboOffset, final int vboUsage, final int vboTarget)
     throws GLException
   {
-      final GLArrayDataWrapper adc = new GLArrayDataWrapper();
-      adc.init(name, -1, comps, dataType, normalized, stride, buffer, 0  /* mappedElementCount */,
-              true, vboName, vboOffset, vboUsage, vboTarget);
-      return adc;
+      return new GLArrayDataWrapper(name, -1, comps, dataType, normalized, stride, buffer, 0  /* mappedElementCount */,
+                                    true, vboName, vboOffset, vboUsage, vboTarget);
   }
 
   /**
@@ -149,10 +143,8 @@ public class GLArrayDataWrapper implements GLArrayData {
                                               final int mappedElementCount, final int vboName, final long vboOffset, final int vboUsage, final int vboTarget)
     throws GLException
   {
-      final GLArrayDataWrapper adc = new GLArrayDataWrapper();
-      adc.init(name, -1, comps, dataType, normalized, stride, null, mappedElementCount,
-              true, vboName, vboOffset, vboUsage, vboTarget);
-      return adc;
+      return new GLArrayDataWrapper(name, -1, comps, dataType, normalized, stride, null, mappedElementCount,
+                                    true, vboName, vboOffset, vboUsage, vboTarget);
   }
 
   /**
@@ -176,7 +168,7 @@ public class GLArrayDataWrapper implements GLArrayData {
         }
         return false;
     }
-    return glp.isValidArrayDataType(getIndex(), getComponentCount(), getComponentType(), isVertexAttribute(), throwException);
+    return glp.isValidArrayDataType(getIndex(), getCompsPerElem(), getCompType(), isVertexAttribute(), throwException);
   }
 
   @Override
@@ -235,21 +227,21 @@ public class GLArrayDataWrapper implements GLArrayData {
   public Buffer getBuffer() { return buffer; }
 
   @Override
-  public final int getComponentCount() { return componentsPerElement; }
+  public final int getCompsPerElem() { return componentsPerElement; }
 
   @Override
-  public final int getComponentType() { return componentType; }
+  public final int getCompType() { return componentType; }
 
   @Override
-  public final int getComponentSizeInBytes() { return componentByteSize; }
+  public final int getBytesPerComp() { return bytesPerComponent; }
 
   @Override
-  public final int getElementCount() {
+  public final int getElemCount() {
     if( 0 != mappedElementCount ) {
         return mappedElementCount;
     } else if( null != buffer ) {
         final int remainingComponents = ( 0 == buffer.position() ) ? buffer.limit() : buffer.position();
-        return ( remainingComponents * componentByteSize ) / strideB ;
+        return ( remainingComponents * bytesPerComponent ) / strideB ;
     } else {
         return 0;
     }
@@ -258,14 +250,28 @@ public class GLArrayDataWrapper implements GLArrayData {
   @Override
   public final int getSizeInBytes() {
     if( 0 != mappedElementCount ) {
-        return mappedElementCount * componentsPerElement * componentByteSize ;
+        return mappedElementCount * componentsPerElement * bytesPerComponent ;
     } else if( null != buffer ) {
-        return ( buffer.position()==0 ) ? ( buffer.limit() * componentByteSize ) : ( buffer.position() * componentByteSize ) ;
+        return ( buffer.position()==0 ) ? ( buffer.limit() * bytesPerComponent ) : ( buffer.position() * bytesPerComponent ) ;
     } else {
         return 0;
     }
   }
 
+  @Override
+  public int getCapacityInBytes() {
+    if( null != buffer ) {
+        return buffer.capacity() * bytesPerComponent;
+    } else {
+        return 0;
+    }
+  }
+
+  @Override
+  public void printStats(final PrintStream out) {
+      out.printf("elements %,d, bytes %,d / %,d", getElemCount(), getSizeInBytes(), getCapacityInBytes());
+  }
+
   @Override
   public final boolean getNormalized() { return normalized; }
 
@@ -291,7 +297,7 @@ public class GLArrayDataWrapper implements GLArrayData {
                        ", isVertexAttribute "+isVertexAttribute+
                        ", dataType 0x"+Integer.toHexString(componentType)+
                        ", bufferClazz "+componentClazz+
-                       ", elements "+getElementCount()+
+                       ", elements "+getElemCount()+
                        ", components "+componentsPerElement+
                        ", stride "+strideB+"b "+strideL+"c"+
                        ", mappedElementCount "+mappedElementCount+
@@ -364,17 +370,14 @@ public class GLArrayDataWrapper implements GLArrayData {
       this.vboTarget = vboTarget;
   }
 
-  protected void init(final String name, final int index, final int componentsPerElement, final int componentType,
-                      final boolean normalized, final int stride, final Buffer data, final int mappedElementCount,
-                      final boolean isVertexAttribute, final int vboName, final long vboOffset, final int vboUsage, final int vboTarget)
+  protected GLArrayDataWrapper(final String name, final int index, final int componentsPerElement, final int componentType,
+                               final boolean normalized, final int stride, final Buffer data, final int mappedElementCount,
+                               final boolean isVertexAttribute, final int vboName, final long vboOffset, final int vboUsage, final int vboTarget)
     throws GLException
   {
     if( 0<mappedElementCount && null != data ) {
         throw new IllegalArgumentException("mappedElementCount:="+mappedElementCount+" specified, but passing non null buffer");
     }
-    this.isVertexAttribute = isVertexAttribute;
-    this.index = index;
-    this.location = -1;
     // We can't have any dependence on the FixedFuncUtil class here for build bootstrapping reasons
 
     if( GL.GL_ELEMENT_ARRAY_BUFFER == vboTarget ) {
@@ -389,15 +392,11 @@ public class GLArrayDataWrapper implements GLArrayData {
         throw new GLException("Invalid GPUBuffer target: 0x"+Integer.toHexString(vboTarget));
     }
 
+    // immutable types
     this.componentType = componentType;
     componentClazz = getBufferClass(componentType);
-    if( GLBuffers.isGLTypeFixedPoint(componentType) ) {
-        this.normalized = normalized;
-    } else {
-        this.normalized = false;
-    }
-    componentByteSize = GLBuffers.sizeOfGLType(componentType);
-    if(0 > componentByteSize) {
+    bytesPerComponent = GLBuffers.sizeOfGLType(componentType);
+    if(0 > bytesPerComponent) {
         throw new GLException("Given componentType not supported: "+componentType+":\n\t"+this);
     }
     if(0 >= componentsPerElement) {
@@ -405,19 +404,30 @@ public class GLArrayDataWrapper implements GLArrayData {
     }
     this.componentsPerElement = componentsPerElement;
 
-    if(0<stride && stride<componentsPerElement*componentByteSize) {
-        throw new GLException("stride ("+stride+") lower than component bytes, "+componentsPerElement+" * "+componentByteSize);
+    if(0<stride && stride<componentsPerElement*bytesPerComponent) {
+        throw new GLException("stride ("+stride+") lower than component bytes, "+componentsPerElement+" * "+bytesPerComponent);
     }
-    if(0<stride && stride%componentByteSize!=0) {
-        throw new GLException("stride ("+stride+") not a multiple of bpc "+componentByteSize);
+    if(0<stride && stride%bytesPerComponent!=0) {
+        throw new GLException("stride ("+stride+") not a multiple of bpc "+bytesPerComponent);
+    }
+    this.strideB=(0==stride)?componentsPerElement*bytesPerComponent:stride;
+    this.strideL=strideB/bytesPerComponent;
+
+    if( GLBuffers.isGLTypeFixedPoint(componentType) ) {
+        this.normalized = normalized;
+    } else {
+        this.normalized = false;
     }
-    this.buffer = data;
     this.mappedElementCount = mappedElementCount;
-    this.strideB=(0==stride)?componentsPerElement*componentByteSize:stride;
-    this.strideL=strideB/componentByteSize;
+    this.isVertexAttribute = isVertexAttribute;
+
+    // mutable types
+    this.index = index;
+    this.location = -1;
+    this.buffer = data;
     this.vboName= vboName;
-    this.vboEnabled= 0 != vboName ;
     this.vboOffset=vboOffset;
+    this.vboEnabled= 0 != vboName ;
 
     switch(vboUsage) {
         case 0: // nop
@@ -441,8 +451,6 @@ public class GLArrayDataWrapper implements GLArrayData {
     this.alive=true;
   }
 
-  protected GLArrayDataWrapper() { }
-
   /**
    * Copy Constructor
    * <p>
@@ -453,17 +461,22 @@ public class GLArrayDataWrapper implements GLArrayData {
    * </p>
    */
   public GLArrayDataWrapper(final GLArrayDataWrapper src) {
-    this.alive = src.alive;
-    this.index = src.index;
-    this.location = src.location;
-    this.name = src.name;
-    this.componentsPerElement = src.componentsPerElement;
+    // immutable types
     this.componentType = src.componentType;
     this.componentClazz = src.componentClazz;
-    this.componentByteSize = src.componentByteSize;
-    this.normalized = src.normalized;
+    this.bytesPerComponent = src.bytesPerComponent;
+    this.componentsPerElement = src.componentsPerElement;
     this.strideB = src.strideB;
     this.strideL = src.strideL;
+    this.normalized = src.normalized;
+    this.mappedElementCount = src.mappedElementCount;
+    this.isVertexAttribute = src.isVertexAttribute;
+
+    // mutable types
+    this.alive = src.alive;
+    this.index = src.index;
+    this.location = src.location;
+    this.name = src.name;
     if( null != src.buffer ) {
         if( src.buffer.position() == 0 ) {
             this.buffer = Buffers.slice(src.buffer);
@@ -473,33 +486,32 @@ public class GLArrayDataWrapper implements GLArrayData {
     } else {
         this.buffer = null;
     }
-    this.mappedElementCount = src.mappedElementCount;
-    this.isVertexAttribute = src.isVertexAttribute;
-    this.vboOffset = src.vboOffset;
     this.vboName = src.vboName;
+    this.vboOffset = src.vboOffset;
     this.vboEnabled = src.vboEnabled;
     this.vboUsage = src.vboUsage;
     this.vboTarget = src.vboTarget;
   }
 
+  protected final int componentType;
+  protected final Class<?> componentClazz;
+  protected final int bytesPerComponent;
+  protected final int componentsPerElement;
+  /** stride in bytes; strideB >= componentsPerElement * componentByteSize */
+  protected final int strideB;
+  /** stride in logical components */
+  protected final int strideL;
+  protected final boolean normalized;
+  protected final int mappedElementCount;
+  protected final boolean isVertexAttribute;
+
   protected boolean alive;
   protected int index;
   protected int location;
   protected String name;
-  protected int componentsPerElement;
-  protected int componentType;
-  protected Class<?> componentClazz;
-  protected int componentByteSize;
-  protected boolean normalized;
-  /** stride in bytes; strideB >= componentsPerElement * componentByteSize */
-  protected int strideB;
-  /** stride in logical components */
-  protected int strideL;
   protected Buffer buffer;
-  protected int mappedElementCount;
-  protected boolean isVertexAttribute;
-  protected long vboOffset;
   protected int vboName;
+  protected long vboOffset;
   protected boolean vboEnabled;
   protected int vboUsage;
   protected int vboTarget;
-- 
cgit v1.2.3