From: <sp...@us...> - 2011-07-12 13:29:14
|
Revision: 3571 http://java-game-lib.svn.sourceforge.net/java-game-lib/?rev=3571&view=rev Author: spasi Date: 2011-07-12 13:29:04 +0000 (Tue, 12 Jul 2011) Log Message: ----------- Mapped object code improvements: added license, additional documentation, reformatted code, now using LWJGLUtil.log instead of System.err. Added system properties for bytecode transformer debug output. (org.lwjgl.util.mapped.PrintTiming & org.lwjgl.util.mapped.PrintActivity) Added support for bounds checking the view of mapped objects. Enabled with org.lwjgl.util.mapped.Checks Added tests for mapped objects. (org.lwjgl.test.mapped package) Added "[LWJGL] " prefix to all LWJGL generated debug messages. Modified Paths: -------------- trunk/LWJGL/src/java/org/lwjgl/LWJGLUtil.java trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedField.java trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedForeach.java trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedHelper.java trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedObject.java trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedObjectClassLoader.java trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedObjectTransformer.java trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedObjectUnsafe.java trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedSet.java trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedSet2.java trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedSet3.java trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedSet4.java trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedType.java Added Paths: ----------- trunk/LWJGL/src/java/org/lwjgl/test/mapped/ trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedFloat.java trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectBench.java trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectTests1.java trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectTests2.java trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectTests3.java trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedSomething.java trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedVec2.java trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedVec3.java trunk/LWJGL/src/java/org/lwjgl/test/mapped/TestMappedObject.java Modified: trunk/LWJGL/src/java/org/lwjgl/LWJGLUtil.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/LWJGLUtil.java 2011-07-12 07:27:00 UTC (rev 3570) +++ trunk/LWJGL/src/java/org/lwjgl/LWJGLUtil.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -448,7 +448,7 @@ /** * Gets a boolean property as a privileged action. */ - private static boolean getPrivilegedBoolean(final String property_name) { + public static boolean getPrivilegedBoolean(final String property_name) { Boolean value = AccessController.doPrivileged(new PrivilegedAction<Boolean>() { public Boolean run() { return Boolean.getBoolean(property_name); @@ -464,7 +464,7 @@ */ public static void log(String msg) { if (DEBUG) { - System.err.println(msg); + System.err.println("[LWJGL] " + msg); } } Added: trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedFloat.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedFloat.java (rev 0) +++ trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedFloat.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.mapped; + +import org.lwjgl.util.mapped.MappedObject; +import org.lwjgl.util.mapped.MappedType; + +/** @author Riven */ +@MappedType(sizeof = 4) +public class MappedFloat extends MappedObject { + + public MappedFloat() { + this.test(); + } + + public float value; + + public void test() { + this.value = 4; + } + + @Override + public String toString() { + return "MappedFloat[" + value + "]"; + } + +} \ No newline at end of file Added: trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectBench.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectBench.java (rev 0) +++ trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectBench.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -0,0 +1,237 @@ +/* + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.mapped; + +import org.lwjgl.util.mapped.MappedObjectUnsafe; + +import java.nio.ByteBuffer; +import java.util.Arrays; + +import static org.lwjgl.util.mapped.MappedHelper.*; + +/** @author Riven */ +@SuppressWarnings("static-access") +public class MappedObjectBench { + + static class InstanceVec3 { + + float x, y, z; + + @Override + public String toString() { + return "InstanceVec3[" + x + ", " + y + ", " + z + "]"; + } + } + + static class ArrayVec3 { + + float[] a; + int i; + + @Override + public String toString() { + return "ArrayVec3[" + a[i * 3 + 0] + ", " + a[i * 3 + 1] + ", " + a[i * 3 + 2] + "]"; + } + } + + static void benchmarkInstances() { + final int runs = 64; + final int iterations = 64 * 1024; + + InstanceVec3 vec1 = new InstanceVec3(); + InstanceVec3 vec2 = new InstanceVec3(); + InstanceVec3 vec3 = new InstanceVec3(); + + long[] took = new long[runs]; + for ( int run = 0; run < runs; run++ ) { + long t0 = System.nanoTime(); + for ( int iteration = 0; iteration < iterations; iteration++ ) { + vec1.x = 13; + vec1.y += vec1.y * vec1.x + 0.3f; + vec1.z += vec2.y + vec1.x + 0.3f; + vec2.z += vec2.y + vec1.x; + vec3.z += vec2.z + vec1.y; + } + long t1 = System.nanoTime(); + took[run] = t1 - t0; + } + + Arrays.sort(took); + System.out.println("instance took: " + took[took.length / 2] / 1024 + "us"); + + System.out.println(vec1); + System.out.println(vec2); + System.out.println(vec3); + } + + static void benchmarkMapped() { + final int runs = 64; + final int iterations = 64 * 1024; + + ByteBuffer bb = ByteBuffer.allocateDirect(200); + + MappedVec3 vecs = MappedVec3.map(bb); + + MappedVec3 vec1 = vecs.dup(); + MappedVec3 vec2 = vecs.dup(); + MappedVec3 vec3 = vecs.dup(); + + vec1.view = 0; + vec2.view = 1; + vec3.view = 2; + + long[] took = new long[runs]; + for ( int run = 0; run < runs; run++ ) { + long t0 = System.nanoTime(); + for ( int iteration = 0; iteration < iterations; iteration += 2 ) { + vec1.x = 13; + vec1.y += vec1.y * vec1.x + 0.3f; + vec1.z += vec2.y + vec1.x + 0.3f; + vec2.z += vec2.y + vec1.x; + vec3.z += vec2.z + vec1.y; + + vec1.x = 13; + vec1.y += vec1.y * vec1.x + 0.3f; + vec1.z += vec2.y + vec1.x + 0.3f; + vec2.z += vec2.y + vec1.x; + vec3.z += vec2.z + vec1.y; + } + long t1 = System.nanoTime(); + took[run] = t1 - t0; + } + + Arrays.sort(took); + System.out.println("mapped took: " + took[took.length / 2] / 1024 + "us"); + + System.out.println(vec1); + System.out.println(vec2); + System.out.println(vec3); + + System.out.println(bb); + } + + static void benchmarkIndirectArray() { + final int runs = 64; + final int iterations = 64 * 1024; + + float[] bb = new float[200]; + + ArrayVec3 vec1 = new ArrayVec3(); + ArrayVec3 vec2 = new ArrayVec3(); + ArrayVec3 vec3 = new ArrayVec3(); + + vec1.a = bb; + vec2.a = bb; + vec3.a = bb; + + vec1.i = 0; + vec2.i = 1; + vec3.i = 2; + + long[] took = new long[runs]; + for ( int run = 0; run < runs; run++ ) { + long t0 = System.nanoTime(); + for ( int iteration = 0; iteration < iterations; iteration++ ) { + vec1.a[vec1.i * 3 + 0] = 13; + vec1.a[vec1.i * 3 + 1] += vec1.a[vec1.i * 3 + 1] * vec1.a[vec1.i * 3 + 0] + 0.3f; + vec1.a[vec1.i * 3 + 2] += vec2.a[vec2.i * 3 + 1] + vec1.a[vec1.i * 3 + 0] + 0.3f; + vec2.a[vec2.i * 3 + 2] += vec2.a[vec2.i * 3 + 1] + vec1.a[vec1.i * 3 + 0]; + vec3.a[vec3.i * 3 + 2] += vec2.a[vec2.i * 3 + 2] + vec2.a[vec2.i * 3 + 1]; + } + long t1 = System.nanoTime(); + took[run] = t1 - t0; + } + + Arrays.sort(took); + System.out.println("array took: " + took[took.length / 2] / 1024 + "us"); + + System.out.println(vec1); + System.out.println(vec2); + System.out.println(vec3); + + System.out.println(bb); + } + + static void benchmarkDirectArray() { + final int runs = 64; + final int iterations = 64 * 1024; + + float[] bb = new float[200]; + + long[] took = new long[runs]; + for ( int run = 0; run < runs; run++ ) { + long t0 = System.nanoTime(); + for ( int iteration = 0; iteration < iterations; iteration++ ) { + bb[1 * 3 + 0] = 13; + bb[1 * 3 + 1] += bb[1 * 3 + 1] * bb[1 * 3 + 0] + 0.3f; + bb[1 * 3 + 2] += bb[2 * 3 + 1] + bb[1 * 3 + 0] + 0.3f; + bb[2 * 3 + 2] += bb[2 * 3 + 1] + bb[1 * 3 + 0]; + bb[3 * 3 + 2] += bb[2 * 3 + 2] + bb[2 * 3 + 1]; + } + long t1 = System.nanoTime(); + took[run] = t1 - t0; + } + + Arrays.sort(took); + System.out.println("array2 took: " + took[took.length / 2] / 1024 + "us"); + + System.out.println(bb); + } + + static void benchmarkUnsafe() { + final int runs = 64; + final int iterations = 64 * 1024; + + ByteBuffer bb = ByteBuffer.allocateDirect(200); + long addr = MappedObjectUnsafe.getBufferBaseAddress(bb); + + long[] took = new long[runs]; + for ( int run = 0; run < runs; run++ ) { + long t0 = System.nanoTime(); + for ( int iteration = 0; iteration < iterations; iteration++ ) { + fput(13, addr + (1 * 3 + 0) * 4); + fput(fget(addr + (1 * 3 + 1) * 4) + fget(addr + (1 * 3 + 1) * 4) * fget(addr + (1 * 3 + 0) * 4) + 0.3f, addr + (1 * 3 + 1) * 4); + fput(fget(addr + (1 * 3 + 2) * 4) + fget(addr + (2 * 3 + 1) * 4) + fget(addr + (1 * 3 + 0) * 4) + 0.3f, addr + (1 * 3 + 2) * 4); + fput(fget(addr + (2 * 3 + 2) * 4) + fget(addr + (2 * 3 + 1) * 4) + fget(addr + (1 * 3 + 0) * 4), addr + (2 * 3 + 2) * 4); + fput(fget(addr + (3 * 3 + 2) * 4) + fget(addr + (2 * 3 + 2) * 4) + fget(addr + (2 * 3 + 1) * 4), addr + (3 * 3 + 2) * 4); + } + long t1 = System.nanoTime(); + took[run] = t1 - t0; + } + + Arrays.sort(took); + System.out.println("unsafe took: " + took[took.length / 2] / 1024 + "us"); + + System.out.println(bb); + } + +} \ No newline at end of file Added: trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectTests1.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectTests1.java (rev 0) +++ trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectTests1.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.mapped; + +import org.lwjgl.util.mapped.MappedHelper; +import org.lwjgl.util.mapped.MappedObjectUnsafe; + +import java.nio.ByteBuffer; + +/** @author Riven */ +@SuppressWarnings("static-access") +public class MappedObjectTests1 { + + static class Test { + + int value; + } + + static void testViewField() { + Test test = new Test(); + test.value = 13; + test.value += 1; + test.value++; + System.out.println(test); + + ByteBuffer bb = ByteBuffer.allocateDirect(200); + MappedFloat vecs = MappedFloat.map(bb); + + // verify 'malloc' and SIZEOF + { + MappedFloat vecs1 = MappedFloat.malloc(1234); + + assert (vecs1.stride == MappedFloat.SIZEOF); + assert (vecs1.stride * 1234 == vecs1.backingByteBuffer().capacity()); + assert (MappedFloat.SIZEOF * 1234 == vecs1.backingByteBuffer().capacity()); + } + + // manipulate 'mapped.value' + { + assert (vecs.value == 0.0f); // 4.0 is set in constructor, but runViewConstructor is not called + vecs.value = 1.1f; + assert (vecs.value == 1.1f); + } + + // manipulate 'view' with assignment + { + assert (vecs.view == 0); + vecs.view = 1; + assert (vecs.view == 1); + assert (vecs.value != 1.1f); // old view + vecs.view = 0; + } + + // manipulate 'view' with iinc + { + assert (vecs.view == 0); + vecs.view++; + assert (vecs.view == 1); + assert (vecs.value != 1.1f); // old view + vecs.view = 0; + } + + // test bound check + { + assert (vecs.view == 0); + try { + vecs.view = 50; + System.out.println("org.lwjgl.util.mapped.Checks is false or there is a bug in bounds checking."); + vecs.view = 0; + } catch (IndexOutOfBoundsException e) { + // expected, ignore + } + assert (vecs.view == 0); + } + + // test dup + { + int newStride = 16; + int doOffset = 0; + + vecs.view = 0; + MappedFloat.configure(vecs, newStride, doOffset); + MappedFloat dec2 = vecs.dup(); + MappedFloat.configure(dec2, newStride, doOffset); + + String s1 = vecs.baseAddress + "," + vecs.viewAddress + "," + vecs.stride + "," + vecs.SIZEOF; + String s2 = dec2.baseAddress + "," + dec2.viewAddress + "," + dec2.stride + "," + dec2.SIZEOF; + // System.out.println(s1); + // System.out.println(s2); + assert (s1.equals(s2)); + + dec2.view++; + + String s3 = vecs.baseAddress + "," + vecs.viewAddress + "," + vecs.stride + "," + vecs.SIZEOF; + String s4 = dec2.baseAddress + "," + dec2.viewAddress + "," + dec2.stride + "," + dec2.SIZEOF; + // System.out.println(s3); + // System.out.println(s4); + assert (!s3.equals(s4)); + } + + // test newBuffer + { + long addr1 = MappedObjectUnsafe.getBufferBaseAddress(bb); + ByteBuffer bb2 = MappedHelper.newBuffer(addr1, bb.capacity()); + long addr2 = MappedObjectUnsafe.getBufferBaseAddress(bb); + + System.out.println(bb); + System.out.println(bb2); + System.out.println(addr1); + System.out.println(addr2); + } + + // test 'copy' + { + vecs.value = 13.37f; + MappedFloat dec2 = vecs.dup(); + dec2.view = 1; + System.out.println(vecs); + System.out.println(dec2); + vecs.copyTo(dec2); + System.out.println(vecs); + System.out.println(dec2); + assert (dec2.value == 13.37f); + + vecs.value = 73.31f; + vecs.copyRange(dec2, 1); + assert (dec2.value == 73.31f); + } + } +} Added: trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectTests2.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectTests2.java (rev 0) +++ trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectTests2.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.mapped; + +import java.nio.ByteBuffer; + +/** @author Riven */ +@SuppressWarnings("static-access") +public class MappedObjectTests2 { + + static void testWriteFieldAccess(MappedVec3 vecs) { + // write access results into a transform-time IllegalAccessException + + System.out.println(vecs.baseAddress); // test read-access + + System.out.println(vecs.viewAddress); // test read-access + + System.out.println(vecs.align); // test read-access + + System.out.println(MappedVec3.SIZEOF); // test read-access + } + + static void testFields() { + ByteBuffer bb = ByteBuffer.allocateDirect(200); + MappedVec3 vecs = MappedVec3.map(bb); + + testWriteFieldAccess(vecs); + + vecs.x = 13.13f; + vecs.y = 14.14f; + vecs.z = 15.15f; + + System.out.println(vecs.viewAddress); + + assert (vecs.x == 13.13f); + assert (vecs.y == 14.14f); + assert (vecs.z == 15.15f); + + vecs.view = 0; + + assert (vecs.x == 13.13f); + assert (vecs.y == 14.14f); + assert (vecs.z == 15.15f); + + System.out.println(vecs); + vecs.view = 1; + System.out.println(vecs); + + assert (vecs.x == 0.0f); + assert (vecs.y == 0.0f); + assert (vecs.z == 0.0f); + + // now it becomes weird: offsets and strides + + vecs.view = 1; + vecs.x = 0.1234f; + vecs.view = 0; + + // test stride & sizeof + { + long a1 = vecs.viewAddress; + vecs.view = 1; + long a2 = vecs.viewAddress; + assert (a2 - a1 == MappedVec3.SIZEOF); + assert (a2 - a1 == vecs.stride); + vecs.view = 0; + } + + int newStride = 16; + + MappedVec3.configure(vecs, newStride, +4); + assert (vecs.z == 0.1234f); // vecs[1].x ended up in vecs[0].z due to 1float offset + + MappedVec3.configure(vecs, newStride, +8); + assert (vecs.z == 0.0000f); // vecs[1].z ended up in vecs[0].z due to 2float offset + + // test new stride + { + long a1 = vecs.viewAddress; + vecs.view = 1; + long a2 = vecs.viewAddress; + assert (a2 - a1 == newStride); + vecs.view = 0; + } + + // example: GPU => VBO => VTN + { + MappedVec3 v = MappedVec3.map(bb); + MappedVec2 t = MappedVec2.map(bb); + MappedVec3 n = MappedVec3.map(bb); + + int stride = MappedVec3.SIZEOF + MappedVec2.SIZEOF + MappedVec3.SIZEOF; + assert (stride == 32); + + MappedVec3.configure(v, stride, 0); + MappedVec2.configure(t, stride, MappedVec3.SIZEOF); + MappedVec3.configure(n, stride, MappedVec3.SIZEOF + MappedVec2.SIZEOF); + } + } + +} \ No newline at end of file Added: trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectTests3.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectTests3.java (rev 0) +++ trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedObjectTests3.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.mapped; + +import org.lwjgl.util.mapped.*; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import static org.lwjgl.util.mapped.MappedObject.*; + +/** @author Riven */ +@SuppressWarnings("static-access") +public class MappedObjectTests3 { + + static void testMappedBuffer() { + int elementCount = 4; + + MappedSomething some = MappedSomething.malloc(elementCount); + + assert (some.data != some.data); + + long addr1 = MappedObjectUnsafe.getBufferBaseAddress(some.backingByteBuffer()); + + ByteBuffer mapped = some.data; // creates new ByteBuffer instance + long addr2 = MappedObjectUnsafe.getBufferBaseAddress(mapped); + + assert (addr2 - addr1 == 4); + assert (mapped.capacity() == MappedSomething.SIZEOF - 4); + + some.view++; + mapped = some.data; // creates new ByteBuffer instance + + long addr3 = MappedObjectUnsafe.getBufferBaseAddress(mapped); + assert (addr3 - addr1 == 4 + MappedSomething.SIZEOF); + assert (addr3 - addr2 == 0 + MappedSomething.SIZEOF); + assert (mapped.capacity() == MappedSomething.SIZEOF - 4); + } + + static void testForeach() { + int elementCount = 4; + MappedSomething some = MappedSomething.malloc(elementCount); + + int i = 0; + for ( MappedSomething item : foreach(some, elementCount) ) { + assert (item.view == i++); + } + assert (some.view != elementCount); + System.out.println("current.view=" + some.view + ", not " + elementCount + ", as you might expect"); + } + + @MappedType(sizeof = 12) + public static class Xyz extends MappedObject { + + int x, y, z; + } + + static void testConstructor() { + int capacity = 1024; + ByteBuffer bb = ByteBuffer.allocateDirect(capacity).order(ByteOrder.nativeOrder()); + long address = MappedObjectUnsafe.getBufferBaseAddress(bb); + + MappedFloat mf = MappedFloat.map(address, capacity); + + assert (address == mf.baseAddress); + + assert (mf.value == 0.0f); + mf.view = 1; + assert (mf.value == 0.0f); + mf.runViewConstructor(); + assert (mf.value == 4.0f); + + Xyz.malloc(3); + } + + static void testMappedSet() { + MappedVec2 vec2 = MappedVec2.malloc(3); + MappedVec3 vec3 = MappedVec3.malloc(3); + + MappedSet2 set = MappedSet.create(vec2, vec3); + + assert (vec2.view == 0); + assert (vec3.view == 0); + + set.view = 2; + assert (vec2.view == 2); + assert (vec3.view == 2); + + set.view = 0; + assert (vec2.view == 0); + assert (vec3.view == 0); + } + +} \ No newline at end of file Added: trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedSomething.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedSomething.java (rev 0) +++ trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedSomething.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.mapped; + +import org.lwjgl.util.mapped.MappedField; +import org.lwjgl.util.mapped.MappedObject; +import org.lwjgl.util.mapped.MappedType; + +import java.nio.ByteBuffer; + +/** @author Riven */ +@MappedType(sizeof = 64) +public class MappedSomething extends MappedObject { + + @MappedField(byteOffset = 0) + public int used; + + @MappedField(byteOffset = 4, byteLength = 64 - 4) + public ByteBuffer data; + + @Override + public String toString() { + return "MappedSomething[" + used + "]"; + } + +} \ No newline at end of file Added: trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedVec2.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedVec2.java (rev 0) +++ trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedVec2.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.mapped; + +import org.lwjgl.util.mapped.MappedObject; +import org.lwjgl.util.mapped.MappedType; + +/** @author Riven */ +@MappedType(sizeof = 8) +public class MappedVec2 extends MappedObject { + + public float x; + + public float y; + + @Override + public String toString() { + return "MappedVec2[" + x + "," + y + "]"; + } + +} \ No newline at end of file Added: trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedVec3.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedVec3.java (rev 0) +++ trunk/LWJGL/src/java/org/lwjgl/test/mapped/MappedVec3.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.mapped; + +import org.lwjgl.util.mapped.MappedObject; +import org.lwjgl.util.mapped.MappedType; + +/** @author Riven */ +@MappedType(sizeof = 12) +public class MappedVec3 extends MappedObject { + + public float x; + + public float y; + + public float z; + + @Override + public String toString() { + return "[" + x + "," + y + "," + z + "]"; + } + +} \ No newline at end of file Added: trunk/LWJGL/src/java/org/lwjgl/test/mapped/TestMappedObject.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/test/mapped/TestMappedObject.java (rev 0) +++ trunk/LWJGL/src/java/org/lwjgl/test/mapped/TestMappedObject.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.lwjgl.test.mapped; + +import org.lwjgl.util.mapped.MappedObjectClassLoader; +import org.lwjgl.util.mapped.MappedObjectTransformer; + +/** @author Riven */ +public class TestMappedObject { + + static { + boolean assertsEnabled = false; + assert assertsEnabled = true; // Intentional side effect!!! + if ( !assertsEnabled ) + throw new RuntimeException("Asserts must be enabled for this test."); + } + + public static void main(String[] args) { + MappedObjectTransformer.register(MappedFloat.class); + MappedObjectTransformer.register(MappedVec2.class); + MappedObjectTransformer.register(MappedVec3.class); + MappedObjectTransformer.register(MappedSomething.class); + + MappedObjectTransformer.register(MappedObjectTests3.Xyz.class); + + if ( MappedObjectClassLoader.fork(TestMappedObject.class, args) ) { + return; + } + + MappedObjectTests1.testViewField(); + + MappedObjectTests2.testFields(); + + // MappedObjectBench.benchmarkMapped(); + // MappedObjectBench.benchmarkInstances(); + // MappedObjectBench.benchmarkIndirectArray(); + // MappedObjectBench.benchmarkDirectArray(); + // MappedObjectBench.benchmarkUnsafe(); + + MappedObjectTests3.testMappedBuffer(); + MappedObjectTests3.testForeach(); + MappedObjectTests3.testConstructor(); + MappedObjectTests3.testMappedSet(); + } + +} \ No newline at end of file Modified: trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedField.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedField.java 2011-07-12 07:27:00 UTC (rev 3570) +++ trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedField.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -1,19 +1,65 @@ /* - * Created on Jun 24, 2011 + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - package org.lwjgl.util.mapped; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; /** + * This annotation can be used on fields of {@link MappedType} classes, + * to manually specify byte offsets and lengths. This is useful when the + * mapped fields require custom alignment. {@link java.nio.ByteBuffer} + * fields are required to have this annotation with a hardcoded byte length. + * * @author Riven */ - @Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) public @interface MappedField { - long byteOffset(); - long byteLength() default -1; -} + /** + * Specifies the field byte offset within the mapped object. + * + * @return the field byte offset + */ + long byteOffset(); + + /** + * Specifies the field byte length. Required for {@link java.nio.ByteBuffer} fields. + * + * @return the field byte length + */ + long byteLength() default -1; + +} \ No newline at end of file Modified: trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedForeach.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedForeach.java 2011-07-12 07:27:00 UTC (rev 3570) +++ trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedForeach.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -1,52 +1,72 @@ /* - * Created on Jul 4, 2011 + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - package org.lwjgl.util.mapped; import java.util.Iterator; /** + * Iterable implementation for {@link MappedObject}. + * * @author Riven */ +public class MappedForeach<T extends MappedObject> implements Iterable<T> { -public class MappedForeach<T extends MappedObject> implements Iterable<T> -{ - final T mapped; - final int elementCount; + private final T mapped; + private final int elementCount; - MappedForeach(T mapped, int elementCount) - { - this.mapped = mapped; - this.elementCount = elementCount; - } + MappedForeach(T mapped, int elementCount) { + this.mapped = mapped; + this.elementCount = elementCount; + } - @Override - public Iterator<T> iterator() - { - return new Iterator<T>() - { - private int index = 0; + public Iterator<T> iterator() { + return new Iterator<T>() { - @Override - public boolean hasNext() - { - return this.index < (MappedForeach.this.elementCount); - } + private int index; - @Override - public T next() - { - mapped.viewAddress = mapped.baseAddress + (this.index++) * mapped.stride; + public boolean hasNext() { + return this.index < (MappedForeach.this.elementCount); + } - return mapped; - } + public T next() { + mapped.viewAddress = mapped.baseAddress + (this.index++) * mapped.stride; - @Override - public void remove() - { - throw new UnsupportedOperationException(); - } - }; - } -} + return mapped; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + +} \ No newline at end of file Modified: trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedHelper.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedHelper.java 2011-07-12 07:27:00 UTC (rev 3570) +++ trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedHelper.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -1,181 +1,188 @@ /* - * Created on Jun 28, 2011 + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - package org.lwjgl.util.mapped; import java.nio.ByteBuffer; /** + * [INTERNAL USE ONLY] + * <p/> + * Helper class used by the bytecode transformer. + * * @author Riven */ +public class MappedHelper { -public class MappedHelper -{ - public static void setup(MappedObject mo, ByteBuffer buffer, int align, int sizeof) - { - if (mo.baseAddress != 0L) - throw new IllegalStateException("this method should not be called by user-code"); + public static void setup(MappedObject mo, ByteBuffer buffer, int align, int sizeof) { + if ( mo.baseAddress != 0L ) + throw new IllegalStateException("this method should not be called by user-code"); - if (buffer == null) - throw new NullPointerException("buffer"); - if (!buffer.isDirect()) - throw new IllegalArgumentException("bytebuffer must be direct"); - mo.preventGC = buffer; + if ( buffer == null ) + throw new NullPointerException("buffer"); + if ( !buffer.isDirect() ) + throw new IllegalArgumentException("bytebuffer must be direct"); + mo.preventGC = buffer; - if (align <= 0) - throw new IllegalArgumentException("invalid alignment"); - mo.align = align; + if ( align <= 0 ) + throw new IllegalArgumentException("invalid alignment"); + mo.align = align; - if (sizeof % align != 0) - throw new IllegalStateException("sizeof not a multiple of alignment"); - mo.stride = sizeof; + if ( sizeof % align != 0 ) + throw new IllegalStateException("sizeof not a multiple of alignment"); + mo.stride = sizeof; - long addr = MappedObjectUnsafe.getBufferBaseAddress(buffer); - if (addr % align != 0) - throw new IllegalStateException("buffer address not aligned on " + align + " bytes"); + long addr = MappedObjectUnsafe.getBufferBaseAddress(buffer); + if ( addr % align != 0 ) + throw new IllegalStateException("buffer address not aligned on " + align + " bytes"); - mo.baseAddress = mo.viewAddress = addr; - } + mo.baseAddress = mo.viewAddress = addr; + } - public static void put_views(MappedSet2 set, int view) - { - set.view(view); - } + public static void put_views(MappedSet2 set, int view) { + set.view(view); + } - public static void put_views(MappedSet3 set, int view) - { - set.view(view); - } + public static void put_views(MappedSet3 set, int view) { + set.view(view); + } - public static void put_views(MappedSet4 set, int view) - { - set.view(view); - } + public static void put_views(MappedSet4 set, int view) { + set.view(view); + } - public static void put_view(MappedObject mapped, int view) - { - mapped.viewAddress = mapped.baseAddress + view * mapped.stride; - } + public static void put_view(MappedObject mapped, int view) { + mapped.setViewAddress(mapped.baseAddress + view * mapped.stride); + } - public static int get_view(MappedObject mapped) - { - return (int) (mapped.viewAddress - mapped.baseAddress) / mapped.stride; - } + public static int get_view(MappedObject mapped) { + return (int)(mapped.viewAddress - mapped.baseAddress) / mapped.stride; + } - public static MappedObject dup(MappedObject src, MappedObject dst) - { - dst.baseAddress = src.baseAddress; - dst.viewAddress = src.viewAddress; - dst.stride = src.stride; - dst.align = src.align; - dst.preventGC = src.preventGC; - return dst; - } + public static MappedObject dup(MappedObject src, MappedObject dst) { + dst.baseAddress = src.baseAddress; + dst.viewAddress = src.viewAddress; + dst.stride = src.stride; + dst.align = src.align; + dst.preventGC = src.preventGC; + return dst; + } - public static MappedObject slice(MappedObject src, MappedObject dst) - { - dst.baseAddress = src.viewAddress; // ! - dst.viewAddress = src.viewAddress; - dst.stride = src.stride; - dst.align = src.align; - dst.preventGC = src.preventGC; - return dst; - } + public static MappedObject slice(MappedObject src, MappedObject dst) { + dst.baseAddress = src.viewAddress; // ! + dst.viewAddress = src.viewAddress; + dst.stride = src.stride; + dst.align = src.align; + dst.preventGC = src.preventGC; + return dst; + } - public static void copy(MappedObject src, MappedObject dst, int bytes) - { - MappedObjectUnsafe.INSTANCE.copyMemory(src.viewAddress, dst.viewAddress, bytes); - } + public static void copy(MappedObject src, MappedObject dst, int bytes) { + MappedObjectUnsafe.INSTANCE.copyMemory(src.viewAddress, dst.viewAddress, bytes); + } - public static ByteBuffer newBuffer(long address, int capacity) - { - return MappedObjectUnsafe.newBuffer(address, capacity); - } + public static ByteBuffer newBuffer(long address, int capacity) { + return MappedObjectUnsafe.newBuffer(address, capacity); + } - // ---- primitive fields read/write + // ---- primitive fields read/write - // byte + // byte - public static void bput(byte value, long addr) - { - MappedObjectUnsafe.INSTANCE.putByte(addr, value); - } + public static void bput(byte value, long addr) { + MappedObjectUnsafe.INSTANCE.putByte(addr, value); + } - public static byte bget(long addr) - { - return MappedObjectUnsafe.INSTANCE.getByte(addr); - } + public static byte bget(long addr) { + return MappedObjectUnsafe.INSTANCE.getByte(addr); + } - // short + // short - public static void sput(short value, long addr) - { - MappedObjectUnsafe.INSTANCE.putShort(addr, value); - } + public static void sput(short value, long addr) { + MappedObjectUnsafe.INSTANCE.putShort(addr, value); + } - public static short sget(long addr) - { - return MappedObjectUnsafe.INSTANCE.getShort(addr); - } + public static short sget(long addr) { + return MappedObjectUnsafe.INSTANCE.getShort(addr); + } - // char + // char - public static void cput(char value, long addr) - { - MappedObjectUnsafe.INSTANCE.putChar(addr, value); - } + public static void cput(char value, long addr) { + MappedObjectUnsafe.INSTANCE.putChar(addr, value); + } - public static char cget(long addr) - { - return MappedObjectUnsafe.INSTANCE.getChar(addr); - } + public static char cget(long addr) { + return MappedObjectUnsafe.INSTANCE.getChar(addr); + } - // int + // int - public static void iput(int value, long addr) - { - MappedObjectUnsafe.INSTANCE.putInt(addr, value); - } + public static void iput(int value, long addr) { + MappedObjectUnsafe.INSTANCE.putInt(addr, value); + } - public static int iget(long addr) - { - return MappedObjectUnsafe.INSTANCE.getInt(addr); - } + public static int iget(long addr) { + return MappedObjectUnsafe.INSTANCE.getInt(addr); + } - // float + // float - public static void fput(float value, long addr) - { - MappedObjectUnsafe.INSTANCE.putFloat(addr, value); - } + public static void fput(float value, long addr) { + MappedObjectUnsafe.INSTANCE.putFloat(addr, value); + } - public static float fget(long addr) - { - return MappedObjectUnsafe.INSTANCE.getFloat(addr); - } + public static float fget(long addr) { + return MappedObjectUnsafe.INSTANCE.getFloat(addr); + } - // long + // long - public static void jput(long value, long addr) - { - MappedObjectUnsafe.INSTANCE.putLong(addr, value); - } + public static void jput(long value, long addr) { + MappedObjectUnsafe.INSTANCE.putLong(addr, value); + } - public static long jget(long addr) - { - return MappedObjectUnsafe.INSTANCE.getLong(addr); - } + public static long jget(long addr) { + return MappedObjectUnsafe.INSTANCE.getLong(addr); + } - // double + // double - public static void dput(double value, long addr) - { - MappedObjectUnsafe.INSTANCE.putDouble(addr, value); - } + public static void dput(double value, long addr) { + MappedObjectUnsafe.INSTANCE.putDouble(addr, value); + } - public static double dget(long addr) - { - return MappedObjectUnsafe.INSTANCE.getDouble(addr); - } + public static double dget(long addr) { + return MappedObjectUnsafe.INSTANCE.getDouble(addr); + } + } \ No newline at end of file Modified: trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedObject.java =================================================================== --- trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedObject.java 2011-07-12 07:27:00 UTC (rev 3570) +++ trunk/LWJGL/src/java/org/lwjgl/util/mapped/MappedObject.java 2011-07-12 13:29:04 UTC (rev 3571) @@ -1,185 +1,248 @@ /* - * Created on Jun 25, 2011 + * Copyright (c) 2002-2011 LWJGL Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of 'LWJGL' nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - package org.lwjgl.util.mapped; +import org.lwjgl.LWJGLUtil; + import java.nio.ByteBuffer; /** + * Base superclass of all mapped objects. Classes that require + * data mapping should extend this class and also be annotated + * with {@link MappedType}. + * <p/> + * Subclasses may only specify the default constructor. Any code + * inside that constructor is optional, but will not run when the + * view is instantiated, see {@link #runViewConstructor()}. + * <p/> + * Bounds checking may be enabled through a JVM system property: org.lwjgl.util.mapped.Checks=true + * * @author Riven */ +public class MappedObject { -public class MappedObject -{ - public MappedObject() - { - // - } + static final boolean CHECKS = LWJGLUtil.getPrivilegedBoolean("org.lwjgl.util.mapped.Checks"); - // these fields are not assignable/writable by user-code - public long baseAddress; - public long viewAddress; - public int stride; - public int align; + public MappedObject() { + // + } - /** - * Holds the value of sizeof of the sub-type of this MappedObject<br> - * <br> - * The behavior of this (transformed) method does not follow the normal Java behavior.<br> - * <code>Vec2.SIZEOF</code> will yield 8 (2 floats)<br> - * <code>Vec3.SIZEOF</code> will yield 12 (3 floats)<br> - * This (required) notation might cause compiler warnings, which can be suppressed with @SuppressWarnings("static-access").<br> - * Using Java 5.0's static-import on this method will break functionality. - */ + /** The mapped object base memory address, in bytes. Read-only. */ + public long baseAddress; - // any method that calls these field will have its call-site modified - public static int SIZEOF = -1; // 'final' per subtype - public int view; // read/write + /** The mapped object view memory address, in bytes. Read-only. */ + public long viewAddress; - public final void next() - { - this.viewAddress += this.stride; - } + /** The mapped object stride, in bytes. Read-only. */ + public int stride; - /** - * Creates a MappedObject instance, mapping the memory region of the specified direct ByteBuffer.<br> - * <br> - * The behavior of this (transformed) method does not follow the normal Java behavior.<br> - * <code>Vec2.map(buffer)</code> will return a mapped Vec2 instance.<br> - * <code>Vec3.map(buffer)</code> will return a mapped Vec3 instance.<br> - * This (required) notation might cause compiler warnings, which can be suppressed with @SuppressWarnings("static-access").<br> - * Using Java 5.0's static-import on this method will break functionality. - */ + /** The mapped object memory alignment, in bytes. Read-only. */ + public int align; - @SuppressWarnings("unused") - public static <T extends MappedObject> T map(ByteBuffer bb) - { - // any method that calls this method will have its call-site modified - throw new InternalError("type not registered"); - } + /** + * Holds the value of sizeof of the sub-type of this MappedObject<br> + * <br> + * The behavior of this (transformed) method does not follow the normal Java behavior.<br> + * <code>Vec2.SIZEOF</code> will yield 8 (2 floats)<br> + * <code>Vec3.SIZEOF</code> will yield 12 (3 floats)<br> + * This (required) notation might cause compiler warnings, which can be suppressed with @SuppressWarnings("static-access").<br> + * Using Java 5.0's static-import on this method will break functionality. + */ + public static int SIZEOF = -1; // any method that calls these field will have its call-site modified ('final' per subtype) - @SuppressWarnings("unused") - public static <T extends MappedObject> T map(long address, int capacity) - { - // any method that calls this method will have its call-site modified - throw new InternalError("ty... [truncated message content] |