miniB3D shaders: ideas, thoughts

BlitzMax Forums/MiniB3D Module/miniB3D shaders: ideas, thoughts

AdamRedwoods(Posted 2011) [#1]
I know this topic has been beaten to death and klepto has a minib3d extended version... but I looked it over and saw some things missing. It could also use a naming system close to miniB3D's, too.

So I started yet another shader type, was hoping to share it as it's own thing, but honestly would need to be yet another forking of minB3D. I actually would be hoping to call it v0.60 just to clarify the lineage. It would be the small differences version+ shaders.

My vision of the TShader class would be VERY VERY SIMPLE, but with some built-in shader effects.

I actually have a class working:
'....etc....

Graphics3D width,height,depth,mode

InitShaders()

Local bshader:TShaderBrush = TShaderBrush.LoadARB(mystream0,mystream1) ''wherever
Local lightshader:TShader = TShader.LoadARB("perpixellight/perpixel.vp","perpixellight/perpixel.fp")
		

Local cam:TCamera=CreateCamera()
PositionEntity cam,0,0,-5

Local light:TLight=CreateLight(2)
PositionEntity light,0,4,0
AmbientLight 20,20,20

Local tex:TTexture=LoadTexture("media/test.png")

Local cube:TMesh=CreateCube()
Local sphere:TMesh=CreateSphere(12)

PositionEntity cube,-3,0,0
PositionEntity sphere,-1,0,0
EntityTexture cube,tex
EntityTexture sphere,tex

bshader.BrushTexture(tex)
bshader.BrushColor(255,255,255)
'bshader.SetFBO()
sphere.PaintEntity( bshader )


Local time#

While Not KeyDown(KEY_ESCAPE)	And Not AppTerminate()		

	TurnEntity cube,0,1,0
	TurnEntity lightpivot,2,0,0
	
	time :+ 0.001
	bshader.SetFragmentLocalParam(0,[time,0,0,0]) ''changing a value within the shader
	
	lightshader.Active()
	RenderWorld()
	lightshader.Active(0)

	Flip

Wend
End


I'd like to make it even more simple, but you can see from the above code I'm using a ShaderBrush, which is an extended brush-- allows for painting of various entities with the same shader.
Plus using a generic global "light shader" which is a working per-pixel light shader.
I've got FBO's by calling myshader.SetFBO() , the whole jazz.


Here's where things get complicated: most interesting shaders (like bloom, glow, shadows) require multiple passes. Other engines like Ogre3D, Unity have special shader languages that setup these passes.

Instead, my idea is to do something like:
tex= TTexture.Create(..etc...)
myshader = TShader.LoadARB(vp,fp)
othershader = TShader.LoadGLSL(vp2, fp2)
myshader.AddFBOPass(tex)
myshader.AddShaderPass(othershader)
''could add more passes .AddFBOPass(tex) ...etc...
myshader.AddFBODisplay(tex) ''would post a quad during the rendering of this shader

which means the multipass is setup in that order, and called when 'myshader' is activated.


My questions are:
1. did klepto's extensions work for most people? If not, why not?
2. is there interest in TShaders.bmx or do most people just use their own?
3. does the above 'shader build' for multi-pass make sense? Are there potential problems that anyone sees? would a shader language mimicing Ogre3D or unity be better (obviously, but worth the extreme amount of work)?


Just curious, sharing my thoughts as a port minib3d to monkey.

Last edited 2011


ima747(Posted 2011) [#2]
A couple thoughts:
As much as I hate forking, I don't think it's appropriate to take over the official thread with a straight up version number without simon's blessing... no idea what he's cooking up and it could cause some confusion. But given how long we've been on the same official version and that 'small fixes' has taken over perhaps there's a fair chance of getting his blessing... If not I would title it based on what it is, i.e. MiniB3D 0.53 + Small Fixes + Shaders. The point of the core tree is to keep directly in line with Blitz3D, so adding new features is definitely out of that scope (not a bad thing at ALL, just out of the main branch scope).

1. I think klepto's implementation was fine in itself, but there were other issues with his extended version, chief of which was he abandoned it during a transition to a new internal structure so it became outdated compared to the main branch...
2. I've never gotten into shaders because I've never had the time to both learn how to write them AND learn how to use them. I'd be quite interested if I only had to do half the work to get started :0)
3. Not understanding shaders that deeply I'm no authority on the subject, but it seems relatively straight forwards... It might be more clear to us minib3ders if it was something like SetTexture() instead of AddFBOPass() but again, I don't understand shaders so maybe in context with more knowledge that makes the most sense... also TShader and TShader2 types could get confusing, which does what and why (again not much shader knowledge, but similar names are usually confusing)

Can't wait to hear more!

Last edited 2011


AdamRedwoods(Posted 2011) [#3]
Thanks for your input, I like all your points.
Sorry about the TShader2, that was a typo! :O)

I'll look for continued input from the community.


ima747(Posted 2011) [#4]
ah, if there is no TShader2 type, just TShader (again perhaps this is just my ignorance of shaders...) perhaps there should be one master structure and then you build out a list of shader passes under it? don't know how the shaders are built but adding one thing to something of the same type is usually reserved for recursive structures (like child/parent entites, where they can both be the same thing, but one acting affects things bellow it), vs a control structure like say a mesh, which contains lists of things under it, like vertexes...


AdamRedwoods(Posted 2011) [#5]
An example of how to extend the TShader that I've been working on. I've side-stepped the "TShader.AddPass" routines for extending the base class.

Next step is to create a glow/corona shaderbrush.

p.s. the glow brush would be COMPOSITED INTO the per-pixel lambert lighting model I already have running, so I'm compositing multiple shaders together, kindof a deferred shader system (kindof :P).


p.s.s. the idea is to allow easy creation of a shader library, so novices can just plug and go...

Type TGlowShaderBrush Extends TShaderBrush
	Global bshader:TShader 
	
	Method New()
		bshader = TShader.LoadARB(mystream0,mystream1) 
		SetFBO(Null, bshader)
		isActive = 1
	EndMethod
	
	Method RenderBrush(mesh:TMesh, cam:TCamera)
		bshader.Active(1)
		mesh.Update()
		bshader.Active(0)
	EndMethod

EndType

'' then all you have to do is....
Local glowshader:TGlowShaderBrush = New TGlowShaderBrush
sphere.PaintEntity( glowshader )



Last edited 2011


AdamRedwoods(Posted 2011) [#6]
A simple glow shader, works. You'll notice the screenshot includes per-pixel lighting, which is a global shader. The glow is a brush shader.

screenshot here:
http://i295.photobucket.com/albums/mm144/adamredwoods/simple_blue_screenshot.jp

Type TGlowShaderBrush Extends TShaderBrush
	Global blurshaderh:TShader
	Global blurshaderv:TShader
	Global inflateshader:TShader


	
	Method New()
		blurshaderh = TShader.LoadARB("blur/gaussianblur.vp","blur/gaussianblur.fp") 
		blurshaderv = TShader.LoadARB("blur/gaussianblur.vp","blur/gaussianblur.fp")
		
		inflateshader = TShader.LoadARB("blur/inflate.vp", "blur/inflate.fp")
		
		
		SetFBO()
		blurshaderh.SetFBO()
		blurshaderv.SetFBO()
		
		Active() ''since it's a brush, self-default to be Active()
		BrushBlend(3)

		shader.name="glow"
	EndMethod

	
	Method RenderBrush( mesh:TMesh, cam:TCamera)

		inflateshader.Active()
		inflateshader.SetVertexLocalParam(5, 1.3 ) ''scale

		mesh.EntityFX(32)

		mesh.Update() ''capture current renderbuffer
		inflateshader.Deactive()
		
		
		''setup our uniforms
		Local blursize:Float = 0.04
		blurshaderh.Active()
		blurshaderh.SetVertexLocalParam(5, EntityX(cam),EntityY(cam),EntityZ(cam) )
		blurshaderh.SetVertexLocalParam(6, blursize )
		blurshaderv.Active()
		blurshaderv.SetVertexLocalParam(5, EntityX(cam),EntityY(cam),EntityZ(cam) )
		blurshaderv.SetVertexLocalParam(6, blursize )

		
		blurshaderh.ClearFBO()
		blurshaderh.ProcessFBO(GetFBO() )
		
		blurshaderv.ClearFBO()
		blurshaderv.ProcessFBO(blurshaderh.GetFBO() )

		DisplayMainFBO()

		DisplayFBO( blurshaderv.GetFBO() )
		

	EndMethod

EndType


Last edited 2011

Last edited 2011


AdamRedwoods(Posted 2011) [#7]
I've now enabled the use of smaller textures in the shader pass stages. Now the blurs are looking good, and faster too!



Now on to normal shading.

Last edited 2011


SLotman(Posted 2011) [#8]
Looks good! Can't wait to try it :)


ima747(Posted 2011) [#9]
Indeed! Keep me commin!


Captain Wicker (crazy hillbilly)(Posted 2012) [#10]
I think some sort of support for shaders would be wonderful :D


Spacechimp(Posted 2012) [#11]
I agree


BLaBZ(Posted 2012) [#12]
What's it take to get this shader class to work with the current version of minib3d?


angros47(Posted 2012) [#13]
You could use OpenB3D: it supports shaders.


AdamRedwoods(Posted 2012) [#14]
Posted my old blitzmax TShader class here:
http://www.blitzmax.com/Community/posts.php?topic=99186

but it's unsupported since I'm doing minib3d+monkey (pc, mac, iOS, android, html5, xna)
http://monkeycoder.co.nz/Community/posts.php?topic=3897

Last edited 2012