Help with shader-based GLMax2D

BlitzMax Forums/BlitzMax Programming/Help with shader-based GLMax2D

Brucey(Posted 2014) [#1]
Hallo :o)

Here's what I currently have in the way of a shader-based GLMax2D module (which I've called GL2GLMax2D).

I know there are some rather talented individuals around who know their stuff, and I would appreciate some help getting it working with textures and the other drawing commands.



Thanks!


LT(Posted 2014) [#2]
Thanks for posting this. I won't have a chance to look at in depth until tomorrow evening, but I'm definitely motivated to do so. :)


Brucey(Posted 2014) [#3]
It's a bit of a mess. I usually tidy up once everything is working properly (and tend to refactor a lot too).

At the moment I see the texture drawing as a single thing, but I dunno if it is at all possible to batch them too?
Anything we can batch would be more efficient. As you can see, the plot and drawlines are effectively batched.


skidracer(Posted 2014) [#4]
Ideally non WRAP textures need to be arranged on an Atlas but BlitzMax game authors do that better than engines...

BlitzMax DX modules feature batching code that could be integrated, they batch between texture state changes in the form of large vertex buffers.

I plan to come back to this thread in the weekend cos I like:)


GfK(Posted 2014) [#5]
I plan to come back to this thread in the weekend cos I like:)
Music to my technically incompetent ears.


LT(Posted 2014) [#6]
It might help to know what is happening for you. Right now, I can't see it doing anything. For example, should line draws be working?

I only spent about ten minutes, but I compiled the module and then attempted to test it with the BreakOut game and with my own engine as a replacement for GLMax2D. Of course, BreakOut uses textures so I wasn't expecting to see anything and that's just what happened - a more or less blank screen.

I was actually expecting my engine to work, though, because the only part of GLMax2D that I'm using is the DrawLine functionality. The result was similar to BreakOut, which tells me that the GLGraphics context is somehow different.

EDIT: I'm also using shaders and have that set up in a different place, so I suppose they could also be interfering with each other.


LT(Posted 2014) [#7]
By the way, I had to change...

Method AttachGraphics:TMax2DGraphics( widget:Byte Ptr, flags )
to...
Method AttachGraphics:TMax2DGraphics( widget, flags )

In order to get it to compile at all. Otherwise, it gives me an "overriding method parameter type" error.

Is there something else I'm missing here?


Brucey(Posted 2014) [#8]
Is there something else I'm missing here?

The example code was set to build against bmx_ng. "widget" is really a pointer, and has to be declared as such to support 64-bit apps.

I'll change the example here as everyone is using the "official" BlitzMax at the moment ;-)


zzz(Posted 2014) [#9]
I posted the initial module hack I did in my old thread: http://www.blitzbasic.com/Community/post.php?topic=102876&post=1240436

Might or might not be work looking at :)


Brucey(Posted 2014) [#10]
You did some more work on it?


AdamStrange(Posted 2014) [#11]
Brucey - compiled fine here on mac
do you have some test files for it. I can do some optimising first though :)

I can also do some opengl and add a number of functions as well?


Derron(Posted 2014) [#12]
Compiled here too.

drawing a rectangle + "rectangle numbers" (the fps :D)

gl 2900
gl2: 2600

Not that these numbers count, but you see that it is "just" about 10% slower. When rendering 100 rectangles at random positions with random colors, it decreases to:
gl: 1870
gl2: 1750

So batching is already used? And if yes: is it slower because of using shaders?


Hmpf again something I cannot be a real helping hand.


bye
Ron


Derron(Posted 2014) [#13]
To make "pixmaps" get drawn on my system, I replaced just one line:


	Method DrawPixmap( p:TPixmap,x,y )
...
		glRasterPos2i 0,0   'old
		glRasterPos2i -1,1   'new


This made pixmaps get drawn at the right spot, somehow raster "0,0" means centered at the screen, instead of bottom-left, so we moved to the left, and to the "top" - i think so.

When not using "SetBlend SOLIDBLEND" you can even ALPHABLEND Pixmaps (with alpha channels). Somehow I think the intention is to replace that whole drawPixmap-part with something "shader"-ish.



bye
Ron


zzz(Posted 2014) [#14]
@Brucey
Not really, its the same thing I mailed you ages ago. It do have working textures (for me at least), so Ill see if I can figure out what is breaking it for other people.

Btw, the batching example should do image batching just fine (if textures starts to work that is). Just have a new texture id trigger a flush.


Brucey(Posted 2014) [#15]
drawing a rectangle + "rectangle numbers" (the fps :D)

And what difference do you get when you draw 500 rectangles per frame?


Derron(Posted 2014) [#16]
rects	gl		gl2
@10     2800    2510
@100    1390    1300
@1000   200     200
@10000  22      22
@50000  5-6     5


Seems it takes a while until the batching-overhead is no longer an issue. Maybe this changes with more realistic scenarios (textured quads).


bye
Ron


LT(Posted 2014) [#17]
Found the problem. UpdateLayout() and Use() are never getting called by the DrawTexture() method. It should start like this...
Method DrawTexture( u0:Float, v0:Float, u1:Float, v1:Float, x0:Float, y0:Float, x1:Float, y1:Float, tx:Float, ty:Float )

  FlushTest( 0 )
  activeProgram = defaultTextureProgram
  activeProgram.Use()
  activeProgram.UpdateLayout()

Looks like there are some issues with the layout of the verts, but otherwise textures are working fine.


Brucey(Posted 2014) [#18]
Cool :-)

Found the problem.

This is what happens when I just type randomly at the keyboard... (on account of not really knowing what I'm doing)


Derron(Posted 2014) [#19]
@LT
What is your change supposed to do (just for the personal matter of being interested). Should it fix some image drawing issues (they are drawn fine here before and after adding your changes).


bye
Ron


LT(Posted 2014) [#20]
I was testing DrawText, which uses Draw, which uses DrawTexture. Without the change, DrawText doesn't work. I was also testing it with the BreakOut sample. Before the change, nothing draws. After the change, it works but the vertices are in the wrong order, so that's a separate issue (but easily fixed).

Draw image does not appear to be using shaders at all, btw.


Derron(Posted 2014) [#21]
Ahh ok ... I assumed "Type TGLImageFrame Extends TImageFrame" automagically takes care of "DrawImage" ...


LoadImage -> TImage.Load -> TImage.Create (which calls "New TImageFrame")

In our case we use "gl2max2d" - which has "TGLImageFrame" as extension of TImageFrame (needed because "draw" is abstract in TImageFrame).

This TGLImageFrame uses "_driver.DrawTexture(...)" to draw itself.


When calling "DrawImage" this is done:
DrawImage -> ImageFrame.Draw -> _driver.DrawTexture()

This means that "DrawImage" calls the "DrawTexture()"-method you appended that two lines (you could also uncomment that "print draw" and include a DrawImage in your loop) .

Hmm did I overlook something?


But like said: I got images displayed before and after your changes.
But I have to admit: you are right that displaying "fonts" does not work at all without your lines - using them the "basic system font" looks kind of garbled (missing parts).


EDIT: somehow when removing your edits .. it no longer displays the image (which did before... hmpff I surely mixed something before so it used "vanilla code") .

EDIT2: Now my image is also only displayed like a "pacman" (like if a piece of a pie is missing). Seems your vertice-assumption is correct.

EDIT3: to save having a local image:




EDIT4:
Ok, so "DrawTexture" does two things (next to the shaders):
- assigns UV coordinates (topleft, topright, bottomright, bottomlleft. values are scaled - mabye because of "power of two" textures)
- assigns screen position of 4 vertices (topleft, topright, bottomright, bottomleft)


-> uv values seem correct
-> vertice position seems right, uv too - but as it does not draw 2 triangles but something else... I assume something is wrong - eg vertice order (order for two triangles should be v0, v1, v2, then v2, v1, v3)

But I think it is more a problem of "glDrawArrays(GL_TRIANGLE_STRIP, 0, index)" - which is used to do degenerated quads - for sprite batching or so.

When playing a bit with the UV values (just set them to 0,0 or screenwidth,0 etc.) you will see that there are 3 triangles each getting the same "uv texture" - but of course filling each of the triangles individually).

EDIT5: Ok, you need to adjust uv AND vertices. Will post the change later.


bye
Ron


Derron(Posted 2014) [#22]
Replace DrawTexture with this one to make images (includes Imagefonts) work



LT(Posted 2014) [#23]
Apologies ... I was looking at DrawPixmap().

This version of DrawTexture() is better, but doesn't take into account the "frame" parameter in DrawImage(). Try the BreakOut sample to see what I mean.


Derron(Posted 2014) [#24]
think the problem is something else ... as soon as I try to draw a "normal image" and an "animimage" it corrupts both images - this sounds as if a state does not get processed finally/totally.

bye
Ron