Browse Source

Cleaned up DeleteTex() function.

woollybah 7 years ago
parent
commit
278e44f905
2 changed files with 38 additions and 27 deletions
  1. 19 14
      gl2sdlmax2d.mod/gl2sdlmax2d.bmx
  2. 19 13
      glsdlmax2d.mod/glsdlmax2d.bmx

+ 19 - 14
gl2sdlmax2d.mod/gl2sdlmax2d.bmx

@@ -72,25 +72,27 @@ Function Pow2Size( n )
 	Return t
 End Function
 
-Global dead_texs[], n_dead_texs, dead_tex_seq
+Global dead_texs[],n_dead_texs,dead_tex_seq,n_live_texs
 
-'Enqueues a texture for deletion, to prevent release textures on wrong thread.
-'
-'Not thread safe, but that's OK because all threads are stopped when TGLImageFrame.Delete()
-'is called, which is what calls us.
+Extern
+	Function bbAtomicAdd:Int( target:Int Ptr,value:Int )="int bbAtomicAdd( int *,int )!"
+End Extern
 
-Function DeleteTex( name, seq )
+'Enqueues a texture for deletion, to prevent release textures on wrong thread.
+Function DeleteTex( name,seq )
+	If seq<>dead_tex_seq Return
+	
+	Local n:Int = bbAtomicAdd(Varptr n_dead_texs, 1)
+	bbAtomicAdd(Varptr n_live_texs, -1)
 
-	If seq <> dead_tex_seq Return
+	dead_texs[n] = name
+End Function
 
-	'add tex to queue
-	If dead_texs.length = n_dead_texs
-		dead_texs = dead_texs[..n_dead_texs + 256]
+Function _ManageDeadTexArray()
+	If dead_texs.length <= n_live_texs
+		' expand array so it's large enough to hold all the current live textures.
+		dead_texs=dead_texs[..n_live_texs+20]
 	EndIf
-
-	dead_texs[n_dead_texs] = name
-	n_dead_texs :+ 1
-
 End Function
 
 Function CreateTex( width, height, flags )
@@ -99,6 +101,9 @@ Function CreateTex( width, height, flags )
 	Local name
 	glGenTextures( 1, Varptr name )
 
+	n_live_texs :+ 1
+	_ManageDeadTexArray()
+
 	'flush dead texs
 	If dead_tex_seq = GraphicsSeq
 		For Local i = 0 Until n_dead_texs

+ 19 - 13
glsdlmax2d.mod/glsdlmax2d.bmx

@@ -84,31 +84,37 @@ Function Pow2Size( n )
 	Return t
 End Function
 
-Global dead_texs[],n_dead_texs,dead_tex_seq
+Global dead_texs[],n_dead_texs,dead_tex_seq,n_live_texs
+
+Extern
+	Function bbAtomicAdd:Int( target:Int Ptr,value:Int )="int bbAtomicAdd( int *,int )!"
+End Extern
 
 'Enqueues a texture for deletion, to prevent release textures on wrong thread.
-'
-'Not thread safe, but that's OK because all threads are stopped when TGLImageFrame.Delete()
-'is called, which is what calls us.
-'
 Function DeleteTex( name,seq )
-
 	If seq<>dead_tex_seq Return
+	
+	Local n:Int = bbAtomicAdd(Varptr n_dead_texs, 1)
+	bbAtomicAdd(Varptr n_live_texs, -1)
+
+	dead_texs[n] = name
+End Function
 
-	'add tex to queue
-	If dead_texs.length=n_dead_texs
-		dead_texs=dead_texs[..n_dead_texs+10]
+Function _ManageDeadTexArray()
+	If dead_texs.length <= n_live_texs
+		' expand array so it's large enough to hold all the current live textures.
+		dead_texs=dead_texs[..n_live_texs+20]
 	EndIf
-	dead_texs[n_dead_texs]=name
-	n_dead_texs:+1
-	'
 End Function
 
 Function CreateTex( width,height,flags )
 	'alloc new tex
 	Local name
 	glGenTextures 1,Varptr name
-	
+
+	n_live_texs :+ 1
+	_ManageDeadTexArray()
+
 	'flush dead texs
 	If dead_tex_seq=GraphicsSeq
 		For Local i=0 Until n_dead_texs