Просмотр исходного кода

Added WIP resource manager support to graphics classes.

Mark Sibly 9 лет назад
Родитель
Сommit
818700ea76

+ 18 - 15
modules/mojo/graphics/font.monkey2

@@ -1,6 +1,8 @@
 
 Namespace mojo.graphics
 
+Using std.resource
+
 #rem monkeydoc The Glyph struct.
 
 Glyph are used to store the individual character data for fonts.
@@ -30,7 +32,7 @@ The glyph struct is used to store the location, size and advance for individual
 All character image data for a font must A font must occupy a single image, 
 
 #end
-Class Font Extends std.resource.Resource
+Class Font Extends Resource
 
 	#rem monkeydoc Creates a new font.
 
@@ -115,28 +117,29 @@ Class Font Extends std.resource.Resource
 		Return font
 	End
 
-	#rem monkeydoc @hidden
-	#end
-	Function Open:Font( path:String,height:Float,shader:Shader=Null )
+	Private
 	
-		path=RealPath( path )
-		
-		Local slug:="Font:path="+path+"&height="+height+"&shader="+(shader ? shader.Name Else "")
+	Field _image:Image
+	Field _height:Float
+	Field _firstChar:Int
+	Field _glyphs:Glyph[]
+
+End
+
+Class ResourceManager Extension
+
+	Method OpenFont:Font( path:String,height:Float,shader:Shader=Null )
+	
+		Local slug:="Font:name="+StripDir( StripExt( path ) )+"&height="+height+"&shader="+(shader ? shader.Name Else "")
 		
 		Local font:=Cast<Font>( OpenResource( slug ) )
 		If font Return font
 		
-		font=Load( path,height )
+		font=Font.Load( path,height )
 		
 		AddResource( slug,font )
 		Return font
 	End
 
-	Private
-	
-	Field _image:Image
-	Field _height:Float
-	Field _firstChar:Int
-	Field _glyphs:Glyph[]
-
 End
+

+ 7 - 6
modules/mojo/graphics/fontloader.monkey2

@@ -64,7 +64,7 @@ Function LoadFont:Font( path:String,fheight:Float,shader:Shader )
 	'
 	Local tx:=0,ty:=0,texw:=0,texh:=0,maxh:=0
 	
-	Const MaxTexWidth:=64'1024
+	Const MaxTexWidth:=1024
 
 	For Local i:=0 Until numChars
 
@@ -94,7 +94,7 @@ Function LoadFont:Font( path:String,fheight:Float,shader:Shader )
 	texw=1 Shl Int( Ceil( Log2( texw ) ) )
 	texh=1 Shl Int( Ceil( Log2( texh ) ) )
 	
-	Print "path="+path+", height="+fheight+", texw="+texw+", texh="+texh
+'	Print "path="+path+", height="+fheight+", texw="+texw+", texh="+texh
 	
 	Local pixmap:=New Pixmap( texw,texh,PixelFormat.A8 )
 	pixmap.Clear( Color.None )
@@ -120,6 +120,8 @@ Function LoadFont:Font( path:String,fheight:Float,shader:Shader )
 		
 		pixmap.Paste( tmp,tx,ty )
 		
+		tmp.Discard()
+		
 		glyphs[i]=New Glyph( New Recti( tx,ty,tx+gw,ty+gh ),New Vec2f( slot->bitmap_left,ascent-slot->bitmap_top ),slot->advance.x Shr 6 )
 
 		maxh=Max( maxh,gh+1 )
@@ -135,10 +137,9 @@ Function LoadFont:Font( path:String,fheight:Float,shader:Shader )
 	
 	Local font:=New Font( image,height,firstChar,glyphs )
 	
-	font.OnDiscarded+=Lambda()
-		image.Discard()
-		pixmap.Discard()
-	End
+	font.AddDependancy( image )
+	
+	pixmap.Discard()
 	
 	Return font
 End

+ 29 - 61
modules/mojo/graphics/image.monkey2

@@ -1,6 +1,8 @@
 
 Namespace mojo.graphics
 
+Using std.resource
+
 #rem monkeydoc The Image class.
 
 An image is a rectangular array of pixels that can be drawn using one of the [[Canvas.DrawImage]] methods.
@@ -8,7 +10,7 @@ An image is a rectangular array of pixels that can be drawn using one of the [[C
 You can load an image from a file using one of the [[Load]], [[LoadBump]] or [[LoadLight]] functions.
 
 #end
-Class Image Extends std.resource.Resource
+Class Image Extends Resource
 
 	#rem monkeydoc Creates a new Image.
 	
@@ -41,9 +43,7 @@ Class Image Extends std.resource.Resource
 		
 		Init( texture,texture.Rect,shader )
 		
-		OnDiscarded+=Lambda()
-			texture.Discard()
-		End
+		AddDependancy( texture )
 	End
 
 	Method New( width:Int,height:Int,textureFlags:TextureFlags=Null,shader:Shader=Null )
@@ -52,15 +52,15 @@ Class Image Extends std.resource.Resource
 		
 		Init( texture,texture.Rect,shader )
 		
-		OnDiscarded+=Lambda()
-			texture.Discard()
-		End
+		AddDependancy( texture )
 	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
@@ -77,6 +77,8 @@ Class Image Extends std.resource.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
@@ -338,23 +340,6 @@ Class Image Extends std.resource.Resource
 		Return image
 	End
 	
-	#rem monkeydoc @hidden experimental!
-	#end	
-	Function Open:Image( path:String,shader:Shader=Null )
-	
-		path=RealPath( path )
-		
-		Local slug:="Image:path="+path+"&shader="+(shader ? shader.Name Else "null")
-		
-		Local image:=Cast<Image>( OpenResource( slug ) )
-		If image Return image
-		
-		image=Load( path,shader )
-
-		AddResource( slug,image )
-		Return image
-	End
-	
 	#rem monkeydoc Loads a bump image from file(s).
 	
 	`diffuse`, `normal` and `specular` are filepaths of the diffuse, normal and specular image files respectively.
@@ -388,26 +373,6 @@ Class Image Extends std.resource.Resource
 		Return image
 	End
 
-	#rem monkeydoc @hidden experimental!
-	#end	
-	Function OpenBump:Image( diffuse:String,normal:String,specular:String,specularScale:Float=1,flipNormalY:Bool=True,shader:Shader=Null )
-	
-		diffuse=RealPath( diffuse )
-		normal=RealPath( normal )
-		specular=RealPath( specular )
-
-		Local slug:="BumpImage:diffuse="+diffuse+"&normal="+normal+"&specular="+specular
-		slug+="&specularScale="+specularScale+"&flipNormalY="+Int( flipNormalY )+"&shader="+(shader ? shader.Name Else "")
-		
-		Local image:=Cast<Image>( OpenResource( slug ) )
-		If image Return image
-		
-		image=LoadBump( diffuse,normal,specular,specularScale,flipNormalY,shader )
-
-		AddResource( slug,image )	
-		Return image
-	End
-	
 	#rem monkeydoc Loads a light image from file.
 	#end
 	Function LoadLight:Image( path:String,shader:Shader=Null )
@@ -480,23 +445,6 @@ Class Image Extends std.resource.Resource
 		Return image
 	End
 
-	#rem monkeydoc @hidden experimental!
-	#end
-	Function OpenLight:Image( path:String,shader:Shader=Null )
-	
-		path=RealPath( path )
-		
-		Local slug:="Light:path="+path+"&shader="+(shader ? shader.Name Else Null)
-		
-		Local light:=Cast<Image>( OpenResource( path ) )
-		If light Return light
-		
-		light=Load( path,shader )
-		
-		AddResource( slug,light )
-		Return light
-	End
-
 	Private
 	
 	Field _shader:Shader
@@ -526,6 +474,8 @@ Class Image Extends std.resource.Resource
 		_shader=shader
 		_material=New UniformBlock
 		
+		AddDependancy( _material )
+		
 		SetTexture( 0,texture )
 		
 		BlendMode=BlendMode.None
@@ -563,3 +513,21 @@ Class Image Extends std.resource.Resource
 	End
 	
 End
+
+Class ResourceManager Extension
+
+	Method OpenImage:Image( path:String,shader:Shader=Null )
+
+		Local slug:="Image:name="+StripDir( StripExt( path ) )+"&shader="+(shader ? shader.Name Else "null")
+		
+		Local image:=Cast<Image>( OpenResource( slug ) )
+		If image Return image
+
+		Local texture:=OpenTexture( path,Null )
+		If texture image=New Image( texture,shader )
+		
+		AddResource( slug,image )
+		Return image
+	End
+
+End

+ 39 - 44
modules/mojo/graphics/texture.monkey2

@@ -1,6 +1,8 @@
 
 Namespace mojo.graphics
 
+Using std.resource
+
 #rem monkeydoc Texture flags.
 
 | TextureFlags	| Description
@@ -41,7 +43,7 @@ End
 
 #rem monkeydoc @hidden
 #end
-Class Texture Extends std.resource.Resource
+Class Texture Extends Resource
 
 	Method New( pixmap:Pixmap,flags:TextureFlags )
 	
@@ -57,6 +59,7 @@ Class Texture Extends std.resource.Resource
 		If _flags & TextureFlags.Unmanaged
 			PastePixmap( pixmap,0,0 )
 		Else
+			AddDependancy( pixmap )
 			_managed=pixmap
 		Endif
 		
@@ -76,10 +79,7 @@ Class Texture Extends std.resource.Resource
 		If Not (_flags & TextureFlags.Unmanaged)
 			_managed=New Pixmap( width,height,format )
 			_managed.Clear( Color.Magenta )
-			OnDiscarded+=Lambda()
-				_managed.Discard()
-				_managed=Null
-			End
+			AddDependancy( _managed )
 		Endif
 		
 	End
@@ -147,23 +147,6 @@ Class Texture Extends std.resource.Resource
 		Return texture
 	End
 	
-	#rem monkeydoc @hidden experimental!
-	#end
-	Function Open:Texture( path:String,textureFlags:TextureFlags )
-	
-		path=RealPath( path )
-		
-		Local slug:="Texture:path="+path+"&flags="+Int( textureFlags )
-		
-		Local texture:=Cast<Texture>( OpenResource( slug ) )
-		If texture Return texture
-		
-		texture=Load( path,textureFlags )
-		
-		AddResource( slug,texture )
-		Return texture
-	End
-	
 	Function LoadNormal:Texture( path:String,textureFlags:TextureFlags,specular:String,specularScale:Float=1,flipNormalY:Bool=True )
 
 		path=RealPath( path )
@@ -203,26 +186,6 @@ Class Texture Extends std.resource.Resource
 		
 	End
 	
-	#rem monkeydoc @hidden experimental!
-	#end
-	Function OpenNormal:Texture( path:String,textureFlags:TextureFlags,specular:String,specularScale:Float=1,flipNormalY:Bool=True )
-	
-		path=RealPath( path )
-		
-		specular=specular ? RealPath( specular ) Else ""
-		
-		Local slug:="NormalTexture:path="+path+"?flags="+Int( textureFlags )
-		slug+="?specular="+specular+"&specularScale="+specularScale+"&flipNormalY="+Int( flipNormalY )
-		
-		Local texture:=Cast<Texture>( OpenResource( slug ) )
-		If texture Return texture
-		
-		texture=LoadNormal( path,textureFlags,specular,specularScale,flipNormalY )
-		
-		AddResource( slug,texture )
-		Return texture
-	End
-
 	Function ColorTexture:Texture( color:Color )
 		Local texture:=_colorTextures[color]
 		If Not texture
@@ -364,6 +327,8 @@ Class Texture Extends std.resource.Resource
 	#end	
 	Method Bind( unit:Int,filter:TextureFilter )
 	
+		If _discarded Print "Binding discarded texture!"
+	
 		If _boundSeq<>glGraphicsSeq
 			_boundSeq=glGraphicsSeq
 			For Local i:=0 Until 8
@@ -406,8 +371,20 @@ Class Texture Extends std.resource.Resource
 	#rem monkeydoc @hidden
 	#end	
 	Method OnDiscard() Override
-		If _texSeq=glGraphicsSeq glDeleteTextures( 1,Varptr _glTexture )
-		If _fbSeq=glGraphicsSeq glDeleteFramebuffers( 1,Varptr _glFramebuffer )
+	
+		If _texSeq=glGraphicsSeq
+			For Local i:=0 Until 8
+				If _bound[i]=_glTexture _bound[i]=0
+			Next
+			glDeleteTextures( 1,Varptr _glTexture )
+		Endif
+		
+		If _fbSeq=glGraphicsSeq
+			glDeleteFramebuffers( 1,Varptr _glFramebuffer )
+		Endif
+		
+		_texSeq=0
+		_fbSeq=0
 		_glTexture=0
 		_glFramebuffer=0
 		_discarded=True
@@ -443,3 +420,21 @@ Class Texture Extends std.resource.Resource
 	Global _colorTextures:Map<Color,Texture>
 	
 End
+
+Class ResourceManager Extension
+
+	Method OpenTexture:Texture( path:String,flags:TextureFlags=Null )
+
+		Local slug:="Texture:name="+StripDir( StripExt( path ) )+"&flags="+Int( flags )
+		
+		Local texture:=Cast<Texture>( OpenResource( slug ) )
+		If texture Return texture
+		
+		Local pixmap:=OpenPixmap( path,Null,True )
+		If pixmap texture=New Texture( pixmap,flags )
+				
+		AddResource( slug,texture )
+		Return texture
+	End
+
+End

+ 12 - 1
modules/mojo/graphics/uniformblock.monkey2

@@ -3,7 +3,7 @@ Namespace mojo.graphics
 
 #rem monkeydoc The UniformBlock class.
 #end
-Class UniformBlock
+Class UniformBlock Extends Resource
 
 	#rem monkeydoc Sets a scalar uniform.
 	#end
@@ -102,6 +102,8 @@ Class UniformBlock
 	#end	
 	Method SetTexture( uniform:String,texture:Texture )
 		Local id:=GetUniformId( uniform )
+		If texture texture.Retain()
+		If _textures[id] _textures[id].Release()
 		_textures[id]=texture
 		_seq=_gseq
 		_gseq+=1
@@ -150,6 +152,15 @@ Class UniformBlock
 		Return _blockIds[ GetUniformId( uniform ) ]
 	End
 	
+	Protected
+	
+	Method OnDiscard() Override
+	
+		For Local t:=Eachin _textures
+			If t t.Release()
+		Next
+	End
+	
 	Private
 	
 	Field _seq:Int