0
\$\begingroup\$

Context : I'm writing a 2D mobile game in Unity where the graphics are essentially black lines drawn on a background image plus some lighting. Running full RGB calculations for every step seems like a waste of processing power. What I'd like to do is render everything in [grey, alpha] and then multiply by the sRGB background image in a separate shader at the end. Actually, ideally, I'd like basically to specify custom components like [grey, highlight, additive, alpha], each of which can be ignored in a given pass, and then do a custom operation with those when I apply the background image, like ceil(src*(dst.r + dst.g)) + half4(dst.b). I can figure out how to do that between 2 textures within a shader, but not between texture and screen (a.k.a. src & dst).

Question : Basically, is there a way of writing a per-component replacement of the built-in Blend functions? Because something like Blend SrcAlpha OneMinusSrcAlpha only works with the full float4 or half4 vectors.

Or do I need to grab the screen like a texture and handle it within a pass block, and then do Blend Off or Blend One Zero? Or do something with RenderTextures perhaps? My intuition suggests that that would undo some of the efficiency I'd gained in previous steps, though...?

\$\endgroup\$
4
  • \$\begingroup\$ "do I need to grab the screen like a texture" Yes. What you're describing is post processing, and Unity has some built-in features like OnRenderImage to help with this. "My intuition suggests that that would undo some of the efficiency I'd gained in previous steps, though" Your intuition that you'd gained substantial efficiency in the previous steps is likely wrong. GPU cores are vectorized, so handling a 4-component colour is about as easy for them as manipulating a single float. \$\endgroup\$ Commented Oct 8, 2020 at 12:15
  • \$\begingroup\$ @DMGregory. Okay, that makes sense - I'll look into post-processing rather than sprite shaders. But concerning GPU core vectorization, would the same hold true for mobile? Sorry - should've specified that that was the target from the beginning (question edited accordingly). \$\endgroup\$ Commented Oct 8, 2020 at 12:29
  • \$\begingroup\$ Yes. In fact on mobile, writing out the results of a previous pass then reading them in as inputs to a new pass can be the bottleneck, due to the limited video memory/bandwidth. So you may in some cases worsen mobile performance by putting off work to a post-process that you could have done cheaply as you drew each sprite. Always build it the simple way first and profile the performance, to be sure you're solving the right problem and not creating extra trouble for yourself. \$\endgroup\$ Commented Oct 8, 2020 at 13:04
  • \$\begingroup\$ Good advice; thanks. I have the simple version built already and definitely needing some serious optimisation, so I guess it's time to do that profiling then try this and a few potential optimisations and compare. \$\endgroup\$ Commented Oct 8, 2020 at 13:19

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.