public void glVertexAttribPointer( int indx, int size, int type, boolean normalized, int stride, Buffer buffer) { if (buffer instanceof ByteBuffer) { if (type == GL_BYTE) GL20.glVertexAttribPointer(indx, size, false, normalized, stride, (ByteBuffer) buffer); else if (type == GL_UNSIGNED_BYTE) GL20.glVertexAttribPointer(indx, size, true, normalized, stride, (ByteBuffer) buffer); else if (type == GL_SHORT) GL20.glVertexAttribPointer( indx, size, false, normalized, stride, ((ByteBuffer) buffer).asShortBuffer()); else if (type == GL_UNSIGNED_SHORT) GL20.glVertexAttribPointer( indx, size, true, normalized, stride, ((ByteBuffer) buffer).asShortBuffer()); else if (type == GL_FLOAT) GL20.glVertexAttribPointer( indx, size, normalized, stride, ((ByteBuffer) buffer).asFloatBuffer()); else throw new GdxRuntimeException( "Can't use " + buffer.getClass().getName() + " with type " + type + " with this method. Use ByteBuffer and one of GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT or GL_FLOAT for type. Blame LWJGL"); } else throw new GdxRuntimeException( "Can't use " + buffer.getClass().getName() + " with this method. Use ByteBuffer instead. Blame LWJGL"); }
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); } } }
/** * Copies numFloats floats from src starting at offset to dst. Dst is assumed to be a direct * {@link Buffer}. The method will crash if that is not the case. The position and limit of the * buffer are ignored, the copy is placed at position 0 in the buffer. After the copying process * the position of the buffer is set to 0 and its limit is set to numFloats * 4 if it is a * ByteBuffer and numFloats if it is a FloatBuffer. In case the Buffer is neither a ByteBuffer nor * a FloatBuffer the limit is not set. This is an expert method, use at your own risk. * * @param src the source array * @param dst the destination buffer, has to be a direct Buffer * @param numFloats the number of floats to copy * @param offset the offset in src to start copying from */ public static void copy(float[] src, Buffer dst, int numFloats, int offset) { copyJni(src, dst, numFloats, offset); dst.position(0); if (dst instanceof ByteBuffer) dst.limit(numFloats << 2); else if (dst instanceof FloatBuffer) dst.limit(numFloats); }
public synchronized int compress(byte[] b, int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } if (off < 0 || len < 0 || off > b.length - len) { throw new ArrayIndexOutOfBoundsException(); } int n = 0; // Check if there is compressed data n = compressedDirectBuf.remaining(); if (n > 0) { n = Math.min(n, len); ((ByteBuffer) compressedDirectBuf).get(b, off, n); return n; } // Re-initialize the zlib's output direct buffer compressedDirectBuf.rewind(); compressedDirectBuf.limit(directBufferSize); // Compress data n = deflateBytesDirect(); compressedDirectBuf.limit(n); // Get atmost 'len' bytes n = Math.min(n, len); ((ByteBuffer) compressedDirectBuf).get(b, off, n); return n; }
public void toBlind() { gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); vertex.position(0); gl.glVertexPointer(2, GL10.GL_FLOAT, 2 * 4, vertex); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); texture.position(0); gl.glVertexPointer(2, GL10.GL_SHORT, 2 * 2, texture); }
@Override public ByteBuffer asByteBuffer() { ByteBuffer duplicate = this.byteBuffer.duplicate(); // Explicit access via Buffer base type for compatibility // with covariant return type on JDK 9's ByteBuffer... Buffer buffer = duplicate; buffer.position(this.readPosition); buffer.limit(this.writePosition); return duplicate; }
/** Resets compressor so that a new set of input data can be processed. */ @Override public synchronized void reset() { finish = false; finished = false; uncompressedDirectBuf.clear(); uncompressedDirectBufLen = 0; compressedDirectBuf.clear(); compressedDirectBuf.limit(0); userBufOff = userBufLen = 0; bytesRead = bytesWritten = 0L; }
/** Optional, throws GLException if not available in profile */ public final int gluScaleImage( int format, int widthin, int heightin, int typein, java.nio.Buffer datain, int widthout, int heightout, int typeout, java.nio.Buffer dataout) { validateMipmap(); java.nio.ByteBuffer in = null; java.nio.ByteBuffer out = null; in = copyToByteBuffer(datain); if (dataout instanceof java.nio.ByteBuffer) { out = (java.nio.ByteBuffer) dataout; } else if (dataout instanceof java.nio.ShortBuffer) { out = Buffers.newDirectByteBuffer(dataout.remaining() * Buffers.SIZEOF_SHORT); } else if (dataout instanceof java.nio.IntBuffer) { out = Buffers.newDirectByteBuffer(dataout.remaining() * Buffers.SIZEOF_INT); } else if (dataout instanceof java.nio.FloatBuffer) { out = Buffers.newDirectByteBuffer(dataout.remaining() * Buffers.SIZEOF_FLOAT); } else { throw new IllegalArgumentException( "Unsupported destination buffer type (must be byte, short, int, or float)"); } int errno = Mipmap.gluScaleImage( getCurrentGL2ES1(), format, widthin, heightin, typein, in, widthout, heightout, typeout, out); if (errno == 0) { out.rewind(); if (out != dataout) { if (dataout instanceof java.nio.ShortBuffer) { ((java.nio.ShortBuffer) dataout).put(out.asShortBuffer()); } else if (dataout instanceof java.nio.IntBuffer) { ((java.nio.IntBuffer) dataout).put(out.asIntBuffer()); } else if (dataout instanceof java.nio.FloatBuffer) { ((java.nio.FloatBuffer) dataout).put(out.asFloatBuffer()); } else { throw new RuntimeException("Should not reach here"); } } } return (errno); }
public synchronized void reset() { checkStream(); reset(stream); finish = false; finished = false; uncompressedDirectBuf.rewind(); uncompressedDirectBufOff = uncompressedDirectBufLen = 0; compressedDirectBuf.limit(directBufferSize); compressedDirectBuf.position(directBufferSize); userBufOff = userBufLen = 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(); } }
/** * 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; } } }
private void drawElements(int mode, int format, Buffer data) { GL gl = GLContext.getCurrentGL(); switch (format) { case GL.GL_UNSIGNED_BYTE: gl.getGL2().glDrawElements(mode, data.limit(), format, (ByteBuffer) data); break; case GL.GL_UNSIGNED_SHORT: gl.getGL2().glDrawElements(mode, data.limit(), format, (ShortBuffer) data); break; case GL.GL_UNSIGNED_INT: gl.getGL2().glDrawElements(mode, data.limit(), format, (IntBuffer) data); break; default: throw new UnsupportedOperationException(); } }
/** * Creates a new compressor. * * @param directBufferSize size of the direct buffer to be used. */ public SnappyCompressor(int directBufferSize) { this.directBufferSize = directBufferSize; uncompressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize); compressedDirectBuf = ByteBuffer.allocateDirect(directBufferSize); compressedDirectBuf.position(directBufferSize); }
public void data(Buffer theData) { data( theData.limit() * GLBufferUtil.typeSize(theData) * 100, theData, GLDataAccesFrequency.STATIC, GLDataAccesNature.DRAW); }
@Override public DataBuffer slice(int index, int length) { int oldPosition = this.byteBuffer.position(); // Explicit access via Buffer base type for compatibility // with covariant return type on JDK 9's ByteBuffer... Buffer buffer = this.byteBuffer; try { buffer.position(index); ByteBuffer slice = this.byteBuffer.slice(); // Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer ((Buffer) slice).limit(length); return new SlicedDefaultDataBuffer(slice, 0, length, this.dataBufferFactory); } finally { buffer.position(oldPosition); } }
public void glTexSubImage2D( int target, int level, int xoffset, int yoffset, int width, int height, int format, int type, Buffer pixels) { if (pixels instanceof ByteBuffer) GL11.glTexSubImage2D( target, level, xoffset, yoffset, width, height, format, type, (ByteBuffer) pixels); else if (pixels instanceof ShortBuffer) GL11.glTexSubImage2D( target, level, xoffset, yoffset, width, height, format, type, (ShortBuffer) pixels); else if (pixels instanceof IntBuffer) GL11.glTexSubImage2D( target, level, xoffset, yoffset, width, height, format, type, (IntBuffer) pixels); else if (pixels instanceof FloatBuffer) GL11.glTexSubImage2D( target, level, xoffset, yoffset, width, height, format, type, (FloatBuffer) pixels); else if (pixels instanceof DoubleBuffer) GL11.glTexSubImage2D( target, level, xoffset, yoffset, width, height, format, type, (DoubleBuffer) pixels); else throw new GdxRuntimeException( "Can't use " + pixels.getClass().getName() + " with this method. Use ByteBuffer, ShortBuffer, IntBuffer, FloatBuffer or DoubleBuffer instead. Blame LWJGL"); }
public static void loadData(BasicCache<String, String> cache, String fileName) throws IOException { try (BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(ScriptingUtils.class.getResourceAsStream(fileName)))) { int chunkSize = 10; int chunkId = 0; CharBuffer cbuf = CharBuffer.allocate(1024 * chunkSize); while (bufferedReader.read(cbuf) >= 0) { Buffer buffer = cbuf.flip(); String textChunk = buffer.toString(); cache.put(fileName + (chunkId++), textChunk); cbuf.clear(); } } }
@Override public void glDrawElements(int mode, int count, int type, Buffer indices) { gl.drawElements( mode, count, type, indices.position()); // FIXME this is assuming WebGL supports client side buffers... }
private static void throwBufferSizeGTException(Buffer buf, int size) { throw new IllegalArgumentException( "Number of remaining buffer elements is " + buf.remaining() + ", must be at most " + size + "."); }
public synchronized void setInput(byte[] b, int off, int len) { if (b == null) { throw new NullPointerException(); } if (off < 0 || len < 0 || off > b.length - len) { throw new ArrayIndexOutOfBoundsException(); } this.userBuf = b; this.userBufOff = off; this.userBufLen = len; setInputFromSavedData(); // Reinitialize zlib's output direct buffer compressedDirectBuf.limit(directBufferSize); compressedDirectBuf.position(directBufferSize); }
private static int positionInBytes(Buffer dst) { if (dst instanceof ByteBuffer) return dst.position(); else if (dst instanceof ShortBuffer) return dst.position() << 1; else if (dst instanceof CharBuffer) return dst.position() << 1; else if (dst instanceof IntBuffer) return dst.position() << 2; else if (dst instanceof LongBuffer) return dst.position() << 3; else if (dst instanceof FloatBuffer) return dst.position() << 2; else if (dst instanceof DoubleBuffer) return dst.position() << 3; else throw new GdxRuntimeException("Can't copy to a " + dst.getClass().getName() + " instance"); }
private static int elementsToBytes(Buffer dst, int elements) { if (dst instanceof ByteBuffer) return elements; else if (dst instanceof ShortBuffer) return elements << 1; else if (dst instanceof CharBuffer) return elements << 1; else if (dst instanceof IntBuffer) return elements << 2; else if (dst instanceof LongBuffer) return elements << 3; else if (dst instanceof FloatBuffer) return elements << 2; else if (dst instanceof DoubleBuffer) return elements << 3; else throw new GdxRuntimeException("Can't copy to a " + dst.getClass().getName() + " instance"); }
/** * Sets input data for compression. This should be called whenever #needsInput() returns <code> * true</code> indicating that more input data is required. * * @param b Input data * @param off Start offset * @param len Length */ @Override public synchronized void setInput(byte[] b, int off, int len) { if (b == null) { throw new NullPointerException(); } if (off < 0 || len < 0 || off > b.length - len) { throw new ArrayIndexOutOfBoundsException(); } finished = false; if (len > uncompressedDirectBuf.remaining()) { // save data; now !needsInput this.userBuf = b; this.userBufOff = off; this.userBufLen = len; } else { ((ByteBuffer) uncompressedDirectBuf).put(b, off, len); uncompressedDirectBufLen = uncompressedDirectBuf.position(); } bytesRead += len; }
public void glDrawElements(int mode, int count, int type, Buffer indices) { if (indices instanceof ShortBuffer && type == GL_UNSIGNED_SHORT) GL11.glDrawElements(mode, (ShortBuffer) indices); else if (indices instanceof ByteBuffer && type == GL_UNSIGNED_SHORT) GL11.glDrawElements(mode, ((ByteBuffer) indices).asShortBuffer()); // FIXME yay... else if (indices instanceof ByteBuffer && type == GL_UNSIGNED_BYTE) GL11.glDrawElements(mode, (ByteBuffer) indices); else throw new GdxRuntimeException( "Can't use " + indices.getClass().getName() + " with this method. Use ShortBuffer or ByteBuffer instead. Blame LWJGL"); }
public void drawTriangleList(VertexBuffer indexBuf, Mesh mesh, int count) { Mesh.Mode mode = mesh.getMode(); Buffer indexData = indexBuf.getData(); indexData.rewind(); if (mesh.getMode() == Mode.Hybrid) { throw new UnsupportedOperationException(); /* int[] modeStart = mesh.getModeStart(); int[] elementLengths = mesh.getElementLengths(); int elMode = convertElementMode(Mode.Triangles); int fmt = convertVertexFormat(indexBuf.getFormat()); // int elSize = indexBuf.getFormat().getComponentSize(); // int listStart = modeStart[0]; int stripStart = modeStart[1]; int fanStart = modeStart[2]; int curOffset = 0; for (int i = 0; i < elementLengths.length; i++) { if (i == stripStart) { elMode = convertElementMode(Mode.TriangleStrip); } else if (i == fanStart) { elMode = convertElementMode(Mode.TriangleStrip); } int elementLength = elementLengths[i]; indexData.position(curOffset); drawElements(elMode, fmt, indexData); curOffset += elementLength; }*/ } else { drawElements(convertElementMode(mode), convertVertexFormat(indexBuf.getFormat()), indexData); } }
synchronized void setInputFromSavedData() { uncompressedDirectBufOff = 0; uncompressedDirectBufLen = userBufLen; if (uncompressedDirectBufLen > directBufferSize) { uncompressedDirectBufLen = directBufferSize; } // Reinitialize zlib's input direct buffer uncompressedDirectBuf.rewind(); ((ByteBuffer) uncompressedDirectBuf).put(userBuf, userBufOff, uncompressedDirectBufLen); // Note how much data is being fed to zlib userBufOff += uncompressedDirectBufLen; userBufLen -= uncompressedDirectBufLen; }
public void glReadPixels( int x, int y, int width, int height, int format, int type, Buffer pixels) { if (pixels instanceof ByteBuffer) GL11.glReadPixels(x, y, width, height, format, type, (ByteBuffer) pixels); else if (pixels instanceof ShortBuffer) GL11.glReadPixels(x, y, width, height, format, type, (ShortBuffer) pixels); else if (pixels instanceof IntBuffer) GL11.glReadPixels(x, y, width, height, format, type, (IntBuffer) pixels); else if (pixels instanceof FloatBuffer) GL11.glReadPixels(x, y, width, height, format, type, (FloatBuffer) pixels); else throw new GdxRuntimeException( "Can't use " + pixels.getClass().getName() + " with this method. Use ByteBuffer, ShortBuffer, IntBuffer or FloatBuffer instead. Blame LWJGL"); }
void grow(int minCapacity) { ByteBuffer oldBuffer = this.byteBuffer; ByteBuffer newBuffer = (oldBuffer.isDirect() ? ByteBuffer.allocateDirect(minCapacity) : ByteBuffer.allocate(minCapacity)); // Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer final int remaining = readableByteCount(); ((Buffer) oldBuffer).position(this.readPosition).limit(this.writePosition); newBuffer.put(oldBuffer); this.byteBuffer = newBuffer; this.readPosition = 0; this.writePosition = remaining; oldBuffer.clear(); }
private final ByteBuffer copyToByteBuffer(Buffer buf) { if (buf instanceof ByteBuffer) { if (buf.position() == 0) { return (ByteBuffer) buf; } return Buffers.copyByteBuffer((ByteBuffer) buf); } else if (buf instanceof ShortBuffer) { return Buffers.copyShortBufferAsByteBuffer((ShortBuffer) buf); } else if (buf instanceof IntBuffer) { return Buffers.copyIntBufferAsByteBuffer((IntBuffer) buf); } else if (buf instanceof FloatBuffer) { return Buffers.copyFloatBufferAsByteBuffer((FloatBuffer) buf); } else { throw new IllegalArgumentException( "Unsupported buffer type (must be one of byte, short, int, or float)"); } }
public void glCompressedTexImage2D( int target, int level, int internalformat, int width, int height, int border, int imageSize, Buffer data) { if (data instanceof ByteBuffer) { GL13.glCompressedTexImage2D( target, level, internalformat, width, height, border, (ByteBuffer) data); } else { throw new GdxRuntimeException( "Can't use " + data.getClass().getName() + " with this method. Use ByteBuffer instead."); } }