Pixel-shaders and SpriteBatch

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
marco.lizza
Citizen
Posts: 52
Joined: Wed Dec 23, 2015 4:03 pm

Pixel-shaders and SpriteBatch

Post by marco.lizza »

Hello everybody.

As far as I understood, shaders are applied on a "draw()" call basis. That is, I prepare my SpriteBatch, set the pixel-shader, draw the SpriteBatch... and that's it!

That's fine.

But, let's imagine I would like to use SpriteBatch(es) for performance reasons and selectively apply a shaders only on some of the items of the batch. Is this possibile, in a way or another?

( for example, image a shooter using SpriteBatches and using a shader to highlight some sprite according to some conditions )

So far I ended in avoiding the use of SpriteBatches to get the result, but I'm not satisfied with it.

Thanks for your suggestions.
User avatar
bakpakin
Party member
Posts: 114
Joined: Sun Mar 15, 2015 9:29 am
Location: Boston

Re: Pixel-shaders and SpriteBatch

Post by bakpakin »

Its not possible in the way you described. Create a separate spritebatch for each of the shaders that you want to apply. Also, don't optimize for performance without profiling.
((_((_CRAYOLA_((_((_> GitHub <_((_((_CRAYOLA_((_(()
marco.lizza
Citizen
Posts: 52
Joined: Wed Dec 23, 2015 4:03 pm

Re: Pixel-shaders and SpriteBatch

Post by marco.lizza »

@bakpakin, I'm not optimizing *only* for performance reason (nor tipically "early optimize" without any clue). The point is that I'm working with some kind of (basic) tile-engine and SpriteBatch(es) do come really handy.

I could use a separate batch for the "highlighted" sprites, but there's also occlusion to be taken into account. So I should bind the batch to the map layers it pertains, in order to be drawn with the the correct z-order.

I still wonder is there's a better (simpler?) approach...
User avatar
bakpakin
Party member
Posts: 114
Joined: Sun Mar 15, 2015 9:29 am
Location: Boston

Re: Pixel-shaders and SpriteBatch

Post by bakpakin »

A simple approach is to draw each sprite independently, and only use sprite batches for background tiles that don't overlap and are all on the same layer.

If you really want to use a single sprite batch, you can use a single shader but pass custom attributes per sprite. In love 0.10.0, Meshes with custom vertex formats were added, allowing custom attributes to shaders. For your highlighting example, you could add an extra attribute of four numbers called "highlightColor" that your shader would use to highlight a sprite. Custom attributes is a great upgrade for love 0.10.0 that removed some constraints on what one could do easily with shaders in love. I haven't used this functionality yet, but I imagine it would be a great help.

As for a tiling engine with sprite batches, check out STI if you haven't already, which is already great and fully functional. If you don't want to use it, I recommend using it for an example.
((_((_CRAYOLA_((_((_> GitHub <_((_((_CRAYOLA_((_(()
marco.lizza
Citizen
Posts: 52
Joined: Wed Dec 23, 2015 4:03 pm

Re: Pixel-shaders and SpriteBatch

Post by marco.lizza »

Thanks again, @bakpakin.

In fact, the "simpler approach" I'm using right now is by handling the highlightable sprites independently, i.e. outside batches.

The "Mesh.attachAttribute" API seems really interesting. This evening I will go and try to use it. I'll let you know!

Also, thanks for pointing me to STI. For my current project I prefer not to use any externa library... but will surely use it, it looks pretty versatile (supporting custom layers with independent drawing function, that would probably solve my problem right off the bat)!
marco.lizza
Citizen
Posts: 52
Joined: Wed Dec 23, 2015 4:03 pm

Re: Pixel-shaders and SpriteBatch

Post by marco.lizza »

Nope. The "attachAttribute" API won't do.

I need to find another way to accomplish this. Probably a way could be in passing to the shader the screen coordinates of the regions to be highlighted. This should work.

However, I'm guessing that from a practical point of view is should be better to have pre-drawn highlighted variants of the objects and switch the atlas/quad when needed.
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Pixel-shaders and SpriteBatch

Post by pedrosgali »

I have a problem similar to this, I'm making a space game with procedurally generated planet textures. This worked fine for small maps with each planet having it's own canvas but as I increased the number of stars and planets map loading time went through the roof.
I then figured I'd just make some big images on game load and give each planet a quad and a reference id to the texture needed. When I cane to draw the quads however it seemed to ignore the shader completely and I was left with 2 spinning squares in space. The shader is a basic fisheye lens to make the squares look spherical, does anyone know how to fix this problem? I'm at work currently but I will post the code later on, sorry if this is off topic by the way.

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Pixel-shaders and SpriteBatch

Post by bobbyjones »

marco.lizza wrote:Nope. The "attachAttribute" API won't do.

I need to find another way to accomplish this. Probably a way could be in passing to the shader the screen coordinates of the regions to be highlighted. This should work.

However, I'm guessing that from a practical point of view is should be better to have pre-drawn highlighted variants of the objects and switch the atlas/quad when needed.
Could you share code or your reasoning behind why the attachAttribute api wouldn't work? With the attachAttribute api you should be able to pass in the four vertices of the sprite. But it will do it in an efficient way. With one draw call. Tomorrow I am going to experiment and see if I can do it.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests