Skip to content

Commit

Permalink
[changed] using hardware mipmap generation now if supported, speeds u…
Browse files Browse the repository at this point in the history
…p loading.
  • Loading branch information
badlogicgames committed Jul 7, 2011
1 parent ab1bd3f commit 8cf20b7
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ protected AndroidDisplayMode (int width, int height, int refreshRate, int bitsPe
@Override public void setVSync (boolean vsync) {
}

@Override public boolean hasExtension (String extension) {
@Override public boolean supportsExtension (String extension) {
if(extensions == null) extensions = Gdx.gl.glGetString(GL10.GL_EXTENSIONS);
return extensions.contains(extension);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.awt.Toolkit;
import java.util.List;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
Expand Down Expand Up @@ -332,8 +333,8 @@ protected static JFrame findJFrame(Component component) {
return new BufferFormat(caps.getRedBits(), caps.getGreenBits(), caps.getBlueBits(), caps.getAlphaBits(), caps.getDepthBits(), caps.getStencilBits(), caps.getNumSamples(), false);
}

@Override public boolean hasExtension (String extension) {
@Override public boolean supportsExtension (String extension) {
if(extensions == null) extensions = Gdx.gl.glGetString(GL10.GL_EXTENSIONS);
return extensions.contains(extension);
return extensions.contains(extension);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public LwjglDisplayMode (int width, int height, int refreshRate, int bitsPerPixe
if(!vsync && !config.useCPUSynch) Display.setVSyncEnabled(false);
}

@Override public boolean hasExtension (String extension) {
@Override public boolean supportsExtension (String extension) {
if(extensions == null) extensions = Gdx.gl.glGetString(GL10.GL_EXTENSIONS);
return extensions.contains(extension);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void updateTime () {
return null;
}

@Override public boolean hasExtension (String extension) {
@Override public boolean supportsExtension (String extension) {
if(extensions == null) extensions = Gdx.gl.glGetString(GL10.GL_EXTENSIONS);
return extensions.contains(extension);
}
Expand Down
2 changes: 1 addition & 1 deletion gdx/src/com/badlogic/gdx/Graphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public String toString() {
* @param extension the extension name
* @return whether the extension is supported
*/
public boolean hasExtension(String extension);
public boolean supportsExtension(String extension);

// /**
// * Opens the first back facing video camera. Only one camera
Expand Down
2 changes: 2 additions & 0 deletions gdx/src/com/badlogic/gdx/graphics/GLCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*
*/
public interface GLCommon {
public static final int GL_GENERATE_MIPMAP = 0x8191;

public void glActiveTexture (int texture);

public void glBindTexture (int target, int texture);
Expand Down
101 changes: 62 additions & 39 deletions gdx/src/com/badlogic/gdx/graphics/Texture.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.files.FileHandle;
Expand Down Expand Up @@ -309,32 +310,70 @@ private void uploadImageData(Pixmap pixmap) {
this.height = pixmap.getHeight();
if(enforcePotImages && Gdx.gl20 == null && (!MathUtils.isPowerOfTwo(width) || !MathUtils.isPowerOfTwo(height)))
throw new GdxRuntimeException("texture width and height must be powers of two");
Gdx.gl.glBindTexture(GL10.GL_TEXTURE_2D, glHandle);
Gdx.gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());
if(isMipMap) {
if(!(Gdx.gl20==null) && width != height)
throw new GdxRuntimeException("texture width and height must be square when using mipmapping in OpenGL ES 1.x");
int width = pixmap.getWidth() / 2;
int height = pixmap.getHeight() / 2;
int level = 1;
while(width > 0 && height > 0) {
Pixmap tmp = new Pixmap(width, height, pixmap.getFormat());
tmp.drawPixmap(pixmap, 0, 0, pixmap.getWidth(), pixmap.getHeight(), 0, 0, width, height);
if(level > 1 || disposePixmap)
pixmap.dispose();
pixmap = tmp;

Gdx.gl.glTexImage2D(GL10.GL_TEXTURE_2D, level, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());

width = pixmap.getWidth() / 2;
height = pixmap.getHeight() / 2;
level++;
}
pixmap.dispose();
Gdx.gl.glBindTexture(GL10.GL_TEXTURE_2D, glHandle);
if(isMipMap) {
generateMipMap(pixmap, disposePixmap);
} else {
Gdx.gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());
if(disposePixmap) pixmap.dispose();
}
}

private void generateMipMap(Pixmap pixmap, boolean disposePixmap) {
if(Gdx.app.getType() == ApplicationType.Android) {
if(Gdx.graphics.isGL20Available())
generateMipMapGLES20(pixmap, disposePixmap);
else
generateMipMapCPU(pixmap, disposePixmap);
} else {
generateMipMapDesktop(pixmap, disposePixmap);
}
}

private void generateMipMapGLES20(Pixmap pixmap, boolean disposePixmap) {
Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_2D, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());
Gdx.gl20.glGenerateMipmap(GL20.GL_TEXTURE_2D);
if(disposePixmap) pixmap.dispose();
}

private void generateMipMapDesktop(Pixmap pixmap, boolean disposePixmap) {
if(Gdx.graphics.isGL20Available() &&
(Gdx.graphics.supportsExtension("GL_ARB_framebuffer_object") ||
Gdx.graphics.supportsExtension("GL_EXT_framebuffer_object"))) {
Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_2D, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());
Gdx.gl20.glGenerateMipmap(GL20.GL_TEXTURE_2D);
if(disposePixmap) pixmap.dispose();
} else if(Gdx.graphics.supportsExtension("GL_SGIS_generate_mipmap")) {
Gdx.gl.glTexParameterf(GL20.GL_TEXTURE_2D, GLCommon.GL_GENERATE_MIPMAP, GL10.GL_TRUE);
Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_2D, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());
if(disposePixmap) pixmap.dispose();
} else {
generateMipMapCPU(pixmap, disposePixmap);
}
}

private void generateMipMapCPU(Pixmap pixmap, boolean disposePixmap) {
Gdx.gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());
if(!(Gdx.gl20==null) && width != height)
throw new GdxRuntimeException("texture width and height must be square when using mipmapping in OpenGL ES 1.x");
int width = pixmap.getWidth() / 2;
int height = pixmap.getHeight() / 2;
int level = 1;
while(width > 0 && height > 0) {
Pixmap tmp = new Pixmap(width, height, pixmap.getFormat());
tmp.drawPixmap(pixmap, 0, 0, pixmap.getWidth(), pixmap.getHeight(), 0, 0, width, height);
if(level > 1 || disposePixmap)
pixmap.dispose();
pixmap = tmp;

Gdx.gl.glTexImage2D(GL10.GL_TEXTURE_2D, level, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());

width = pixmap.getWidth() / 2;
height = pixmap.getHeight() / 2;
level++;
}
pixmap.dispose();
}

/**
* Binds this texture. The texture will be bound to the currently active texture unit specified via
Expand Down Expand Up @@ -370,23 +409,7 @@ public void draw (Pixmap pixmap, int x, int y) {
Gdx.gl.glTexSubImage2D(GL10.GL_TEXTURE_2D, 0, x, y, pixmap.getWidth(), pixmap.getHeight(), pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());

if(isMipMap) {
int level = 1;
int height = pixmap.getHeight();
int width = pixmap.getWidth();

while (height > 0 && width > 0) {
Pixmap tmp = new Pixmap(width, height, pixmap.getFormat());
tmp.drawPixmap(pixmap, 0, 0, pixmap.getWidth(), pixmap.getHeight(), 0, 0, width, height);
if(level > 1)
pixmap.dispose();
pixmap = tmp;

Gdx.gl.glTexSubImage2D(GL10.GL_TEXTURE_2D, level, x, y, pixmap.getWidth(), pixmap.getHeight(), pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels());

width = pixmap.getWidth() / 2;
height = pixmap.getHeight() / 2;
level++;
}
generateMipMap(pixmap, false);
}
}

Expand Down

0 comments on commit 8cf20b7

Please sign in to comment.