2
0
Эх сурвалжийг харах

Nifty batch fix - memory improvement (#1220)

* 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 <[email protected]>
Co-authored-by: Stephen Gold <[email protected]>
joliver82 4 жил өмнө
parent
commit
c7ff8063ef

+ 14 - 14
jme3-niftygui/src/main/java/com/jme3/niftygui/JmeBatchRenderBackend.java

@@ -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
@@ -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
@@ -186,16 +186,19 @@ public class JmeBatchRenderBackend implements BatchRenderBackend {
     @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);
@@ -368,10 +371,7 @@ public class JmeBatchRenderBackend implements BatchRenderBackend {
 
     // 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));