Sfoglia il codice sorgente

Added Int type support to Shader/UniformBlock.

Mark Sibly 8 anni fa
parent
commit
1a0e49663d

+ 4 - 0
modules/mojo/graphics/shader.monkey2

@@ -124,6 +124,10 @@ Class GLProgram
 			For Local u:=Eachin _uniforms[i]
 			For Local u:=Eachin _uniforms[i]
 			
 			
 				Select u.type
 				Select u.type
+				Case GL_INT
+				
+					glUniform1i( u.location,ublock.GetInt( u.uniformId ) )
+					
 				Case GL_FLOAT
 				Case GL_FLOAT
 				
 				
 					glUniform1f( u.location,ublock.GetFloat( u.uniformId ) )
 					glUniform1f( u.location,ublock.GetFloat( u.uniformId ) )

+ 46 - 38
modules/mojo/graphics/uniformblock.monkey2

@@ -46,14 +46,28 @@ Class UniformBlock Extends Resource
 		Return id
 		Return id
 	End
 	End
 
 
+	'***** Int *****
+	'	
+	Method SetInt( uniform:String,value:Int )
+		SetData( uniform,value,Type.Integer )
+	End
+	
+	Method GetInt:Int( uniform:String )
+		Return GetData<Int>( uniform,Type.Integer )
+	End
+	
+	Method GetInt:Int( id:Int )
+		Return GetDataPtr<Int>( id,Type.Integer )[0]
+	End
+	
 	'***** Float *****
 	'***** Float *****
 	'	
 	'	
 	Method SetFloat( uniform:String,value:Float )
 	Method SetFloat( uniform:String,value:Float )
-		SetFloatData( uniform,value,Type.Scalar )
+		SetData( uniform,value,Type.Scalar )
 	End
 	End
 	
 	
 	Method GetFloat:Float( uniform:String )
 	Method GetFloat:Float( uniform:String )
-		Return GetFloatData<Float>( uniform,Type.Scalar )
+		Return GetData<Float>( uniform,Type.Scalar )
 	End
 	End
 	
 	
 	Method GetFloat:Float( id:Int )
 	Method GetFloat:Float( id:Int )
@@ -63,11 +77,11 @@ Class UniformBlock Extends Resource
 	'***** Vec2f *****
 	'***** Vec2f *****
 	'
 	'
 	Method SetVec2f( uniform:String,value:Vec2f )
 	Method SetVec2f( uniform:String,value:Vec2f )
-		SetFloatData( uniform,value,Type.Vec2f )
+		SetData( uniform,value,Type.Vec2f )
 	End
 	End
 	
 	
 	method GetVec2f:Vec2f( uniform:String )
 	method GetVec2f:Vec2f( uniform:String )
-		Return GetFloatData<Vec2f>( uniform,Type.Vec2f )
+		Return GetData<Vec2f>( uniform,Type.Vec2f )
 	End
 	End
 	
 	
 	Method GetVec2fv:Float Ptr( id:Int )
 	Method GetVec2fv:Float Ptr( id:Int )
@@ -77,11 +91,11 @@ Class UniformBlock Extends Resource
 	'***** Vec3f *****
 	'***** Vec3f *****
 	'
 	'
 	Method SetVec3f( uniform:String,value:Vec3f )
 	Method SetVec3f( uniform:String,value:Vec3f )
-		SetFloatData( uniform,value,Type.Vec3f )
+		SetData( uniform,value,Type.Vec3f )
 	End
 	End
 
 
 	Method GetVec3f:Vec3f( uniform:String )
 	Method GetVec3f:Vec3f( uniform:String )
-		Return GetFloatData<Vec3f>( uniform,Type.Vec3f )
+		Return GetData<Vec3f>( uniform,Type.Vec3f )
 	End
 	End
 	
 	
 	Method GetVec3fv:Float Ptr( id:Int )
 	Method GetVec3fv:Float Ptr( id:Int )
@@ -91,11 +105,11 @@ Class UniformBlock Extends Resource
 	'***** Vec4f *****
 	'***** Vec4f *****
 	'	
 	'	
 	Method SetVec4f( uniform:String,value:Vec4f )
 	Method SetVec4f( uniform:String,value:Vec4f )
-		SetFloatData( uniform,value,Type.Vec4f )
+		SetData( uniform,value,Type.Vec4f )
 	End
 	End
 
 
 	Method GetVec4f:Vec4f( uniform:String )
 	Method GetVec4f:Vec4f( uniform:String )
-		Return GetFloatData<Vec4f>( uniform,Type.Vec4f )
+		Return GetData<Vec4f>( uniform,Type.Vec4f )
 	End
 	End
 	
 	
 	Method GetVec4fv:Float Ptr( id:Int )
 	Method GetVec4fv:Float Ptr( id:Int )
@@ -105,21 +119,21 @@ Class UniformBlock Extends Resource
 	'***** Color (really just Vec4f) *****
 	'***** Color (really just Vec4f) *****
 	'
 	'
 	Method SetColor( uniform:String,value:Color )
 	Method SetColor( uniform:String,value:Color )
-		SetFloatData( uniform,value,Type.Vec4f )
+		SetData( uniform,value,Type.Vec4f )
 	End
 	End
 
 
 	Method GetColor:Color( uniform:String )
 	Method GetColor:Color( uniform:String )
-		Return GetFloatData<Color>( uniform,Type.Vec4f )
+		Return GetData<Color>( uniform,Type.Vec4f )
 	End
 	End
 	
 	
 	'***** Mat3f *****
 	'***** Mat3f *****
 	'
 	'
 	Method SetMat3f( uniform:String,value:Mat3f )
 	Method SetMat3f( uniform:String,value:Mat3f )
-		SetFloatData( uniform,value,Type.Mat3f )
+		SetData( uniform,value,Type.Mat3f )
 	End
 	End
 
 
 	Method GetMat3f:Mat3f( uniform:String )
 	Method GetMat3f:Mat3f( uniform:String )
-		Return GetFloatData<Mat3f>( uniform,Type.Mat3f )
+		Return GetData<Mat3f>( uniform,Type.Mat3f )
 	End
 	End
 	
 	
 	Method GetMat3fv:Float Ptr( id:Int )
 	Method GetMat3fv:Float Ptr( id:Int )
@@ -130,26 +144,26 @@ Class UniformBlock Extends Resource
 	'
 	'
 	Method SetAffineMat3f( uniform:String,value:AffineMat3f )
 	Method SetAffineMat3f( uniform:String,value:AffineMat3f )
 		Local m:=New Mat3f( value.i.x,value.i.y,0, value.j.x,value.j.y,0, value.t.x,value.t.y,1 )
 		Local m:=New Mat3f( value.i.x,value.i.y,0, value.j.x,value.j.y,0, value.t.x,value.t.y,1 )
-		SetFloatData( uniform,m,Type.Mat3f )
+		SetData( uniform,m,Type.Mat3f )
 	End
 	End
 	
 	
 	Method GetAffineMat3f:AffineMat3f( uniform:String )
 	Method GetAffineMat3f:AffineMat3f( uniform:String )
-		Local m:=GetFloatData<Mat3f>( uniform,Type.Mat3f )
+		Local m:=GetData<Mat3f>( uniform,Type.Mat3f )
 		Return New AffineMat3f( m.i.x,m.i.y,m.j.x,m.j.y,m.k.x,m.k.y )
 		Return New AffineMat3f( m.i.x,m.i.y,m.j.x,m.j.y,m.k.x,m.k.y )
 	End
 	End
 	
 	
 	'***** Mat4f *****
 	'***** Mat4f *****
 	'
 	'
 	Method SetMat4f( uniform:String,value:Mat4f )
 	Method SetMat4f( uniform:String,value:Mat4f )
-		SetFloatData( uniform,value,Type.Mat4f )
+		SetData( uniform,value,Type.Mat4f )
 	End
 	End
 
 
 	Method SetMat4f( uniform:String,value:AffineMat4f )
 	Method SetMat4f( uniform:String,value:AffineMat4f )
-		SetFloatData( uniform,New Mat4f( value ),Type.Mat4f )
+		SetData( uniform,New Mat4f( value ),Type.Mat4f )
 	End
 	End
 
 
 	Method GetMat4f:Mat4f( uniform:String )
 	Method GetMat4f:Mat4f( uniform:String )
-		Return GetFloatData<Mat4f>( uniform,Type.Mat4f )
+		Return GetData<Mat4f>( uniform,Type.Mat4f )
 	End
 	End
 	
 	
 	Method GetMat4fv:Float Ptr( id:Int )
 	Method GetMat4fv:Float Ptr( id:Int )
@@ -177,7 +191,6 @@ Class UniformBlock Extends Resource
 	'
 	'
 	Method SetTexture( uniform:String,value:Texture )
 	Method SetTexture( uniform:String,value:Texture )
 		Local id:=GetUniformId( uniform )
 		Local id:=GetUniformId( uniform )
-		
 		_uniforms[id].texture=value
 		_uniforms[id].texture=value
 		_uniforms[id].type=Type.Texture
 		_uniforms[id].type=Type.Texture
 		_seq=_gseq
 		_seq=_gseq
@@ -207,12 +220,12 @@ Class UniformBlock Extends Resource
 	#end	
 	#end	
 	Method OnDiscard() Override
 	Method OnDiscard() Override
 		
 		
-		_uniforms=null
+		_uniforms=Null
 	End
 	End
 	
 	
 	Private
 	Private
 	
 	
-	Global _gseq:Int
+	Global _gseq:Int=1
 	Global _ids:=New StringMap<Int>[8]
 	Global _ids:=New StringMap<Int>[8]
 	
 	
 	Enum Type
 	Enum Type
@@ -225,6 +238,7 @@ Class UniformBlock Extends Resource
 		Mat4f=6
 		Mat4f=6
 		Texture=7
 		Texture=7
 		Mat4fArray=8
 		Mat4fArray=8
+		Integer=9
 	End
 	End
 	
 	
 	Struct Uniform
 	Struct Uniform
@@ -232,41 +246,35 @@ Class UniformBlock Extends Resource
 		Field texture:Texture
 		Field texture:Texture
 		Field arrayData:Mat4f[]
 		Field arrayData:Mat4f[]
 		Field floatData:Mat4f
 		Field floatData:Mat4f
-		
-		Method SetFloatData<T>( t:T,type:Type )
-			Cast<T Ptr>(Varptr floatData.i.x)[0]=t
-			Self.type=type
-		End
-		
-		Method GetFloatData<T>:T()
-			Return Cast<T Ptr>(Varptr floatData.i.x)[0]
-		End
-		
-		Method GetFloatPtr:Float Ptr()
-			Return Cast<Float Ptr>(Varptr floatData.i.x)
-		End
 	End
 	End
 
 
 	Field _name:Int
 	Field _name:Int
 	Field _seq:Int
 	Field _seq:Int
 	Field _uniforms:=New Uniform[64]
 	Field _uniforms:=New Uniform[64]
 	
 	
-	Method SetFloatData<T>( uniform:String,data:T,type:Type )
+	Method SetData<T>( uniform:String,data:T,type:Type )
 		Local id:=GetUniformId( uniform )
 		Local id:=GetUniformId( uniform )
-		_uniforms[id].SetFloatData( data,type )
+		Cast<T Ptr>( Varptr _uniforms[id].floatData )[0]=data
+		_uniforms[id].type=type
 		_seq=_gseq
 		_seq=_gseq
 		_gseq+=1
 		_gseq+=1
 	End
 	End
 	
 	
-	Method GetFloatData<T>:T( uniform:String,type:Type )
+	Method GetData<T>:T( uniform:String,type:Type )
+		Local id:=GetUniformId( uniform )
+		DebugAssert( _uniforms[id].type=type,"Invalid uniform type" )
+		Return Cast<T Ptr>( Varptr _uniforms[id].floatData )[0]
+	End
+
+	Method GetDataPtr<T>:T Ptr( uniform:String,type:Type )
 		Local id:=GetUniformId( uniform )
 		Local id:=GetUniformId( uniform )
 		DebugAssert( _uniforms[id].type=type,"Invalid uniform type" )
 		DebugAssert( _uniforms[id].type=type,"Invalid uniform type" )
-		Return _uniforms[id].GetFloatData<T>()
+		Return Cast<T Ptr>( Varptr _uniforms[id].floatData )
 	End
 	End
 	
 	
 	Method GetFloatPtr:Float Ptr( id:Int,type:Type )
 	Method GetFloatPtr:Float Ptr( id:Int,type:Type )
 		DebugAssert( _uniforms[id].type=type,"Invalid uniform type "+Int(_uniforms[id].type)+" expecting "+Int(type) )
 		DebugAssert( _uniforms[id].type=type,"Invalid uniform type "+Int(_uniforms[id].type)+" expecting "+Int(type) )
-		Return _uniforms[id].GetFloatPtr()
+		Return Cast<Float Ptr>( Varptr _uniforms[id].floatData )
 	End
 	End
 	
 	
 End
 End