## Batching for rectangles, points, lines?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Whatthefuck
Party member
Posts: 106
Joined: Sat Jun 21, 2014 3:45 pm

### Batching for rectangles, points, lines?

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.

szensk
Party member
Posts: 155
Joined: Sat Jan 19, 2013 3:57 am

### Re: Batching for rectangles, points, lines?

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.

T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

### Re: Batching for rectangles, points, lines?

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...

s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

### Re: Batching for rectangles, points, lines?

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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
baby:hurt(me)
end

Whatthefuck
Party member
Posts: 106
Joined: Sat Jun 21, 2014 3:45 pm

### Re: Batching for rectangles, points, lines?

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.

szensk
Party member
Posts: 155
Joined: Sat Jan 19, 2013 3:57 am

### Re: Batching for rectangles, points, lines?

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.

slime
Solid Snayke
Posts: 2904
Joined: Mon Aug 23, 2010 6:45 am
Contact:

### Re: Batching for rectangles, points, lines?

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.

Whatthefuck
Party member
Posts: 106
Joined: Sat Jun 21, 2014 3:45 pm

### Re: Batching for rectangles, points, lines?

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.

slime
Solid Snayke
Posts: 2904
Joined: Mon Aug 23, 2010 6:45 am
Contact:

### Re: Batching for rectangles, points, lines?

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.

Whatthefuck
Party member
Posts: 106
Joined: Sat Jun 21, 2014 3:45 pm

### Re: Batching for rectangles, points, lines?

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

### Who is online

Users browsing this forum: No registered users and 24 guests