- Faster BatchNode
git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8564 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
This commit is contained in:
parent
a5ff915fc1
commit
96acebfe4f
@ -80,6 +80,9 @@ public class BatchNode extends Node implements Savable {
|
|||||||
* used to store transformed vectors before proceeding to a bulk put into the FloatBuffer
|
* used to store transformed vectors before proceeding to a bulk put into the FloatBuffer
|
||||||
*/
|
*/
|
||||||
private float[] tmpFloat;
|
private float[] tmpFloat;
|
||||||
|
private float[] tmpFloatN;
|
||||||
|
private float[] tmpFloatT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a batchNode
|
* Construct a batchNode
|
||||||
*/
|
*/
|
||||||
@ -142,21 +145,23 @@ public class BatchNode extends Node implements Savable {
|
|||||||
if (batch != null) {
|
if (batch != null) {
|
||||||
Mesh mesh = batch.geometry.getMesh();
|
Mesh mesh = batch.geometry.getMesh();
|
||||||
|
|
||||||
FloatBuffer buf = (FloatBuffer) mesh.getBuffer(VertexBuffer.Type.Position).getData();
|
VertexBuffer pvb = mesh.getBuffer(VertexBuffer.Type.Position);
|
||||||
doTransformVerts(buf, bg.startIndex, bg.startIndex + bg.getVertexCount(), buf, bg.cachedOffsetMat);
|
FloatBuffer posBuf = (FloatBuffer) pvb.getData();
|
||||||
mesh.getBuffer(VertexBuffer.Type.Position).updateData(buf);
|
VertexBuffer nvb = mesh.getBuffer(VertexBuffer.Type.Normal);
|
||||||
|
FloatBuffer normBuf = (FloatBuffer) nvb.getData();
|
||||||
buf = (FloatBuffer) mesh.getBuffer(VertexBuffer.Type.Normal).getData();
|
|
||||||
doTransformNorm(buf, bg.startIndex, bg.startIndex + bg.getVertexCount(), buf, bg.cachedOffsetMat);
|
|
||||||
mesh.getBuffer(VertexBuffer.Type.Normal).updateData(buf);
|
|
||||||
|
|
||||||
|
|
||||||
if (mesh.getBuffer(VertexBuffer.Type.Tangent) != null) {
|
if (mesh.getBuffer(VertexBuffer.Type.Tangent) != null) {
|
||||||
|
|
||||||
buf = (FloatBuffer) mesh.getBuffer(VertexBuffer.Type.Tangent).getData();
|
VertexBuffer tvb = mesh.getBuffer(VertexBuffer.Type.Tangent);
|
||||||
doTransformNorm(buf, bg.startIndex, bg.startIndex + bg.getVertexCount(), buf, bg.cachedOffsetMat);
|
FloatBuffer tanBuf = (FloatBuffer) tvb.getData();
|
||||||
mesh.getBuffer(VertexBuffer.Type.Tangent).updateData(buf);
|
doTransformsTangents(posBuf, normBuf, tanBuf, bg.startIndex, bg.startIndex + bg.getVertexCount(), bg.cachedOffsetMat);
|
||||||
|
tvb.updateData(tanBuf);
|
||||||
|
} else {
|
||||||
|
doTransforms(posBuf, normBuf, bg.startIndex, bg.startIndex + bg.getVertexCount(), bg.cachedOffsetMat);
|
||||||
}
|
}
|
||||||
|
pvb.updateData(posBuf);
|
||||||
|
nvb.updateData(normBuf);
|
||||||
|
|
||||||
|
|
||||||
batch.needMeshUpdate = true;
|
batch.needMeshUpdate = true;
|
||||||
}
|
}
|
||||||
@ -351,13 +356,16 @@ public class BatchNode extends Node implements Savable {
|
|||||||
int totalVerts = 0;
|
int totalVerts = 0;
|
||||||
int totalTris = 0;
|
int totalTris = 0;
|
||||||
int totalLodLevels = 0;
|
int totalLodLevels = 0;
|
||||||
|
int maxVertCount = 0;
|
||||||
|
|
||||||
Mesh.Mode mode = null;
|
Mesh.Mode mode = null;
|
||||||
for (Geometry geom : geometries) {
|
for (Geometry geom : geometries) {
|
||||||
totalVerts += geom.getVertexCount();
|
totalVerts += geom.getVertexCount();
|
||||||
totalTris += geom.getTriangleCount();
|
totalTris += geom.getTriangleCount();
|
||||||
totalLodLevels = Math.min(totalLodLevels, geom.getMesh().getNumLodLevels());
|
totalLodLevels = Math.min(totalLodLevels, geom.getMesh().getNumLodLevels());
|
||||||
|
if (maxVertCount < geom.getVertexCount()) {
|
||||||
|
maxVertCount = geom.getVertexCount();
|
||||||
|
}
|
||||||
Mesh.Mode listMode;
|
Mesh.Mode listMode;
|
||||||
int components;
|
int components;
|
||||||
switch (geom.getMesh().getMode()) {
|
switch (geom.getMesh().getMode()) {
|
||||||
@ -403,8 +411,6 @@ public class BatchNode extends Node implements Savable {
|
|||||||
formatForBuf[VertexBuffer.Type.Index.ordinal()] = VertexBuffer.Format.UnsignedShort;
|
formatForBuf[VertexBuffer.Type.Index.ordinal()] = VertexBuffer.Format.UnsignedShort;
|
||||||
}
|
}
|
||||||
|
|
||||||
int maxElemCount = 0;
|
|
||||||
int elements = 0;
|
|
||||||
// generate output buffers based on retrieved info
|
// generate output buffers based on retrieved info
|
||||||
for (int i = 0; i < compsForBuf.length; i++) {
|
for (int i = 0; i < compsForBuf.length; i++) {
|
||||||
if (compsForBuf[i] == 0) {
|
if (compsForBuf[i] == 0) {
|
||||||
@ -414,15 +420,10 @@ public class BatchNode extends Node implements Savable {
|
|||||||
Buffer data;
|
Buffer data;
|
||||||
if (i == VertexBuffer.Type.Index.ordinal()) {
|
if (i == VertexBuffer.Type.Index.ordinal()) {
|
||||||
data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalTris);
|
data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalTris);
|
||||||
elements = compsForBuf[i]* totalTris;
|
|
||||||
} else {
|
} else {
|
||||||
data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalVerts);
|
data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalVerts);
|
||||||
elements = compsForBuf[i]* totalVerts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(maxElemCount<elements){
|
|
||||||
maxElemCount = elements;
|
|
||||||
}
|
|
||||||
VertexBuffer vb = new VertexBuffer(VertexBuffer.Type.values()[i]);
|
VertexBuffer vb = new VertexBuffer(VertexBuffer.Type.values()[i]);
|
||||||
vb.setupData(VertexBuffer.Usage.Dynamic, compsForBuf[i], formatForBuf[i], data);
|
vb.setupData(VertexBuffer.Usage.Dynamic, compsForBuf[i], formatForBuf[i], data);
|
||||||
outMesh.setBuffer(vb);
|
outMesh.setBuffer(vb);
|
||||||
@ -478,68 +479,126 @@ public class BatchNode extends Node implements Savable {
|
|||||||
globalVertIndex += geomVertCount;
|
globalVertIndex += geomVertCount;
|
||||||
globalTriIndex += geomTriCount;
|
globalTriIndex += geomTriCount;
|
||||||
}
|
}
|
||||||
tmpFloat = new float[maxElemCount];
|
tmpFloat = new float[maxVertCount * 3];
|
||||||
|
tmpFloatN = new float[maxVertCount * 3];
|
||||||
|
tmpFloatT = new float[maxVertCount * 4];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void doTransforms(FloatBuffer bufPos, FloatBuffer bufNorm, int start, int end, Matrix4f transform) {
|
||||||
private void doTransformVerts(FloatBuffer inBuf, int start, int end, FloatBuffer outBuf, Matrix4f transform) {
|
|
||||||
TempVars vars = TempVars.get();
|
TempVars vars = TempVars.get();
|
||||||
Vector3f pos = vars.vect1;
|
Vector3f pos = vars.vect1;
|
||||||
|
Vector3f norm = vars.vect2;
|
||||||
|
|
||||||
int length = (end - start) * 3;
|
int length = (end - start) * 3;
|
||||||
|
|
||||||
// offset is given in element units
|
// offset is given in element units
|
||||||
// convert to be in component units
|
// convert to be in component units
|
||||||
int offset = start * 3;
|
int offset = start * 3;
|
||||||
|
bufPos.position(offset);
|
||||||
for (int i = start; i < end; i++) {
|
bufNorm.position(offset);
|
||||||
int index = i * 3;
|
bufPos.get(tmpFloat, 0, length);
|
||||||
pos.x = inBuf.get(index);
|
bufNorm.get(tmpFloatN, 0, length);
|
||||||
pos.y = inBuf.get(index + 1);
|
int index = 0;
|
||||||
pos.z = inBuf.get(index + 2);
|
while (index < length) {
|
||||||
|
pos.x = tmpFloat[index];
|
||||||
|
norm.x = tmpFloatN[index++];
|
||||||
|
pos.y = tmpFloat[index];
|
||||||
|
norm.y = tmpFloatN[index++];
|
||||||
|
pos.z = tmpFloat[index];
|
||||||
|
norm.z = tmpFloatN[index];
|
||||||
|
|
||||||
transform.mult(pos, pos);
|
transform.mult(pos, pos);
|
||||||
index -= offset;
|
transform.multNormal(norm, norm);
|
||||||
|
|
||||||
|
index -= 2;
|
||||||
tmpFloat[index] = pos.x;
|
tmpFloat[index] = pos.x;
|
||||||
tmpFloat[index + 1] = pos.y;
|
tmpFloatN[index++] = norm.x;
|
||||||
tmpFloat[index + 2] = pos.z;
|
tmpFloat[index] = pos.y;
|
||||||
|
tmpFloatN[index++] = norm.y;
|
||||||
|
tmpFloat[index] = pos.z;
|
||||||
|
tmpFloatN[index++] = norm.z;
|
||||||
|
|
||||||
}
|
}
|
||||||
vars.release();
|
vars.release();
|
||||||
outBuf.position(offset);
|
bufPos.position(offset);
|
||||||
//using bulk put as it's faster
|
//using bulk put as it's faster
|
||||||
outBuf.put(tmpFloat, 0, length);
|
bufPos.put(tmpFloat, 0, length);
|
||||||
|
bufNorm.position(offset);
|
||||||
|
//using bulk put as it's faster
|
||||||
|
bufNorm.put(tmpFloatN, 0, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doTransformNorm(FloatBuffer inBuf, int start, int end, FloatBuffer outBuf, Matrix4f transform) {
|
private void doTransformsTangents(FloatBuffer bufPos, FloatBuffer bufNorm, FloatBuffer bufTangents, int start, int end, Matrix4f transform) {
|
||||||
TempVars vars = TempVars.get();
|
TempVars vars = TempVars.get();
|
||||||
Vector3f pos = vars.vect1;
|
Vector3f pos = vars.vect1;
|
||||||
int length = (end - start) * 3;
|
Vector3f norm = vars.vect2;
|
||||||
|
Vector3f tan = vars.vect3;
|
||||||
|
|
||||||
|
int length = (end - start) * 3;
|
||||||
|
int tanLength = (end - start) * 4;
|
||||||
|
|
||||||
// offset is given in element units
|
// offset is given in element units
|
||||||
// convert to be in component units
|
// convert to be in component units
|
||||||
int offset = start * 3;
|
int offset = start * 3;
|
||||||
|
int tanOffset = start * 4;
|
||||||
|
|
||||||
for (int i = start; i < end; i++) {
|
bufPos.position(offset);
|
||||||
int index = i * 3;
|
bufNorm.position(offset);
|
||||||
pos.x = inBuf.get(index);
|
bufTangents.position(tanOffset);
|
||||||
pos.y = inBuf.get(index + 1);
|
bufPos.get(tmpFloat, 0, length);
|
||||||
pos.z = inBuf.get(index + 2);
|
bufNorm.get(tmpFloatN, 0, length);
|
||||||
|
bufTangents.get(tmpFloatT, 0, tanLength);
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
int tanIndex = 0;
|
||||||
|
while (index < length) {
|
||||||
|
pos.x = tmpFloat[index];
|
||||||
|
norm.x = tmpFloatN[index++];
|
||||||
|
pos.y = tmpFloat[index];
|
||||||
|
norm.y = tmpFloatN[index++];
|
||||||
|
pos.z = tmpFloat[index];
|
||||||
|
norm.z = tmpFloatN[index];
|
||||||
|
|
||||||
|
tan.x = tmpFloatT[tanIndex++];
|
||||||
|
tan.y = tmpFloatT[tanIndex++];
|
||||||
|
tan.z = tmpFloatT[tanIndex++];
|
||||||
|
|
||||||
|
|
||||||
|
transform.mult(pos, pos);
|
||||||
|
transform.multNormal(norm, norm);
|
||||||
|
transform.multNormal(tan, tan);
|
||||||
|
|
||||||
|
index -= 2;
|
||||||
|
tanIndex -= 3;
|
||||||
|
|
||||||
transform.multNormal(pos, pos);
|
|
||||||
index -= offset;
|
|
||||||
tmpFloat[index] = pos.x;
|
tmpFloat[index] = pos.x;
|
||||||
tmpFloat[index + 1] = pos.y;
|
tmpFloatN[index++] = norm.x;
|
||||||
tmpFloat[index + 2] = pos.z;
|
tmpFloat[index] = pos.y;
|
||||||
|
tmpFloatN[index++] = norm.y;
|
||||||
|
tmpFloat[index] = pos.z;
|
||||||
|
tmpFloatN[index++] = norm.z;
|
||||||
|
|
||||||
|
tmpFloatT[tanIndex++] = tan.x;
|
||||||
|
tmpFloatT[tanIndex++] = tan.y;
|
||||||
|
tmpFloatT[tanIndex++] = tan.z;
|
||||||
|
|
||||||
|
tanIndex++;
|
||||||
|
|
||||||
}
|
}
|
||||||
vars.release();
|
vars.release();
|
||||||
outBuf.position(offset);
|
bufPos.position(offset);
|
||||||
//using bulk put as it's faster
|
//using bulk put as it's faster
|
||||||
outBuf.put(tmpFloat, 0, length);
|
bufPos.put(tmpFloat, 0, length);
|
||||||
|
bufNorm.position(offset);
|
||||||
|
//using bulk put as it's faster
|
||||||
|
bufNorm.put(tmpFloatN, 0, length);
|
||||||
|
bufTangents.position(tanOffset);
|
||||||
|
//using bulk put as it's faster
|
||||||
|
bufTangents.put(tmpFloatT, 0, tanLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void doCopyBuffer(FloatBuffer inBuf, int offset, FloatBuffer outBuf) {
|
private void doCopyBuffer(FloatBuffer inBuf, int offset, FloatBuffer outBuf) {
|
||||||
TempVars vars = TempVars.get();
|
TempVars vars = TempVars.get();
|
||||||
Vector3f pos = vars.vect1;
|
Vector3f pos = vars.vect1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user