Page 1 of 1

Batching for rectangles, points, lines?

Posted: Fri Apr 10, 2015 2:21 pm
by Whatthefuck
I remember there was a user-made library called "pointbatch" which allowed batching things like rectangle and points, though I'm not sure whether it works in the newest version anymore. Is there a possibility you guys could make it an actual thing and add something like that natively to love2d? Performance wise this can be a huge win for things like GUI rendering.

Re: Batching for rectangles, points, lines?

Posted: Fri Apr 10, 2015 2:29 pm
by szensk
Why not draw to a canvas and only redraw to the canvas when the GUI layout changes?

You can batch rectangles & lines really easily: add a 1x1 pixel image with the desired width/height/rotation to a spritebatch.

Re: Batching for rectangles, points, lines?

Posted: Fri Apr 10, 2015 3:04 pm
by T-Bone
As szensk mentions, canvases can do that and much more. Is there anything a "pointbatch" would do easier or faster than how a canvas works?

I guess the same question applies to Spritebatches too...

Re: Batching for rectangles, points, lines?

Posted: Fri Apr 10, 2015 3:42 pm
by s-ol
T-Bone wrote:As szensk mentions, canvases can do that and much more. Is there anything a "pointbatch" would do easier or faster than how a canvas works?

I guess the same question applies to Spritebatches too...
SpriteBatches are faster than drawing all images seperately (onto canvas or screen). A Mesh is basically the equivalent of a SpriteBatch for non-AABBs (Quads)
Meshes also support points, i am not sure why GL_LINES is not exposed... though thinking about it, it's probably because lines are not actually OpenGL lines but are hand-drawn by löve to enable things like caps etc.

So if you want to draw lines with a VBO you will need to tesselate them by yourself and build a mesh with them.

Re: Batching for rectangles, points, lines?

Posted: Fri Apr 10, 2015 4:00 pm
by Whatthefuck
szensk wrote:Why not draw to a canvas and only redraw to the canvas when the GUI layout changes?

You can batch rectangles & lines really easily: add a 1x1 pixel image with the desired width/height/rotation to a spritebatch.
Because in the situation that I need to use rects in, that's still slower. I wouldn't be asking for a pointbatch if I didn't know what I was talking about.

Re: Batching for rectangles, points, lines?

Posted: Fri Apr 10, 2015 4:23 pm
by szensk
Whatthefuck wrote:Because in the situation that I need to use rects in, that's still slower. I wouldn't be asking for a pointbatch if I didn't know what I was talking about.
OK then. What's the issue with a sprite batch of a 1x1 image with the width + height of the rectangle as scale? It works fine for many thousand of rectangles and definitely isn't slower than a sprite batch, because it is a sprite batch.

Anything more complicated than a rectangle and you'll have to use meshes, as mentioned above.

Re: Batching for rectangles, points, lines?

Posted: Fri Apr 10, 2015 4:53 pm
by slime
You can also use a [wiki]Mesh[/wiki] as a 'point batch'. Create a new Mesh with the number of vertices set to the maximum number of points you want, and use the "points" draw mode with the texture argument left at nil. You can use [wiki]Mesh:setVertex[/wiki] and [wiki]Mesh:setDrawRange[/wiki] to modify where the points are drawn and what color they are, and how many points are drawn.

Generally I'd recommend just using the rectangle approach (via spritebatches, as mentioned above) instead, though.

Something like a shape batch and a line batch might potentially make it into LÖVE 0.10.0.

Re: Batching for rectangles, points, lines?

Posted: Fri Apr 10, 2015 6:28 pm
by Whatthefuck
szensk wrote:
Whatthefuck wrote:Because in the situation that I need to use rects in, that's still slower. I wouldn't be asking for a pointbatch if I didn't know what I was talking about.
OK then. What's the issue with a sprite batch of a 1x1 image with the width + height of the rectangle as scale? It works fine for many thousand of rectangles and definitely isn't slower than a sprite batch, because it is a sprite batch.

Anything more complicated than a rectangle and you'll have to use meshes, as mentioned above.
Because calling :set on a spritebatch every frame is much more expensive than rendering a rect.
slime wrote:You can also use a [wiki]Mesh[/wiki] as a 'point batch'. Create a new Mesh with the number of vertices set to the maximum number of points you want, and use the "points" draw mode with the texture argument left at nil. You can use [wiki]Mesh:setVertex[/wiki] and [wiki]Mesh:setDrawRange[/wiki] to modify where the points are drawn and what color they are, and how many points are drawn.

Generally I'd recommend just using the rectangle approach (via spritebatches, as mentioned above) instead, though.

Something like a shape batch and a line batch might potentially make it into LÖVE 0.10.0.
Okay, that mesh part is exactly what I was looking for, thanks a lot.

Re: Batching for rectangles, points, lines?

Posted: Fri Apr 10, 2015 6:51 pm
by slime
Whatthefuck wrote:calling :set on a spritebatch every frame is much more expensive than rendering a rect.
It shouldn't be, especially if you use LÖVE 0.9.2.

Every love.graphics.rectangle call has to upload the rectangle's vertices to the GPU and issue a draw call. The spritebatch approach just sets the values of the vertices (without uploading them), then uploads all modified vertices at once later, and issues a single draw call for all sprites/rectangles in the batch.

Re: Batching for rectangles, points, lines?

Posted: Wed Jun 10, 2015 8:23 am
by Whatthefuck
slime wrote:
Whatthefuck wrote:calling :set on a spritebatch every frame is much more expensive than rendering a rect.
It shouldn't be, especially if you use LÖVE 0.9.2.

Every love.graphics.rectangle call has to upload the rectangle's vertices to the GPU and issue a draw call. The spritebatch approach just sets the values of the vertices (without uploading them), then uploads all modified vertices at once later, and issues a single draw call for all sprites/rectangles in the batch.
Wait, the :set call is now much cheaper than it was in 0.9.1? Because I remember :bind'ing spritebatches, then calling :set, and yet it was still much slower than rendering a rect. (in 0.9.1)
Sorry for the necro-bump, just need a small clarification. :)

EDIT: Just tried doing that, it is much more expensive to call :set every frame instead of drawing a rect, unfortunately