diff options
-rwxr-xr-x | src/classes/com/sun/opengl/util/texture/Texture.java | 91 | ||||
-rwxr-xr-x | src/classes/com/sun/opengl/util/texture/TextureData.java | 37 |
2 files changed, 128 insertions, 0 deletions
diff --git a/src/classes/com/sun/opengl/util/texture/Texture.java b/src/classes/com/sun/opengl/util/texture/Texture.java index 9987551e2..4daf5d141 100755 --- a/src/classes/com/sun/opengl/util/texture/Texture.java +++ b/src/classes/com/sun/opengl/util/texture/Texture.java @@ -74,6 +74,9 @@ public class Texture { /** The texture coordinates corresponding to the entire image. */ private TextureCoords coords; + /** An estimate of the amount of texture memory this texture consumes. */ + private int estimatedMemorySize; + private static final boolean DEBUG = Debug.debug("Texture"); // For now make Texture constructor package-private to limit the @@ -369,6 +372,9 @@ public class Texture { (this.target == GL.GL_TEXTURE_RECTANGLE_ARB)) { this.target = newTarget; } + + // This estimate will be wrong for cube maps + estimatedMemorySize = data.getEstimatedMemorySize(); } /** @@ -394,6 +400,52 @@ public class Texture { } /** + * Sets the OpenGL floating-point texture parameter for the + * texture's target. This gives control over parameters such as + * GL_TEXTURE_MAX_ANISOTROPY_EXT. Causes this texture to be bound to + * the current texture state. + * + * @throws GLException if no OpenGL context was current or if any + * OpenGL-related errors occurred + */ + public void setTexParameterf(int parameterName, + float value) { + bind(); + GL gl = GLU.getCurrentGL(); + gl.glTexParameterf(target, parameterName, value); + } + + /** + * Sets the OpenGL multi-floating-point texture parameter for the + * texture's target. Causes this texture to be bound to the current + * texture state. + * + * @throws GLException if no OpenGL context was current or if any + * OpenGL-related errors occurred + */ + public void setTexParameterfv(int parameterName, + FloatBuffer params) { + bind(); + GL gl = GLU.getCurrentGL(); + gl.glTexParameterfv(target, parameterName, params); + } + + /** + * Sets the OpenGL multi-floating-point texture parameter for the + * texture's target. Causes this texture to be bound to the current + * texture state. + * + * @throws GLException if no OpenGL context was current or if any + * OpenGL-related errors occurred + */ + public void setTexParameterfv(int parameterName, + float[] params, int params_offset) { + bind(); + GL gl = GLU.getCurrentGL(); + gl.glTexParameterfv(target, parameterName, params, params_offset); + } + + /** * Sets the OpenGL integer texture parameter for the texture's * target. This gives control over parameters such as * GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T, which by default are set @@ -411,6 +463,36 @@ public class Texture { } /** + * Sets the OpenGL multi-integer texture parameter for the texture's + * target. Causes this texture to be bound to the current texture + * state. + * + * @throws GLException if no OpenGL context was current or if any + * OpenGL-related errors occurred + */ + public void setTexParameteriv(int parameterName, + IntBuffer params) { + bind(); + GL gl = GLU.getCurrentGL(); + gl.glTexParameteriv(target, parameterName, params); + } + + /** + * Sets the OpenGL multi-integer texture parameter for the texture's + * target. Causes this texture to be bound to the current texture + * state. + * + * @throws GLException if no OpenGL context was current or if any + * OpenGL-related errors occurred + */ + public void setTexParameteriv(int parameterName, + int[] params, int params_offset) { + bind(); + GL gl = GLU.getCurrentGL(); + gl.glTexParameteriv(target, parameterName, params, params_offset); + } + + /** * Returns the underlying OpenGL texture object for this texture. * Most applications will not need to access this, since it is * handled automatically by the bind() and dispose() APIs. @@ -419,6 +501,15 @@ public class Texture { return texID; } + /** Returns an estimate of the amount of texture memory in bytes + this Texture consumes. It should only be treated as an estimate; + most applications should not need to query this but instead let + the OpenGL implementation page textures in and out as + necessary. */ + public int getEstimatedMemorySize() { + return estimatedMemorySize; + } + //---------------------------------------------------------------------- // Internals only below this point // diff --git a/src/classes/com/sun/opengl/util/texture/TextureData.java b/src/classes/com/sun/opengl/util/texture/TextureData.java index 83a1c983e..91915438b 100755 --- a/src/classes/com/sun/opengl/util/texture/TextureData.java +++ b/src/classes/com/sun/opengl/util/texture/TextureData.java @@ -74,6 +74,7 @@ public class TextureData { private Buffer[] mipmapData; // ...or a series of mipmaps private Flusher flusher; private int alignment; // 1, 2, or 4 bytes + private int estimatedMemorySize; private static final ColorModel rgbaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), @@ -151,6 +152,7 @@ public class TextureData { this.buffer = buffer; this.flusher = flusher; alignment = 1; // FIXME: is this correct enough in all situations? + estimatedMemorySize = estimatedMemorySize(buffer); } /** @@ -211,6 +213,9 @@ public class TextureData { this.mipmapData = (Buffer[]) mipmapData.clone(); this.flusher = flusher; alignment = 1; // FIXME: is this correct enough in all situations? + for (int i = 0; i < mipmapData.length; i++) { + estimatedMemorySize += estimatedMemorySize(mipmapData[i]); + } } /** @@ -240,6 +245,7 @@ public class TextureData { } createFromImage(image); this.mipmap = mipmap; + estimatedMemorySize = estimatedMemorySize(buffer); } /** Returns the width in pixels of the texture data. */ @@ -293,6 +299,15 @@ public class TextureData { /** Sets the required byte alignment for the texture data. */ public void setAlignment(int alignment) { this.alignment = alignment; } + /** Returns an estimate of the amount of memory in bytes this + TextureData will consume once uploaded to the graphics card. It + should only be treated as an estimate; most applications should + not need to query this but instead let the OpenGL implementation + page textures in and out as necessary. */ + public int getEstimatedMemorySize() { + return estimatedMemorySize; + } + /** Flushes resources associated with this TextureData by calling Flusher.flush(). */ public void flush() { @@ -509,4 +524,26 @@ public class TextureData { pixelFormat = hasAlpha ? GL.GL_RGBA : GL.GL_RGB; alignment = 1; // FIXME: do we need better? } + + private int estimatedMemorySize(Buffer buffer) { + if (buffer == null) { + return 0; + } + int capacity = buffer.capacity(); + if (buffer instanceof ByteBuffer) { + return capacity; + } else if (buffer instanceof IntBuffer) { + return capacity * BufferUtil.SIZEOF_INT; + } else if (buffer instanceof FloatBuffer) { + return capacity * BufferUtil.SIZEOF_FLOAT; + } else if (buffer instanceof ShortBuffer) { + return capacity * BufferUtil.SIZEOF_SHORT; + } else if (buffer instanceof LongBuffer) { + return capacity * BufferUtil.SIZEOF_LONG; + } else if (buffer instanceof DoubleBuffer) { + return capacity * BufferUtil.SIZEOF_DOUBLE; + } + throw new RuntimeException("Unexpected buffer type " + + buffer.getClass().getName()); + } } |