Quellcode durchsuchen

Added Finzlizers;Fixes for resources.

Mark Sibly vor 8 Jahren
Ursprung
Commit
f13185e4fd

+ 11 - 3
modules/mojo/app/skin.monkey2

@@ -47,11 +47,21 @@ Class Skin Extends Resource
 		If Not pixmap Return Null
 		
 		Return New Skin( pixmap )
+		
+		pixmap.Release()
+	End
+	
+	Protected
+	
+	Method OnDiscard() Override
+
+		_image.Release()
 	End
 	
 	Private
 	
 	Field _image:Image
+	
 	Field _bounds:Recti
 	
 	Field _x0:Int,_x1:Int,_x2:Int,_x3:Int
@@ -107,12 +117,10 @@ Class Skin Extends Resource
 		_y1=stretch.min.y
 		_y2=stretch.max.y
 		_y3=pixmap.Height
-		
-		_image=New Image( pixmap )
 
 		_bounds=New Recti( -padding.min.x,-padding.min.y,_x3-padding.max.x,_y3-padding.max.y )
 		
-		AddDependancy( _image )
+		_image=New Image( pixmap )
 	End
 End
 

+ 3 - 1
modules/mojo/graphics/canvas.monkey2

@@ -26,7 +26,7 @@ Class Canvas
 		
 		Local rtarget:=New RenderTarget( New Texture[]( image.Texture ),Null )
 		
-		image.OnDiscarded+=rtarget.Discard
+		image.Discarded+=rtarget.Release
 		
 		Init( rtarget,New GraphicsDevice )
 		
@@ -36,12 +36,14 @@ Class Canvas
 	#rem monkeydoc @hidden Creates a canvas that renders to the backbuffer.
 	#end	
 	Method New( width:Int,height:Int )
+		
 		Init( Null,New GraphicsDevice( width,height ) )
 	End
 
 	#rem monkeydoc @hidden Resizes a canvas that renders to the backbuffer.
 	#end	
 	Method Resize( size:Vec2i )
+		
 		_device.Resize( size )
 	End
 

+ 2 - 3
modules/mojo/graphics/font.monkey2

@@ -130,13 +130,12 @@ Class Font Extends Resource
 	End
 	
 	Method OnDiscard() Override
-	
+
 		For Local page:=Eachin _pages
-			If page And page.image page.image.Discard()
+			If page SafeRelease( page.image )
 		Next
 		
 		_pages=Null
-		
 	End
 	
 	Private

+ 4 - 5
modules/mojo/graphics/freetypefont.monkey2

@@ -36,11 +36,12 @@ Class FreeTypeFont Extends Font
 		
 		Local face:FT_Face
 		If FT_New_Memory_Face( FreeType,data.Data,data.Length,0,Varptr face ) 
-			data.Discard()
+			data.Release()
 			Return Null
 		Endif
 		
 		Local font:=New FreeTypeFont( data,face,fheight,shader )
+		font.Discarded+=data.Discard
 		
 		Return font
 	End
@@ -51,8 +52,6 @@ Class FreeTypeFont Extends Font
 	
 		FT_Done_Face( _face )
 	
-		_data.Discard()
-		
 		Super.OnDiscard()
 	End
 	
@@ -131,7 +130,7 @@ Class FreeTypeFont Extends Font
 			
 			pixmap.Paste( tmp,tx,ty )
 			
-			tmp.Discard()
+			tmp.Release()
 			
 			Local glyph:=New Glyph( New Recti( tx,ty,tx+gw,ty+gh ),New Vec2f( slot->bitmap_left,_ascent-slot->bitmap_top ),slot->advance.x Shr 6 )
 	
@@ -144,7 +143,7 @@ Class FreeTypeFont Extends Font
 		gpage.image=New Image( pixmap,TextureFlags.Filter|TextureFlags.Mipmap,_shader )
 		gpage.glyphs=glyphs
 		
-		pixmap.Discard()
+		pixmap.Release()
 		
 '		Print "Loading glyph page "+page+", image size="+gpage.image.Rect.Size
 	End

+ 23 - 34
modules/mojo/graphics/image.monkey2

@@ -58,14 +58,9 @@ Class Image Extends Resource
 	
 		Local texture:=New Texture( pixmap,textureFlags )
 		
-		Init( texture,shader )
-		
-		AddDependancy( texture )
-	End
-
-	Method New( width:Int,height:Int,textureFlags:TextureFlags=null,shader:Shader=Null )
+		Discarded+=texture.Release
 		
-		Self.New( width,height,PixelFormat.RGBA8,textureFlags,shader )
+		Init( texture,shader )
 	End
 
 	Method New( width:Int,height:Int,format:PixelFormat,textureFlags:TextureFlags=Null,shader:Shader=Null )
@@ -74,17 +69,20 @@ Class Image Extends Resource
 	
 		Local texture:=New Texture( width,height,format,textureFlags )
 		
+		Discarded+=texture.Release
+		
 		Init( texture,shader )
+	End
+
+	Method New( width:Int,height:Int,textureFlags:TextureFlags=null,shader:Shader=Null )
 		
-		AddDependancy( texture )
+		Self.New( width,height,PixelFormat.RGBA8,textureFlags,shader )
 	End
 
 	Method New( image:Image )
 	
 		Init( image._textures[0],image._rect,image._shader )
 		
-		image.AddDependancy( Self )
-		
 		For Local i:=1 Until 4
 			SetTexture( i,image.GetTexture( i ) )
 		Next
@@ -100,8 +98,6 @@ Class Image Extends Resource
 	
 		Init( image._textures[0],rect+image._rect.Origin,image._shader )
 		
-		image.AddDependancy( Self )
-		
 		For Local i:=1 Until 4
 			SetTexture( i,image.GetTexture( i ) )
 		Next
@@ -336,10 +332,8 @@ Class Image Extends Resource
 		If Not shader shader=mojo.graphics.Shader.GetShader( "sprite" )
 		
 		Local image:=New Image( pixmap,Null,shader )
-			
-		image.OnDiscarded+=Lambda()
-			pixmap.Discard()
-		End
+		
+		image.Discarded+=pixmap.Release
 		
 		Return image
 	End
@@ -367,12 +361,11 @@ Class Image Extends Resource
 		If Not shader shader=graphics.Shader.GetShader( "bump" )
 		
 		Local image:=New Image( texture0,shader )
-		image.SetTexture( 1,texture1 )
+
+		image.Discarded+=texture0.Release
+		image.Discarded+=texture1.Release
 		
-		image.OnDiscarded+=Lambda()
-			If texture0 texture0.Discard()
-			If texture1 texture1.Discard()
-		End
+		image.SetTexture( 1,texture1 )
 		
 		Return image
 	End
@@ -395,7 +388,7 @@ Class Image Extends Resource
 
 			Local tpixmap:=pixmap
 			pixmap=pixmap.Convert( PixelFormat.IA16 )
-			tpixmap.Discard()
+			tpixmap.Release()
 
 			'Copy A->I
 			For Local y:=0 Until pixmap.Height
@@ -410,7 +403,7 @@ Class Image Extends Resource
 		
 			Local tpixmap:=pixmap
 			pixmap=pixmap.Convert( PixelFormat.IA16 )
-			tpixmap.Discard()
+			tpixmap.Release()
 			
 			'Copy I->A
 			For Local y:=0 Until pixmap.Height
@@ -425,7 +418,7 @@ Class Image Extends Resource
 		
 			Local tpixmap:=pixmap
 			pixmap=pixmap.Convert( PixelFormat.RGBA32 )
-			tpixmap.Discard()
+			tpixmap.Release()
 			
 			'Copy Max(R,G,B)->A
 			For Local y:=0 Until pixmap.Height
@@ -442,18 +435,13 @@ Class Image Extends Resource
 		
 		Local image:=New Image( texture,shader )
 		
-		image.OnDiscarded+=Lambda()
-			pixmap.Discard()
-		End
-		
 		Return image
 	End
-
+	
 	Private
 	
 	Field _shader:Shader
 	Field _material:UniformBlock
-
 	Field _textures:=New Texture[4]
 	Field _blendMode:BlendMode
 	Field _color:Color
@@ -482,8 +470,6 @@ Class Image Extends Resource
 		_shader=shader
 		_material=New UniformBlock( 2 )
 		
-		AddDependancy( _material )
-		
 		SetTexture( 0,texture )
 		
 		BlendMode=BlendMode.None
@@ -538,11 +524,14 @@ Class ResourceManager Extension
 		
 		Local image:=Cast<Image>( OpenResource( slug ) )
 		If image Return image
-
+		
 		Local texture:=OpenTexture( path,Null )
-		If texture image=New Image( texture,shader )
+		If Not texture Return Null
 		
+		image=New Image( texture,shader )
+
 		AddResource( slug,image )
+
 		Return image
 	End
 

+ 29 - 17
modules/mojo/graphics/texture.monkey2

@@ -106,7 +106,6 @@ Class Texture Extends Resource
 #If Not __DESKTOP_TARGET__
 		If Not IsPow2( _size.x,_size.y ) _flags&=~TextureFlags.Mipmap
 #Endif
-
 		_glTarget=_flags & TextureFlags.Cubemap ? GL_TEXTURE_CUBE_MAP Else GL_TEXTURE_2D
 		_glInternalFormat=glInternalFormat( _format )
 		_glFormat=glFormat( _format )
@@ -148,7 +147,7 @@ Class Texture Extends Resource
 		If Not (_flags & TextureFlags.Dynamic)
 			_managed=New Pixmap( width,height,format )
 			_managed.Clear( Color.Magenta )
-			AddDependancy( _managed )
+			Discarded+=_managed.Release
 		Endif
 	End
 	
@@ -221,9 +220,7 @@ Class Texture Extends Resource
 		
 		Local texture:=New Texture( pixmap,flags )
 		
-		texture.OnDiscarded+=Lambda()
-			pixmap.Discard()
-		End
+		texture.Discarded+=pixmap.Release
 		
 		Return texture
 	End
@@ -260,11 +257,13 @@ Class Texture Extends Resource
 			Next
 		Endif
 			
-		If pspec pspec.Discard()
+		If pspec pspec.Release()
 			
 		Local texture:=New Texture( pnorm,Null )
-		Return texture
 		
+		texture.Discarded+=pnorm.Release
+		
+		Return texture
 	End
 	
 	Function ColorTexture:Texture( color:Color )
@@ -280,7 +279,12 @@ Class Texture Extends Resource
 		Endif
 		Return texture
 	End
-
+	
+	Function FlatNormal:Texture()
+		
+		Return ColorTexture( New Color( .5,.5,1 ) )
+	End
+	
 	'***** INTERNAL *****
 		
 	#rem monkeydoc @hidden
@@ -377,7 +381,6 @@ Class Texture Extends Resource
 					If _managed
 						Local image:=_managed.Window( offsets[i*2]*Width,offsets[i*2+1]*Height,Width,Height )
 						If _flags & TextureFlags.Mipmap
-'							UploadTexImage2D( cubeFaces[i],image )
 							UploadTexImageCubeMap( cubeFaces[i],image )
 							_dirty&=~Dirty.Mipmaps
 						Else
@@ -422,9 +425,6 @@ Class Texture Extends Resource
 	Method OnDiscard() Override
 	
 		If _glSeq=glGraphicsSeq
-'			For Local i:=0 Until 8
-'				If _bound[i]=_glTexture _bound[i]=0
-'			Next
 			glDeleteTextures( 1,Varptr _glTexture )
 		Endif
 		
@@ -433,6 +433,15 @@ Class Texture Extends Resource
 		_discarded=True
 	End
 	
+	#rem monkeydoc @hidden
+	#end	
+	Method Finalize() Override
+		
+		If _glSeq=glGraphicsSeq
+			glDeleteTextures( 1,Varptr _glTexture )
+		Endif
+	End
+	
 	Private
 	
 	Enum Dirty
@@ -496,7 +505,7 @@ Class Texture Extends Resource
 			If width=1 And height=1 Exit
 			
 			Local hdata:=data.MipHalve()
-			data.Discard()
+			data.Release()
 			data=hdata
 			width/=2
 			height/=2
@@ -504,7 +513,7 @@ Class Texture Extends Resource
 		
 		Wend
 		
-		data.Discard()
+		data.Release()
 	End
 	
 	Method UploadTexImage2D( glTarget:GLenum,image:Pixmap )
@@ -546,7 +555,7 @@ Class Texture Extends Resource
 			
 			glFlush() 'macos nvidia bug!
 			
-			image.Discard()
+			image.Release()
 		
 		Endif
 		
@@ -565,9 +574,12 @@ Class ResourceManager Extension
 		If texture Return texture
 		
 		Local pixmap:=OpenPixmap( path,Null,True )
-		If pixmap texture=New Texture( pixmap,flags )
-				
+		If Not pixmap Return Null
+		
+		texture=New Texture( pixmap,flags )
+		
 		AddResource( slug,texture )
+		
 		Return texture
 	End
 

+ 15 - 0
modules/mojo/graphics/uniformblock.monkey2

@@ -10,9 +10,12 @@ Class UniformBlock Extends Resource
 	End
 	
 	Method New( uniforms:UniformBlock )
+		Self.New( uniforms._name )
+		
 		_name=uniforms._name
 		For Local i:=0 Until _uniforms.Length
 			_uniforms[i]=uniforms._uniforms[i]
+			SafeRetain( _uniforms[i].texture )
 		Next
 	End
 	
@@ -167,6 +170,10 @@ Class UniformBlock Extends Resource
 	'
 	Method SetTexture( uniform:String,value:Texture )
 		Local id:=GetUniformId( uniform )
+		
+		SafeRetain( value )
+		SafeRelease( _uniforms[id].texture )
+		
 		_uniforms[id].texture=value
 		_uniforms[id].type=Type.Texture
 		_seq=_gseq
@@ -190,6 +197,14 @@ Class UniformBlock Extends Resource
 		Return _seq
 	End
 	
+	Protected
+	
+	Method OnDiscard() Override
+		For Local i:=0 Until _uniforms.Length
+			SafeRelease( _uniforms[i].texture )
+		Next
+	End
+	
 	Private
 	
 	Global _gseq:Int