예제 #1
0
  @Test
  public void createBufferTest() {

    out.println(" - - - highLevelTest; create buffer test - - - ");

    final CLContext context = CLContext.create();
    try {
      final int size = 6;

      final CLBuffer<ByteBuffer> bb = context.createByteBuffer(size);
      final CLBuffer<ShortBuffer> sb = context.createShortBuffer(size);
      final CLBuffer<IntBuffer> ib = context.createIntBuffer(size);
      final CLBuffer<LongBuffer> lb = context.createLongBuffer(size);
      final CLBuffer<FloatBuffer> fb = context.createFloatBuffer(size);
      final CLBuffer<DoubleBuffer> db = context.createDoubleBuffer(size);

      final List<CLMemory<? extends Buffer>> buffers = context.getMemoryObjects();
      assertEquals(6, buffers.size());

      assertEquals(1, bb.getElementSize());
      assertEquals(2, sb.getElementSize());
      assertEquals(4, ib.getElementSize());
      assertEquals(8, lb.getElementSize());
      assertEquals(4, fb.getElementSize());
      assertEquals(8, db.getElementSize());

      final ByteBuffer anotherNIO = newDirectByteBuffer(2);

      for (final CLMemory<? extends Buffer> memory : buffers) {

        final CLBuffer<? extends Buffer> buffer = (CLBuffer<? extends Buffer>) memory;
        final Buffer nio = buffer.getBuffer();

        assertEquals(nio.capacity(), buffer.getCLCapacity());
        assertEquals(buffer.getNIOSize(), buffer.getCLSize());
        assertEquals(sizeOfBufferElem(nio), buffer.getElementSize());
        assertEquals(nio.capacity() * sizeOfBufferElem(nio), buffer.getCLSize());

        final CLBuffer<ByteBuffer> clone = buffer.cloneWith(anotherNIO);

        assertEquals(buffer.ID, clone.ID);
        assertTrue(clone.equals(buffer));
        assertTrue(buffer.equals(clone));

        assertEquals(buffer.getCLSize(), clone.getCLCapacity());
        assertEquals(buffer.getCLSize(), clone.getCLSize());
        assertEquals(anotherNIO.capacity(), clone.getNIOCapacity());
      }

    } finally {
      context.release();
    }
  }
예제 #2
0
  /**
   * Allocates hardware buffers on the graphics card and fills them with data if a buffer has not
   * already been previously allocated. Note that this function uses the GL_OES_vertex_buffer_object
   * extension, which is not guaranteed to be supported on every device.
   *
   * @param gl A pointer to the OpenGL ES context.
   */
  public void generateHardwareBuffers(GL10 gl) {
    if (!mUseHardwareBuffers) {
      if (gl instanceof GL11) {
        GL11 gl11 = (GL11) gl;
        int[] buffer = new int[1];

        // Allocate and fill the vertex buffer.
        gl11.glGenBuffers(1, buffer, 0);
        mVertBufferIndex = buffer[0];
        gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mVertBufferIndex);
        final int vertexSize = mVertexBuffer.capacity() * mCoordinateSize;
        gl11.glBufferData(GL11.GL_ARRAY_BUFFER, vertexSize, mVertexBuffer, GL11.GL_STATIC_DRAW);

        // Allocate and fill the texture coordinate buffer.
        gl11.glGenBuffers(1, buffer, 0);
        mTextureCoordBufferIndex = buffer[0];
        gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mTextureCoordBufferIndex);
        final int texCoordSize = mTexCoordBuffer.capacity() * mCoordinateSize;
        gl11.glBufferData(GL11.GL_ARRAY_BUFFER, texCoordSize, mTexCoordBuffer, GL11.GL_STATIC_DRAW);

        // Allocate and fill the color buffer.
        gl11.glGenBuffers(1, buffer, 0);
        mColorBufferIndex = buffer[0];
        gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mColorBufferIndex);
        final int colorSize = mColorBuffer.capacity() * mCoordinateSize;
        gl11.glBufferData(GL11.GL_ARRAY_BUFFER, colorSize, mColorBuffer, GL11.GL_STATIC_DRAW);

        // Unbind the array buffer.
        gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);

        // Allocate and fill the index buffer.
        gl11.glGenBuffers(1, buffer, 0);
        mIndexBufferIndex = buffer[0];
        gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, mIndexBufferIndex);
        // A char is 2 bytes.
        final int indexSize = mIndexBuffer.capacity() * 2;
        gl11.glBufferData(
            GL11.GL_ELEMENT_ARRAY_BUFFER, indexSize, mIndexBuffer, GL11.GL_STATIC_DRAW);

        // Unbind the element array buffer.
        gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);

        mUseHardwareBuffers = true;

        assert mVertBufferIndex != 0;
        assert mTextureCoordBufferIndex != 0;
        assert mIndexBufferIndex != 0;
        assert gl11.glGetError() == 0;
      }
    }
  }
예제 #3
0
  private static void convertTexCoords2D(FloatBuffer input, Buffer output) {
    if (output.capacity() < input.capacity())
      throw new RuntimeException("Output must be at least as large as input!");

    input.clear();
    output.clear();
    Vector2f temp = new Vector2f();
    int vertexCount = input.capacity() / 2;

    ShortBuffer sb = null;
    IntBuffer ib = null;

    if (output instanceof ShortBuffer) sb = (ShortBuffer) output;
    else if (output instanceof IntBuffer) ib = (IntBuffer) output;
    else throw new UnsupportedOperationException();

    for (int i = 0; i < vertexCount; i++) {
      BufferUtils.populateFromBuffer(temp, input, i);

      if (sb != null) {
        sb.put((short) (temp.getX() * Short.MAX_VALUE));
        sb.put((short) (temp.getY() * Short.MAX_VALUE));
      } else {
        int v1 = (int) (temp.getX() * ((float) (1 << 16)));
        int v2 = (int) (temp.getY() * ((float) (1 << 16)));
        ib.put(v1).put(v2);
      }
    }
  }
예제 #4
0
  private static Transform convertPositions(FloatBuffer input, BoundingBox bbox, Buffer output) {
    if (output.capacity() < input.capacity())
      throw new RuntimeException("Output must be at least as large as input!");

    Vector3f offset = bbox.getCenter().negate();
    Vector3f size = new Vector3f(bbox.getXExtent(), bbox.getYExtent(), bbox.getZExtent());
    size.multLocal(2);

    ShortBuffer sb = null;
    ByteBuffer bb = null;
    float dataTypeSize;
    float dataTypeOffset;
    if (output instanceof ShortBuffer) {
      sb = (ShortBuffer) output;
      dataTypeOffset = shortOff;
      dataTypeSize = shortSize;
    } else {
      bb = (ByteBuffer) output;
      dataTypeOffset = byteOff;
      dataTypeSize = byteSize;
    }
    Vector3f scale = new Vector3f();
    scale.set(dataTypeSize, dataTypeSize, dataTypeSize).divideLocal(size);

    Vector3f invScale = new Vector3f();
    invScale.set(size).divideLocal(dataTypeSize);

    offset.multLocal(scale);
    offset.addLocal(dataTypeOffset, dataTypeOffset, dataTypeOffset);

    // offset = (-modelOffset * shortSize)/modelSize + shortOff
    // scale = shortSize / modelSize

    input.clear();
    output.clear();
    Vector3f temp = new Vector3f();
    int vertexCount = input.capacity() / 3;
    for (int i = 0; i < vertexCount; i++) {
      BufferUtils.populateFromBuffer(temp, input, i);

      // offset and scale vector into -32768 ... 32767
      // or into -128 ... 127 if using bytes
      temp.multLocal(scale);
      temp.addLocal(offset);

      // quantize and store
      if (sb != null) {
        short v1 = (short) temp.getX();
        short v2 = (short) temp.getY();
        short v3 = (short) temp.getZ();
        sb.put(v1).put(v2).put(v3);
      } else {
        byte v1 = (byte) temp.getX();
        byte v2 = (byte) temp.getY();
        byte v3 = (byte) temp.getZ();
        bb.put(v1).put(v2).put(v3);
      }
    }

    Transform transform = new Transform();
    transform.setTranslation(offset.negate().multLocal(invScale));
    transform.setScale(invScale);
    return transform;
  }
예제 #5
0
 protected static int estimatedMemorySize(Buffer buffer) {
   if (buffer == null) {
     return 0;
   }
   return buffer.capacity() * GLBuffers.sizeOfBufferElem(buffer);
 }
예제 #6
0
 void _reset(Buffer b) {
   b.position(0);
   b.limit(b.capacity());
 }