Opinions on using Cg Clip()

Hi,

Writing a cg shader at the moment where I could use clip() to avoid rendering pixels.

Just wondering if anyone knows any good reasons not to use it? From the cg docs it says its the equivalent of using if(x<0) discard, so I guess in many ways its more about the use of discard. Though I’m a little concerned over the use of a conditional.

I’ve searched online but not found anything concrete, so was interested if anyone has had experience of using it and whether it might be a costly method (conditional statement/branching) or cause issues, perhaps on older or lesser hardware (e.g. mobile platforms)?

Although not as straightforward I can achieve the same thing by making alpha zero for these pixels, but I like the appeal of using clip() as it keeps the code clean and tidy.

Any thoughts?

Clip and discard are the same thing from different languages.

I’m not dealing with this useless Flash video’s lack of ability to scan – I gave it a shot and got too infuriated – but find what this guy has to say about alpha testing. It’s in there and should answer your questions.
http://video.unity3d.com/video/3716584/unite-11-optimizing-graphics

Branching is bad. Avoid it if possible, but the workaround may end up using so many GPU cycles that they outweigh the fact that the branch might be turning off processing on half of your cores. Also, overdraw/blending is bad! There’s no one-size-fits-all solution. Base your decision on your target hardware. If you have no target hardware, as Unity pushes you not to have, then code what takes the least time and hope for the best. :stuck_out_tongue:

Thanks Jessy,

Just scanned the video, found stuff on clip and alphaTesting about 30 mins in, though unsure if they equate to the same thing. The talk on Alpha-testing was to avoid if possible as the fragment will travel further through the pipeline. I.e. its bad to use alpha-testing if you want to cull areas, alpha blending is better. Again not sure if that directly relates to clip(), although they then go on to say the next gen chips will be almost as good at AT as AB.

I think for now I’ll follow you last bit of advice and just use clip(), i’ll worry about optimising it later for specific hardware.

I’m not even doing anything fancy, just dealing with floating point checks against alpha component and clip() make it easier currently to see if i’ve made any mistakes in my algorithms.

Yes. When the operation was fixed function, it had to be based on alpha values, as far as I understand. Programmable pixel shaders changed this.

We shall see. Maybe they’ll make strides into branch prediction for massively parallel architecture, to go with the GPGPU stuff for series 6. These people were behind the Dreamcast, so they can do anything, after all. :wink: Rumors are March / iPad 3.