From 307479391f955a5bd611b4ad4db6f53e097d15c5 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 24 Feb 2023 22:15:20 +0100 Subject: Graph Region: Address overflow issue using GL2ES3 integer indices (WIP...); Ease GLArrayData* buffer growth. Using integer indices, i.e. GL_UNSIGNED_INT, requires us to pass a GLProfile 'hint' to the GLRegion ctor. Region.max_indices is computed in this regard and used in Region.addOutlineShape(). TODO: If exceeding max_indices, the code path needs some work. Buffer growth is eased via GLArrayData using its golden growth ratio and manually triggering growth before processing all triangles in Region.addOutlineShape(). +++ TextRegionUtil static drawText() won't clear passed Region anymore, caller has to do this if so intended. --- .../classes/com/jogamp/graph/curve/Region.java | 50 ++++++++++++++++------ .../com/jogamp/graph/curve/opengl/GLRegion.java | 41 +++++++++++++++--- .../jogamp/graph/curve/opengl/TextRegionUtil.java | 15 ++++--- 3 files changed, 81 insertions(+), 25 deletions(-) (limited to 'src/jogl/classes/com/jogamp/graph') diff --git a/src/jogl/classes/com/jogamp/graph/curve/Region.java b/src/jogl/classes/com/jogamp/graph/curve/Region.java index 264b00b92..8843188b4 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/Region.java +++ b/src/jogl/classes/com/jogamp/graph/curve/Region.java @@ -27,6 +27,7 @@ */ package com.jogamp.graph.curve; +import java.io.PrintStream; import java.util.ArrayList; import java.util.List; @@ -35,7 +36,9 @@ import jogamp.opengl.Debug; import com.jogamp.graph.geom.Triangle; import com.jogamp.graph.geom.Vertex; import com.jogamp.graph.geom.plane.AffineTransform; +import com.jogamp.common.nio.Buffers; import com.jogamp.graph.curve.opengl.GLRegion; +import com.jogamp.opengl.GLProfile; import com.jogamp.opengl.math.geom.AABBox; import com.jogamp.opengl.math.geom.Frustum; import com.jogamp.opengl.util.texture.TextureSequence; @@ -110,6 +113,8 @@ public abstract class Region { protected static final int DIRTY_STATE = 1 << 1 ; private final int renderModes; + private final boolean use_int32_idx; + private final int max_indices; private int quality; private int dirty = DIRTY_SHAPE | DIRTY_STATE; private int numVertices = 0; @@ -168,19 +173,33 @@ public abstract class Region { } } - protected Region(final int regionRenderModes) { + protected Region(final int regionRenderModes, final boolean use_int32_idx) { this.renderModes = regionRenderModes; + this.use_int32_idx = use_int32_idx; + if( use_int32_idx ) { + this.max_indices = GL_INT32_MAX / Buffers.SIZEOF_INT; // byte-size int32_t limit + } else { + this.max_indices = GL_UINT16_MAX; + } this.quality = MAX_QUALITY; } - // FIXME: Better handling of impl. buffer growth .. ! - // protected abstract void setupInitialComponentCount(int attributeCount, int indexCount); + /** Print implementation buffer stats like detailed and total size and capacity in bytes etc */ + public abstract void printBufferStats(PrintStream out); + + /** + * Returns true if implementation uses `int32_t` sized indices implying at least a {@link GLProfile#isGL2ES3()} alike context. + * Otherwise method returns false on {@link GLProfile#isGLES2()} using `uint16_t` sized indices. + */ + public final boolean usesI32Idx() { return this.use_int32_idx; } + + protected abstract void growBufferSize(int verticeCount, int indexCount); protected abstract void pushVertex(final float[] coords, final float[] texParams, float[] rgba); protected abstract void pushIndex(int idx); /** - * Return bit-field of render modes, see {@link GLRegion#create(int, TextureSequence)}. + * Return bit-field of render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence)}. */ public final int getRenderModes() { return renderModes; } @@ -269,6 +288,9 @@ public abstract class Region { private final AABBox tmpBox = new AABBox(); + protected static final int GL_UINT16_MAX = 0xffff; // 65,535 + protected static final int GL_INT32_MAX = 0x7fffffff; // 2,147,483,647 + /** * Add the given {@link OutlineShape} to this region with the given optional {@link AffineTransform}. *

@@ -297,15 +319,17 @@ public abstract class Region { } final List trisIn = shape.getTriangles(OutlineShape.VerticesState.QUADRATIC_NURBS); final ArrayList vertsIn = shape.getVertices(); - if(DEBUG_INSTANCE) { + { final int addedVerticeCount = shape.getAddedVerticeCount(); final int verticeCount = vertsIn.size() + addedVerticeCount; final int indexCount = trisIn.size() * 3; - System.err.println("Region.addOutlineShape().0: tris: "+trisIn.size()+", verts "+vertsIn.size()+", transform "+t); - System.err.println("Region.addOutlineShape().0: VerticeCount "+vertsIn.size()+" + "+addedVerticeCount+" = "+verticeCount); - System.err.println("Region.addOutlineShape().0: IndexCount "+indexCount); + if(DEBUG_INSTANCE) { + System.err.println("Region.addOutlineShape().0: tris: "+trisIn.size()+", verts "+vertsIn.size()+", transform "+t); + System.err.println("Region.addOutlineShape().0: VerticeCount "+vertsIn.size()+" + "+addedVerticeCount+" = "+verticeCount); + System.err.println("Region.addOutlineShape().0: IndexCount "+indexCount); + } + growBufferSize(verticeCount, indexCount); } - // setupInitialComponentCount(verticeCount, indexCount); // FIXME: Use it ? final int idxOffset = numVertices; int vertsVNewIdxCount = 0, vertsTMovIdxCount = 0, vertsTNewIdxCount = 0, tris = 0; @@ -330,7 +354,7 @@ public abstract class Region { // triangles.add( triEx ); final Vertex[] triInVertices = triIn.getVertices(); final int tv0Idx = triInVertices[0].getId(); - if( Integer.MAX_VALUE-idxOffset > tv0Idx ) { // Integer.MAX_VALUE != i0 // FIXME: renderer uses SHORT! + if ( max_indices - idxOffset > tv0Idx ) { // valid 'known' idx - move by offset if(Region.DEBUG_INSTANCE) { System.err.println("T["+i+"]: Moved "+tv0Idx+" + "+idxOffset+" -> "+(tv0Idx+idxOffset)); @@ -340,7 +364,7 @@ public abstract class Region { pushIndex(triInVertices[2].getId()+idxOffset); vertsTMovIdxCount+=3; } else { - // invalid idx - generate new one + // FIXME: Invalid idx - generate new one if(Region.DEBUG_INSTANCE) { System.err.println("T["+i+"]: New Idx "+numVertices); } @@ -353,12 +377,13 @@ public abstract class Region { } } if(DEBUG_INSTANCE) { - System.err.println("Region.addOutlineShape().X: idxOffset "+idxOffset+", tris: "+tris+", verts [idx "+vertsTNewIdxCount+", add "+vertsTNewIdxCount+" = "+(vertsVNewIdxCount+vertsTNewIdxCount)+"]"); + System.err.println("Region.addOutlineShape().X: idx[ui32 "+usesI32Idx()+", offset "+idxOffset+"], tris: "+tris+", verts [idx "+vertsTNewIdxCount+", add "+vertsTNewIdxCount+" = "+(vertsVNewIdxCount+vertsTNewIdxCount)+"]"); System.err.println("Region.addOutlineShape().X: verts: idx[v-new "+vertsVNewIdxCount+", t-new "+vertsTNewIdxCount+" = "+(vertsVNewIdxCount+vertsTNewIdxCount)+"]"); System.err.println("Region.addOutlineShape().X: verts: idx t-moved "+vertsTMovIdxCount+", numVertices "+numVertices); System.err.println("Region.addOutlineShape().X: verts: v-dups "+vertsDupCountV+", t-dups "+vertsDupCountT+", t-known "+vertsKnownMovedT); // int vertsDupCountV = 0, vertsDupCountT = 0; System.err.println("Region.addOutlineShape().X: box "+box); + printBufferStats(System.err); } markShapeDirty(); } @@ -405,6 +430,7 @@ public abstract class Region { } protected final int getDirtyBits() { return dirty; } + @Override public String toString() { return "Region["+getRenderModeString(this.renderModes)+", q "+quality+", dirty "+dirty+", vertices "+numVertices+", box "+box+"]"; } diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java index d924fa9c8..69638f60e 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java +++ b/src/jogl/classes/com/jogamp/graph/curve/opengl/GLRegion.java @@ -29,6 +29,8 @@ package com.jogamp.graph.curve.opengl; import com.jogamp.opengl.GL; import com.jogamp.opengl.GL2ES2; +import com.jogamp.opengl.GLArrayData; +import com.jogamp.opengl.GLProfile; import jogamp.graph.curve.opengl.VBORegion2PMSAAES2; import jogamp.graph.curve.opengl.VBORegion2PVBAAES2; @@ -37,6 +39,10 @@ import jogamp.graph.curve.opengl.VBORegionSPES2; import com.jogamp.opengl.util.PMVMatrix; import com.jogamp.opengl.util.texture.TextureSequence; import com.jogamp.graph.curve.Region; + +import java.io.PrintStream; + +import com.jogamp.common.nio.Buffers; import com.jogamp.graph.curve.OutlineShape; /** A GLRegion is the OGL binding of one or more OutlineShapes @@ -57,31 +63,36 @@ public abstract class GLRegion extends Region { * *

In case {@link Region#VBAA_RENDERING_BIT} is being requested the default texture unit * {@link Region#DEFAULT_TWO_PASS_TEXTURE_UNIT} is being used.

+ * @param glp intended GLProfile to use. Instance may use higher OpenGL features if indicated by GLProfile. * @param renderModes bit-field of modes, e.g. {@link Region#VARWEIGHT_RENDERING_BIT}, {@link Region#VBAA_RENDERING_BIT} * @param colorTexSeq optional {@link TextureSequence} for {@link Region#COLORTEXTURE_RENDERING_BIT} rendering mode. */ - public static GLRegion create(int renderModes, final TextureSequence colorTexSeq) { + public static GLRegion create(final GLProfile glp, int renderModes, final TextureSequence colorTexSeq) { if( null != colorTexSeq ) { renderModes |= Region.COLORTEXTURE_RENDERING_BIT; } else if( Region.hasColorTexture(renderModes) ) { throw new IllegalArgumentException("COLORTEXTURE_RENDERING_BIT set but null TextureSequence"); } if( isVBAA(renderModes) ) { - return new VBORegion2PVBAAES2(renderModes, colorTexSeq, Region.DEFAULT_TWO_PASS_TEXTURE_UNIT); + return new VBORegion2PVBAAES2(glp, renderModes, colorTexSeq, Region.DEFAULT_TWO_PASS_TEXTURE_UNIT); } else if( isMSAA(renderModes) ) { - return new VBORegion2PMSAAES2(renderModes, colorTexSeq, Region.DEFAULT_TWO_PASS_TEXTURE_UNIT); + return new VBORegion2PMSAAES2(glp, renderModes, colorTexSeq, Region.DEFAULT_TWO_PASS_TEXTURE_UNIT); } else { - return new VBORegionSPES2(renderModes, colorTexSeq); + return new VBORegionSPES2(glp, renderModes, colorTexSeq); } } + private final int gl_idx_type; protected final TextureSequence colorTexSeq; - protected GLRegion(final int renderModes, final TextureSequence colorTexSeq) { - super(renderModes); + protected GLRegion(final GLProfile glp, final int renderModes, final TextureSequence colorTexSeq) { + super(renderModes, glp.isGL2ES3() /* use_int32_idx */); + this.gl_idx_type = usesI32Idx() ? GL.GL_UNSIGNED_INT : GL.GL_UNSIGNED_SHORT; this.colorTexSeq = colorTexSeq; } + protected final int glIdxType() { return this.gl_idx_type; } + /** * Updates a graph region by updating the ogl related * objects for use in rendering if {@link #isShapeDirty()}. @@ -94,12 +105,28 @@ public abstract class GLRegion extends Region { protected abstract void clearImpl(final GL2ES2 gl); + protected static void printAndCount(final PrintStream out, final String name, final GLArrayData data, final int[] size, final int[] capacity) { + out.print(name+"["); + if( null != data ) { + data.printStats(out); + size[0] += data.getSizeInBytes(); + capacity[0] += data.getCapacityInBytes(); + out.print("]"); + } else { + out.print("null]"); + } + } + /** * Clears all data, i.e. triangles, vertices etc. + * + * @param gl the current {@link GL2ES2} object + * @return this {@link GLRegion} for chaining. */ - public void clear(final GL2ES2 gl) { + public GLRegion clear(final GL2ES2 gl) { clearImpl(gl); clearImpl(); + return this; } /** diff --git a/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java b/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java index 88e972bdc..547a07fba 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java +++ b/src/jogl/classes/com/jogamp/graph/curve/opengl/TextRegionUtil.java @@ -144,7 +144,7 @@ public class TextRegionUtil { GLRegion region = getCachedRegion(font, str); AABBox res; if(null == region) { - region = GLRegion.create(renderModes, null); + region = GLRegion.create(gl.getGLProfile(), renderModes, null); res = addStringToRegion(region, font, null, str, rgbaColor, tempT1, tempT2); addCachedRegion(gl, font, str, region); } else { @@ -185,7 +185,7 @@ public class TextRegionUtil { if(!renderer.isInitialized()){ throw new GLException("TextRendererImpl01: not initialized!"); } - final GLRegion region = GLRegion.create(renderModes, null); + final GLRegion region = GLRegion.create(gl.getGLProfile(), renderModes, null); final AABBox res = addStringToRegion(region, font, null, str, rgbaColor); region.draw(gl, renderer, sampleCount); region.destroy(gl); @@ -193,8 +193,10 @@ public class TextRegionUtil { } /** - * Render the string in 3D space w.r.t. the font in font em-size [0..1] at the end of the given {@link GLRegion}, - * which will {@link GLRegion#clear(GL2ES2) cleared} beforehand. + * Render the string in 3D space w.r.t. the font in font em-size [0..1] at the end of the given {@link GLRegion}. + *

+ * User might want to {@link GLRegion#clear(GL2ES2)} the region before calling this method. + *

*

* The shapes added to the GLRegion are in font em-size [0..1]. *

@@ -202,6 +204,8 @@ public class TextRegionUtil { * Origin of rendered text is 0/0 at bottom left. *

* @param gl the current GL state + * @param region + * @param renderer * @param font {@link Font} to be used * @param str text to be rendered * @param rgbaColor if {@link Region#hasColorChannel()} RGBA color must be passed, otherwise value is ignored. @@ -216,14 +220,13 @@ public class TextRegionUtil { if(!renderer.isInitialized()){ throw new GLException("TextRendererImpl01: not initialized!"); } - region.clear(gl); final AABBox res = addStringToRegion(region, font, null, str, rgbaColor); region.draw(gl, renderer, sampleCount); return res; } /** - * Clear all cached {@link GLRegions}. + * Clear all cached {@link GLRegions} and mapped values. */ public void clear(final GL2ES2 gl) { // fluchCache(gl) already called -- cgit v1.2.3