From 0f21f197356249315b30709c75fa8372c5349e46 Mon Sep 17 00:00:00 2001 From: "sha..RD" Date: Thu, 18 Apr 2013 04:14:55 +0000 Subject: [PATCH] * Mesh.prepareForAnim() is now much smarter about what to do. It won't convert the buffers unless absolutely necessary and apply the proper usages to the buffers as needed. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10555 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- engine/src/core/com/jme3/scene/Mesh.java | 100 ++++++++++++----------- 1 file changed, 51 insertions(+), 49 deletions(-) diff --git a/engine/src/core/com/jme3/scene/Mesh.java b/engine/src/core/com/jme3/scene/Mesh.java index 1ba48268e..1f2c03d3c 100644 --- a/engine/src/core/com/jme3/scene/Mesh.java +++ b/engine/src/core/com/jme3/scene/Mesh.java @@ -353,69 +353,71 @@ public class Mesh implements Savable, Cloneable { */ public void prepareForAnim(boolean forSoftwareAnim){ if (forSoftwareAnim) { - // convert indices to ubytes on the heap or floats + // convert indices to ubytes on the heap VertexBuffer indices = getBuffer(Type.BoneIndex); - Buffer buffer = indices.getData(); - if (buffer instanceof ByteBuffer) { - ByteBuffer originalIndex = (ByteBuffer) buffer; + if (!indices.getData().hasArray()) { + ByteBuffer originalIndex = (ByteBuffer) indices.getData(); ByteBuffer arrayIndex = ByteBuffer.allocate(originalIndex.capacity()); originalIndex.clear(); arrayIndex.put(originalIndex); indices.updateData(arrayIndex); - } else if (buffer instanceof FloatBuffer) { - //Floats back to bytes - FloatBuffer originalIndex = (FloatBuffer) buffer; - ByteBuffer arrayIndex = ByteBuffer.allocate(originalIndex.capacity()); - originalIndex.clear(); - for (int i = 0; i < originalIndex.capacity(); i++) { - arrayIndex.put((byte) originalIndex.get(i)); - } - indices.updateData(arrayIndex); } + indices.setUsage(Usage.CpuOnly); // convert weights on the heap VertexBuffer weights = getBuffer(Type.BoneWeight); - FloatBuffer originalWeight = (FloatBuffer) weights.getData(); - FloatBuffer arrayWeight = FloatBuffer.allocate(originalWeight.capacity()); - originalWeight.clear(); - arrayWeight.put(originalWeight); - weights.updateData(arrayWeight); + if (!weights.getData().hasArray()) { + FloatBuffer originalWeight = (FloatBuffer) weights.getData(); + FloatBuffer arrayWeight = FloatBuffer.allocate(originalWeight.capacity()); + originalWeight.clear(); + arrayWeight.put(originalWeight); + weights.updateData(arrayWeight); + } + weights.setUsage(Usage.CpuOnly); + + // position, normal, and tanget buffers to be in "Stream" mode + VertexBuffer positions = getBuffer(Type.Position); + VertexBuffer normals = getBuffer(Type.Normal); + VertexBuffer tangents = getBuffer(Type.Tangent); + positions.setUsage(Usage.Stream); + if (normals != null) { + normals.setUsage(Usage.Stream); + } + if (tangents != null) { + tangents.setUsage(Usage.Stream); + } } else { - //BoneIndex must be 32 bit for attribute type constraints in shaders VertexBuffer indices = getBuffer(Type.BoneIndex); - Buffer buffer = indices.getData(); - if (buffer instanceof ByteBuffer) { - ByteBuffer bIndex = (ByteBuffer) buffer; - final float[] rval = new float[bIndex.capacity()]; - for (int i = 0; i < rval.length; i++) { - rval[i] = bIndex.get(i); - } - clearBuffer(Type.BoneIndex); - - VertexBuffer ib = new VertexBuffer(Type.BoneIndex); - ib.setupData(Usage.Stream, - 4, - Format.Float, - BufferUtils.createFloatBuffer(rval)); - setBuffer(ib); - } else if (buffer instanceof FloatBuffer) { - //BoneWeights on DirectBuffer - FloatBuffer originalIndices = (FloatBuffer) buffer; - FloatBuffer arrayIndices = BufferUtils.createFloatBuffer(originalIndices.capacity()); - originalIndices.clear(); - arrayIndices.put(originalIndices); - indices.setUsage(Usage.Stream); - indices.updateData(arrayIndices); + if (!indices.getData().isDirect()) { + ByteBuffer originalIndex = (ByteBuffer) indices.getData(); + ByteBuffer directIndex = BufferUtils.createByteBuffer(originalIndex.capacity()); + originalIndex.clear(); + directIndex.put(originalIndex); + indices.updateData(directIndex); } - - //BoneWeights on DirectBuffer + indices.setUsage(Usage.Static); + VertexBuffer weights = getBuffer(Type.BoneWeight); - FloatBuffer originalWeight = (FloatBuffer) weights.getData(); - FloatBuffer arrayWeight = BufferUtils.createFloatBuffer(originalWeight.capacity()); - originalWeight.clear(); - arrayWeight.put(originalWeight); + if (!weights.getData().isDirect()) { + FloatBuffer originalWeight = (FloatBuffer) weights.getData(); + FloatBuffer directWeight = BufferUtils.createFloatBuffer(originalWeight.capacity()); + originalWeight.clear(); + directWeight.put(originalWeight); + weights.updateData(directWeight); + } weights.setUsage(Usage.Static); - weights.updateData(arrayWeight); + + // position, normal, and tanget buffers to be in "Static" mode + VertexBuffer positions = getBuffer(Type.Position); + VertexBuffer normals = getBuffer(Type.Normal); + VertexBuffer tangents = getBuffer(Type.Tangent); + positions.setUsage(Usage.Static); + if (normals != null) { + normals.setUsage(Usage.Static); + } + if (tangents != null) { + tangents.setUsage(Usage.Static); + } } }