use of java.nio.ShortBuffer in project libgdx by libgdx.
the class BufferUtilsTest method create.
@Override
public void create() {
//Not emulated in gwt
//ByteBuffer bytebuffer = BufferUtils.newUnsafeByteBuffer(1000 * 1000);
//BufferUtils.disposeUnsafeByteBuffer(bytebuffer);
ByteBuffer bb = BufferUtils.newByteBuffer(8);
CharBuffer cb = BufferUtils.newCharBuffer(8);
ShortBuffer sb = BufferUtils.newShortBuffer(8);
IntBuffer ib = BufferUtils.newIntBuffer(8);
LongBuffer lb = BufferUtils.newLongBuffer(8);
FloatBuffer fb = BufferUtils.newFloatBuffer(8);
DoubleBuffer db = BufferUtils.newDoubleBuffer(8);
bb.position(4);
BufferUtils.copy(new byte[] { 1, 2, 3, 4 }, 0, bb, 4);
checkInt(bb.get(), 1);
checkInt(bb.get(), 2);
checkInt(bb.get(), 3);
checkInt(bb.get(), 4);
cb.position(4);
BufferUtils.copy(new char[] { 1, 2, 3, 4 }, 0, cb, 4);
checkInt(cb.get(), 1);
checkInt(cb.get(), 2);
checkInt(cb.get(), 3);
checkInt(cb.get(), 4);
cb.position(0);
BufferUtils.copy(new char[] { 5, 6, 7, 8 }, 1, cb, 3);
checkInt(cb.get(), 6);
checkInt(cb.get(), 7);
checkInt(cb.get(), 8);
sb.position(4);
BufferUtils.copy(new short[] { 1, 2, 3, 4 }, 0, sb, 4);
checkInt(sb.get(), 1);
checkInt(sb.get(), 2);
checkInt(sb.get(), 3);
checkInt(sb.get(), 4);
sb.position(0);
BufferUtils.copy(new short[] { 5, 6, 7, 8 }, 1, sb, 3);
checkInt(sb.get(), 6);
checkInt(sb.get(), 7);
checkInt(sb.get(), 8);
ib.position(4);
BufferUtils.copy(new int[] { 1, 2, 3, 4 }, 0, ib, 4);
checkInt(ib.get(), 1);
checkInt(ib.get(), 2);
checkInt(ib.get(), 3);
checkInt(ib.get(), 4);
ib.position(0);
BufferUtils.copy(new int[] { 5, 6, 7, 8 }, 1, ib, 3);
checkInt(ib.get(), 6);
checkInt(ib.get(), 7);
checkInt(ib.get(), 8);
lb.position(4);
BufferUtils.copy(new long[] { 1, 2, 3, 4 }, 0, lb, 4);
checkInt(lb.get(), 1);
checkInt(lb.get(), 2);
checkInt(lb.get(), 3);
checkInt(lb.get(), 4);
lb.position(0);
BufferUtils.copy(new long[] { 5, 6, 7, 8 }, 1, lb, 3);
checkInt(lb.get(), 6);
checkInt(lb.get(), 7);
checkInt(lb.get(), 8);
fb.position(4);
BufferUtils.copy(new float[] { 1, 2, 3, 4 }, 0, fb, 4);
checkFloat(fb.get(), 1);
checkFloat(fb.get(), 2);
checkFloat(fb.get(), 3);
checkFloat(fb.get(), 4);
fb.position(0);
BufferUtils.copy(new float[] { 5, 6, 7, 8 }, 1, fb, 3);
checkFloat(fb.get(), 6);
checkFloat(fb.get(), 7);
checkFloat(fb.get(), 8);
if (Gdx.app.getType() != ApplicationType.WebGL) {
// gwt throws: NYI: Numbers.doubleToRawLongBits
db.position(4);
BufferUtils.copy(new double[] { 1, 2, 3, 4 }, 0, db, 4);
checkFloat(db.get(), 1);
checkFloat(db.get(), 2);
checkFloat(db.get(), 3);
checkFloat(db.get(), 4);
db.position(0);
BufferUtils.copy(new double[] { 5, 6, 7, 8 }, 1, db, 3);
checkFloat(db.get(), 6);
checkFloat(db.get(), 7);
checkFloat(db.get(), 8);
}
ByteBuffer bb2 = BufferUtils.newByteBuffer(4);
bb.position(4);
BufferUtils.copy(bb, bb2, 4);
checkInt(bb2.get(), 1);
checkInt(bb2.get(), 2);
checkInt(bb2.get(), 3);
checkInt(bb2.get(), 4);
bench();
}
use of java.nio.ShortBuffer in project libgdx by libgdx.
the class BufferUtilsTest method benchShort.
private void benchShort() {
ShortBuffer sb = BufferUtils.newShortBuffer(1024 * 1024 / 2);
short[] shorts = new short[1024 * 1024 / 2];
int len = shorts.length;
// relative put
long start = TimeUtils.nanoTime();
for (int j = 0; j < NUM_MB; j++) {
sb.clear();
for (int i = 0; i < len; i++) sb.put(shorts[i]);
}
Gdx.app.log("BufferUtilsTest", "ShortBuffer relative put: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
// absolute put
start = TimeUtils.nanoTime();
for (int j = 0; j < NUM_MB; j++) {
sb.clear();
for (int i = 0; i < len; i++) sb.put(i, shorts[i]);
}
Gdx.app.log("BufferUtilsTest", "ShortBuffer absolute put: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
// bulk put
start = TimeUtils.nanoTime();
for (int j = 0; j < NUM_MB; j++) {
sb.clear();
sb.put(shorts);
}
Gdx.app.log("BufferUtilsTest", "ShortBuffer bulk put: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
// JNI put
start = TimeUtils.nanoTime();
for (int j = 0; j < NUM_MB; j++) {
sb.clear();
BufferUtils.copy(shorts, 0, sb, len);
}
Gdx.app.log("BufferUtilsTest", "ShortBuffer native bulk put: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
}
use of java.nio.ShortBuffer in project libgdx by libgdx.
the class TriangleRaycastTest method tap.
@Override
public boolean tap(float screenX, float screenY, int count, int button) {
Ray ray = camera.getPickRay(screenX, screenY);
rayFrom.set(ray.origin);
rayTo.set(ray.direction).scl(100).add(rayFrom);
// Clean the callback object for reuse.
triangleRaycastCallback.setHitFraction(1);
triangleRaycastCallback.clearReport();
triangleRaycastCallback.setFrom(rayFrom);
triangleRaycastCallback.setTo(rayTo);
// Ray casting is performed directly on the collision shape.
// The callback specifies the intersected MeshPart as well as triangle.
triangleShape.performRaycast(triangleRaycastCallback, rayFrom, rayTo);
int currentTriangleIndex = triangleRaycastCallback.triangleIndex;
int currentPartId = triangleRaycastCallback.partId;
if (currentTriangleIndex == -1 || currentPartId == -1) {
// No intersection was found.
return false;
}
// Get the position coordinates of the vertices belonging to intersected triangle.
Mesh mesh = model.meshParts.get(currentPartId).mesh;
FloatBuffer verticesBuffer = mesh.getVerticesBuffer();
ShortBuffer indicesBuffer = mesh.getIndicesBuffer();
int posOffset = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Position).offset / 4;
int vertexSize = mesh.getVertexSize() / 4;
int currentTriangleFirstVertexIndex = currentTriangleIndex * 3;
// Store the three vertices belonging to the selected triangle.
for (int i = 0; i < 3; i++) {
int currentVertexIndex = indicesBuffer.get(currentTriangleFirstVertexIndex + i);
int j = currentVertexIndex * vertexSize + posOffset;
float x = verticesBuffer.get(j++);
float y = verticesBuffer.get(j++);
float z = verticesBuffer.get(j);
selectedTriangleVertices[i].set(x, y, z);
}
return true;
}
use of java.nio.ShortBuffer in project bigbluebutton by bigbluebutton.
the class Frame method createIndexer.
/** Returns an {@link Indexer} for the <i>i</i>th image plane. */
public <I extends Indexer> I createIndexer(boolean direct, int i) {
long[] sizes = { imageHeight, imageWidth, imageChannels };
long[] strides = { imageStride, imageChannels, 1 };
Buffer buffer = image[i];
Object array = buffer.hasArray() ? buffer.array() : null;
switch(imageDepth) {
case DEPTH_UBYTE:
return array != null ? (I) UByteIndexer.create((byte[]) array, sizes, strides) : direct ? (I) UByteIndexer.create((ByteBuffer) buffer, sizes, strides) : (I) UByteIndexer.create(new BytePointer((ByteBuffer) buffer), sizes, strides, false);
case DEPTH_BYTE:
return array != null ? (I) ByteIndexer.create((byte[]) array, sizes, strides) : direct ? (I) ByteIndexer.create((ByteBuffer) buffer, sizes, strides) : (I) ByteIndexer.create(new BytePointer((ByteBuffer) buffer), sizes, strides, false);
case DEPTH_USHORT:
return array != null ? (I) UShortIndexer.create((short[]) array, sizes, strides) : direct ? (I) UShortIndexer.create((ShortBuffer) buffer, sizes, strides) : (I) UShortIndexer.create(new ShortPointer((ShortBuffer) buffer), sizes, strides, false);
case DEPTH_SHORT:
return array != null ? (I) ShortIndexer.create((short[]) array, sizes, strides) : direct ? (I) ShortIndexer.create((ShortBuffer) buffer, sizes, strides) : (I) ShortIndexer.create(new ShortPointer((ShortBuffer) buffer), sizes, strides, false);
case DEPTH_INT:
return array != null ? (I) IntIndexer.create((int[]) array, sizes, strides) : direct ? (I) IntIndexer.create((IntBuffer) buffer, sizes, strides) : (I) IntIndexer.create(new IntPointer((IntBuffer) buffer), sizes, strides, false);
case DEPTH_LONG:
return array != null ? (I) LongIndexer.create((long[]) array, sizes, strides) : direct ? (I) LongIndexer.create((LongBuffer) buffer, sizes, strides) : (I) LongIndexer.create(new LongPointer((LongBuffer) buffer), sizes, strides, false);
case DEPTH_FLOAT:
return array != null ? (I) FloatIndexer.create((float[]) array, sizes, strides) : direct ? (I) FloatIndexer.create((FloatBuffer) buffer, sizes, strides) : (I) FloatIndexer.create(new FloatPointer((FloatBuffer) buffer), sizes, strides, false);
case DEPTH_DOUBLE:
return array != null ? (I) DoubleIndexer.create((double[]) array, sizes, strides) : direct ? (I) DoubleIndexer.create((DoubleBuffer) buffer, sizes, strides) : (I) DoubleIndexer.create(new DoublePointer((DoubleBuffer) buffer), sizes, strides, false);
default:
assert false;
}
return null;
}
use of java.nio.ShortBuffer in project processing by processing.
the class PGL method allocateShortBuffer.
protected static ShortBuffer allocateShortBuffer(short[] arr) {
if (USE_DIRECT_BUFFERS) {
ShortBuffer buf = allocateDirectShortBuffer(arr.length);
buf.put(arr);
buf.position(0);
return buf;
} else {
return ShortBuffer.wrap(arr);
}
}
Aggregations