love.graphics.newShader and instances

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
poke1024
Prole
Posts: 6
Joined: Tue May 01, 2018 2:25 pm

love.graphics.newShader and instances

Post by poke1024 »

Hi there, I have a rather technical question regarding love.graphics.newShader():

I have the same shader code running in a dozen different variations (with different parameters, configured on each shader instance using shader:send). I wonder if I need to call love.graphics.newShader() every time I need a new shader instance. This seems to recompile the shader each time (telling from the time it takes).

I wonder if I can just compile the shader code once and then use it to build 20 shader instances from that shader (without recompiling)?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: love.graphics.newShader and instances

Post by raidho36 »

Short answer is that you can't. You need to individually compile each instance. Do not use if-else statements within shader to do this - that will be slower.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: love.graphics.newShader and instances

Post by pgimeno »

If the shader code is the same and the parameters are "configured" using shader:send, you only need one shader instance.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: love.graphics.newShader and instances

Post by raidho36 »

Yes and switching parameters using "send" will be slower than switching shaders - as I said.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: love.graphics.newShader and instances

Post by pgimeno »

raidho36 wrote: Tue May 01, 2018 8:06 pm Short answer is that you can't. You need to individually compile each instance. Do not use if-else statements within shader to do this - that will be slower.
raidho36 wrote: Tue May 01, 2018 11:56 pm Yes and switching parameters using "send" will be slower than switching shaders - as I said.
As you said where exactly?

If recompiling each with different parameters is really the best option (I'd check first), you could as well recompile the shaders with the parameters built into them, and save the sends.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: love.graphics.newShader and instances

Post by raidho36 »

As you said where exactly?
That was implied. Excluding backwards approaches that drop performance to zero, your only way of doing this is through "send".

You'll be changing shaders anyway, so changing it to the right shader is free, compared to changing shader and then sending a bunch of data on top of that. Additionally, the if statement in shaders isn't free, on uniforms its cost normally minimal but not necessarily, it could still result in heavy performance penalties for just having it there. Every GPU programming book and programming manual says not to have if statements in the shader code unless absolutely necessary - and for good reason. Lastly, fewer milliseconds of one-time loading screen duration comes nowhere close to competing with fewer milliseconds of rendering time per frame, that's sort of a no-brainer.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love.graphics.newShader and instances

Post by zorg »

Just tell me this, when exactly does shader:send imply that the sent data will necessarily correlate to the code having a branch in it? Because from what i saw, this was the source of the miscommunication.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: love.graphics.newShader and instances

Post by raidho36 »

Because if code branches at anything other than a uniform (which you put there through "send"), performance will suffer badly. Which is what I also said.

That's not to mention, OP specifically says he's using "send" to control "variations", presumably by if statement - because making it a different size or color is hardly a "variation" and it wouldn't make sense to bake few specific values rather than leave it as variable.

I'm being intellectually charitable here, I assume that you have high intelligence - not the opposite. And so if that's not the case and you need me to spell out everything in one post so there's no further questions, you'll have to ask me. But I'd rather you don't, I hate the idea of being surrounded by idiots, so please flex your brain muscle, nothing I say is arcane or cryptic, and it all well follows basic logic.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love.graphics.newShader and instances

Post by zorg »

To me, variations can also imply that he's sending one number, for example, through :send, and does no if-like branches in the shader code at all, is all i'm saying. In that case, the thread ended with Pgimeno's first reply, after yours.

And to be perfectly honest, i'd rather not quote all previous posts and highlight the meaning i parsed out of each of them. : 3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: love.graphics.newShader and instances

Post by raidho36 »

Well as I said, baking few specific values would make no sense there, so I assume that's not the purpose.
Post Reply

Who is online

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