6262import com .jme3 .util .NativeObjectManager ;
6363import jme3tools .shader .ShaderDebug ;
6464
65+ import java .lang .ref .WeakReference ;
6566import java .nio .ByteBuffer ;
6667import java .nio .FloatBuffer ;
6768import java .nio .IntBuffer ;
@@ -2385,9 +2386,9 @@ private void bindTextureAndUnit(int target, Image img, int unit) {
23852386 gl .glActiveTexture (GL .GL_TEXTURE0 + unit );
23862387 context .boundTextureUnit = unit ;
23872388 }
2388- if (context .boundTextures [unit ] != img ) {
2389+ if (context .boundTextures [unit ]== null || context . boundTextures [ unit ]. get () != img . getWeakRef (). get () ) {
23892390 gl .glBindTexture (target , img .getId ());
2390- context .boundTextures [unit ] = img ;
2391+ context .boundTextures [unit ] = img . getWeakRef () ;
23912392 statistics .onTextureUse (img , true );
23922393 } else {
23932394 statistics .onTextureUse (img , false );
@@ -2403,13 +2404,13 @@ private void bindTextureAndUnit(int target, Image img, int unit) {
24032404 * @param unit At what unit to bind the texture.
24042405 */
24052406 private void bindTextureOnly (int target , Image img , int unit ) {
2406- if (context .boundTextures [unit ] != img ) {
2407+ if (context .boundTextures [unit ] == null || context . boundTextures [ unit ]. get () != img . getWeakRef (). get () ) {
24072408 if (context .boundTextureUnit != unit ) {
24082409 gl .glActiveTexture (GL .GL_TEXTURE0 + unit );
24092410 context .boundTextureUnit = unit ;
24102411 }
24112412 gl .glBindTexture (target , img .getId ());
2412- context .boundTextures [unit ] = img ;
2413+ context .boundTextures [unit ] = img . getWeakRef () ;
24132414 statistics .onTextureUse (img , true );
24142415 } else {
24152416 statistics .onTextureUse (img , false );
@@ -2824,7 +2825,7 @@ public void clearVertexAttribs() {
28242825 for (int i = 0 ; i < attribList .oldLen ; i ++) {
28252826 int idx = attribList .oldList [i ];
28262827 gl .glDisableVertexAttribArray (idx );
2827- if (context .boundAttribs [idx ].isInstanced ()) {
2828+ if (context .boundAttribs [idx ].get (). isInstanced ()) {
28282829 glext .glVertexAttribDivisorARB (idx , 0 );
28292830 }
28302831 context .boundAttribs [idx ] = null ;
@@ -2879,13 +2880,13 @@ public void setVertexAttrib(VertexBuffer vb, VertexBuffer idb) {
28792880 updateBufferData (vb );
28802881 }
28812882
2882- VertexBuffer [] attribs = context .boundAttribs ;
2883+ WeakReference < VertexBuffer > [] attribs = context .boundAttribs ;
28832884 for (int i = 0 ; i < slotsRequired ; i ++) {
28842885 if (!context .attribIndexList .moveToNew (loc + i )) {
28852886 gl .glEnableVertexAttribArray (loc + i );
28862887 }
28872888 }
2888- if (attribs [loc ] != vb ) {
2889+ if (attribs [loc ]== null || attribs [ loc ]. get () != vb ) {
28892890 // NOTE: Use id from interleaved buffer if specified
28902891 int bufId = idb != null ? idb .getId () : vb .getId ();
28912892 assert bufId != -1 ;
@@ -2925,14 +2926,14 @@ public void setVertexAttrib(VertexBuffer vb, VertexBuffer idb) {
29252926
29262927 for (int i = 0 ; i < slotsRequired ; i ++) {
29272928 int slot = loc + i ;
2928- if (vb .isInstanced () && (attribs [slot ] == null || !attribs [slot ].isInstanced ())) {
2929+ if (vb .isInstanced () && (attribs [slot ] == null || attribs [ slot ]. get () == null || !attribs [slot ]. get () .isInstanced ())) {
29292930 // non-instanced -> instanced
29302931 glext .glVertexAttribDivisorARB (slot , vb .getInstanceSpan ());
2931- } else if (!vb .isInstanced () && attribs [slot ] != null && attribs [slot ].isInstanced ()) {
2932+ } else if (!vb .isInstanced () && attribs [slot ] != null && attribs [slot ].get () != null && attribs [ slot ]. get (). isInstanced ()) {
29322933 // instanced -> non-instanced
29332934 glext .glVertexAttribDivisorARB (slot , 0 );
29342935 }
2935- attribs [slot ] = vb ;
2936+ attribs [slot ] = vb . getWeakRef () ;
29362937 }
29372938 }
29382939 }
0 commit comments