Clip planes on NVIDIA with Shaders?

Started by
7 comments, last by Jerax 16 years, 4 months ago
My NVIDIA 8600GT supports user clip planes, but only when using the fixed pipeline. Is there any way to enable them when using shaders? I've occasionally come across places suggesting this is possible (e.g., a comment here suggests it works if you "write a clip distance to result.clip[0] in your vertex program"), but I don't know enough to know how to implement that? I'm using Cg and OpenGL if that makes any difference. Note, I've tried Oblique Depth Projection, but it doesn't work for me - please see this thread for details on that. Thanks in advance.

http://erebusrpg.sourceforge.net/ - Erebus, Open Source RPG for Windows/Linux/Android
http://conquests.sourceforge.net/ - Conquests, Open Source Civ-like Game for Windows/Linux

Advertisement
IIRC, clip planes are implemented as a texkill in the FFP's pixel shader anyway, so there's nothing to gain from enabling this mechanism. Is there a reason you can't test the world-space coordinate against the appropriate plane equation like everyone else? It's a two-liner.
Ring3 Circus - Diary of a programmer, journal of a hacker.
From what I've heard on Geforce 6&7 clip planes are implemented with texkill, so as to not change the results of rasterization due to floating point imprecision. It's much more efficient than doing it yourself though, because triangles completely on one side of the plane are not rasterized at all, and only triangles intersecting the clip plane use texkill. Geforce 8 however actually splits triangles and so you can get some nasty z-fighting with multipass techniques if you enable clip planes in later passes, so you have to use a depth bias. I get the z-fighting from ATI cards too so I can only assume they also do the geometry approach.
User clip planes together with shaders have some issues. In GLSL you would have to write to gl_ClipVertex. On some hardware that might not be enough, but I don't know if it's Nvidia or ATI hardware.

AFAIK on GF6/7 the user clip planes are implemented using texkill, but for GF8 and other DX10 hardware this is probably not an option since the geometry shader has to be feed with fully clipped geometry.
for many cases doing the clipping in the pixel shader is the easiest way. There are few gains from a user clip plane on current hardware in my experience...

To do clipping in a shader is easy:

the clip(x) function in HLSL for example clips out pixels if any component of x<0

so basically in your vertex shader just find where a vertex should be clipped using whatever method you want..(it's easy for rectilinear planes, a bit harder for other rotations).

Like say i want to clip all pixels below the coord (0,0,0) ..i define a variable x=worldpos.y and pass this to the pixel shader where all you do is call clip(x) .. and everything negative will be clipped out...
Thanks, doing clipping in the shaders with clip() seems to work fine!

OTOH, I came across this thread where people were suggesting this could give poor performance, I don't know what the current view on this is?

Still, it seems to work well enough, so I'm happy.

One thing, my scene currently mixes shaders with fixed pipeline (I haven't got round to writing shaders for everything, though I guess I could), and so I'm setting the OpenGL user clip plane as well, I don't know if this will cause problems on some cards (I mean, I would hope they either are implemented properly, or ignored).

http://erebusrpg.sourceforge.net/ - Erebus, Open Source RPG for Windows/Linux/Android
http://conquests.sourceforge.net/ - Conquests, Open Source Civ-like Game for Windows/Linux

I found writing to gl_ClipVertex seems to be totally ignored (GeForce 7900 GT).
Quote:Original post by mdwh
Thanks, doing clipping in the shaders with clip() seems to work fine!

OTOH, I came across this thread where people were suggesting this could give poor performance, I don't know what the current view on this is?


It would give you less than optimal performance compared to fixed clip planes, for multiple reasons. The shader has to be executed for all pixels even those that are clipped, the early z acception/rejection can also be disabled on some hardware if you kill pixels in the shader (and early z is one of the nice thing of modern 3d hardware).

Of course if you're less concerned by performance, more with flexibility (you can have a larger number of clip planes with clip()) and/or with depth invariance (to avoid z-fighting) then clip is a good solution (you can also fight z-fighting with a depth bias of course).

LeGreg
GF 6 and 7 have 6 hardware clip planes (as in real clip planes that clip before the fragment shader, not texkill). I've had some very significant speed improvements by using them to reduce fragment shader load. However, I've only ever used them from D3D, so I'm not sure how you'd enable them in OGL (if it's even possible).

In fact according to a quick search in the driver caps database, nvidia cards ahve had clip planes since GF FX series cards, and ATI since Radeon 7500.

This topic is closed to new replies.

Advertisement