Page 1 of 1

love.graphics.newShader and instances

Posted: Tue May 01, 2018 2:33 pm
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)?

Re: love.graphics.newShader and instances

Posted: Tue May 01, 2018 8:06 pm
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.

Re: love.graphics.newShader and instances

Posted: Tue May 01, 2018 9:48 pm
by pgimeno
If the shader code is the same and the parameters are "configured" using shader:send, you only need one shader instance.

Re: love.graphics.newShader and instances

Posted: Tue May 01, 2018 11:56 pm
by raidho36
Yes and switching parameters using "send" will be slower than switching shaders - as I said.

Re: love.graphics.newShader and instances

Posted: Wed May 02, 2018 2:06 am
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.

Re: love.graphics.newShader and instances

Posted: Wed May 02, 2018 5:02 am
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.

Re: love.graphics.newShader and instances

Posted: Wed May 02, 2018 10:32 am
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.

Re: love.graphics.newShader and instances

Posted: Wed May 02, 2018 6:15 pm
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.

Re: love.graphics.newShader and instances

Posted: Wed May 02, 2018 6:56 pm
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

Re: love.graphics.newShader and instances

Posted: Wed May 02, 2018 7:01 pm
by raidho36
Well as I said, baking few specific values would make no sense there, so I assume that's not the purpose.