Skip to content

Commit

Permalink
Nifty batch fix - memory improvement (#1220)
Browse files Browse the repository at this point in the history
* Update GLImageFormats.java

* Modified JmeBatchRenderBackend to clear the inner buffer of the image in the atlases instead of setting a predefined byte buffer
on disposal that made all atlases in the backend use the same buffer and generated rendering issues.

* First impl of testcasefor multiple atlases issue. Still missing to add more images to the screens so it really uses more atlases

* Fix conflict

* Fix buffer allocator

* Modified JmeBatchRenderBackend removing some changes and improving memory usage

* standardize formatting per issue #1098

* JmeBatchRenderBackend.java: copyright date -> 2020

Co-authored-by: joliver82 <joliver@allot.com>
Co-authored-by: Stephen Gold <sgold@sonic.net>
  • Loading branch information
3 people committed Nov 8, 2020
1 parent e0310c7 commit c7ff806
Showing 1 changed file with 14 additions and 14 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2019 jMonkeyEngine
* Copyright (c) 2009-2020 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -92,7 +92,7 @@ public class JmeBatchRenderBackend implements BatchRenderBackend {
private int textureAtlasId = 1;
private Batch currentBatch;
private Matrix4f tempMat = new Matrix4f();
private ByteBuffer initialData;
private ByteBuffer initialData = null;

// this is only used for debugging purpose and will make the removed textures filled with a color
// please note: the old way to init this via a system property has been
Expand Down Expand Up @@ -186,16 +186,19 @@ public void disableMouseCursor() {
@Override
public int createTextureAtlas(final int width, final int height) {
try {
// we initialize a buffer here that will be used as base for all texture atlas images
if (initialData == null) {
initialData = BufferUtils.createByteBuffer(width * height * 4);
for (int i = 0; i < width * height; i++) {
initialData.put((byte) 0x00);
initialData.put((byte) 0x00);
initialData.put((byte) 0x00);
initialData.put((byte) 0xff);
}
}

int atlasId = addTexture(createAtlasTextureInternal(width, height));

// we just initialize a second buffer here that will replace the texture atlas image
initialData = BufferUtils.createByteBuffer(width * height * 4);
for (int i = 0; i < width * height; i++) {
initialData.put((byte) 0x00);
initialData.put((byte) 0xff);
initialData.put((byte) 0x00);
initialData.put((byte) 0xff);
}
return atlasId;
} catch (Exception e) {
log.log(Level.WARNING, e.getMessage(), e);
Expand Down Expand Up @@ -368,10 +371,7 @@ public void fillRemovedImagesInAtlas(final boolean shouldFill) {

// internal implementations
private Texture2D createAtlasTextureInternal(final int width, final int height) throws Exception {
ByteBuffer initialData = BufferUtils.createByteBuffer(width * height * 4);
for (int i = 0; i < width * height * 4; i++) {
initialData.put((byte) 0x00);
}
// re-use pre-defined initial data instead of creating a new buffer
initialData.rewind();

Texture2D texture = new Texture2D(new com.jme3.texture.Image(Format.RGBA8, width, height, initialData, ColorSpace.sRGB));
Expand Down

0 comments on commit c7ff806

Please sign in to comment.