• Advertisement
Sign in to follow this  

Why do clipping?

This topic is 2157 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I was just wondering.

Why do clipping? I can't find any info why clipping is useful. I mean, it doesn't reduce the data that is sent to the GPU.. and it only costs more processing time to clip a polygon against a plane..

So.. why is clipping useful?

Share this post


Link to post
Share on other sites
Advertisement
Hidden
Ah the usual misinterpretation of clipping being the same as culling. With clipping I mean a polygon which is to be 'cut' in pieces by a viewing frustum plane.

Share this post


Link to post
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

Ah the usual misinterpretation of clipping being the same as culling. With clipping I mean a polygon which is to be 'cut' in pieces by a viewing frustum plane (the polygon is intersecting the plane)

[/font]

Share this post


Link to post
Share on other sites
Because you can draw a primitive much faster when you can simply assume that all its pixels are within the bounds you are trying to draw. Clipping a primitive is really quite cheap, and it's already-low cost is further reduced by amortization across however many pixels would have been in the non-clipped primitive -- if you assume triangles averaging only 25 pixels in area, the cost to clip it is probably less than 1 instruction per pixel. The cost is more-or-less fixed (a point or line can never result in more than one point or line, but triangles can result in upwards of 4-5 new triangles after clip).

The only other alternative would be to have a conditional test on each pixel, trivial rejection costs at least one conditional, up to four for the worst case (non-clipped pixels), per pixel -- at, say, 2 instructions per conditional that's an order of magnitude more work just do test boundaries, then, you also have to interpolate at least the coordinates for all pixels, even ones that won't be drawn, so probably 2 orders of magnitude. The disparity gets worse the more of the primitive is out of bounds.

In short, it's almost always far better to choose O(1) than O(n), even when the function of O(1) seems to have a sizable fixed-cost.

It's all about avoiding computations further down the pipeline -- remember, the fastest way to do work is to avoid having to do it at all.

Share this post


Link to post
Share on other sites
Alright, thanks for your answer.

Is it normal practise to use IDirect3DDevice9::SetClipPlane everytime the view frustum changes?
Or is it possible to enable Direct3D clipping but not having to use SetClipPlane yourself everytime the projection/view changes?

Share this post


Link to post
Share on other sites
SetClipPlane is used for user-defined clipping planes, in addition to the 6 planes of the projection frustum. Clipping against the default 6 planes happens automatically and can't be enabled/disabled.

So you only need to use SetClipPlane if you've got extra, custom planes to clip against.

Share this post


Link to post
Share on other sites
I see.

Well when you do the clipping yourself, you ca clearly see in wireframe mode how the clipping is happening.

However, if I don't do clipping myself, then in wireframe I don't see DirectX doing any clipping for me..

So, I don't think DirectX is clipping for us

Share this post


Link to post
Share on other sites
Can you provide screenshots? You shouldn't be able to see the clipping that Direct3D does, since it's clipping things that are already offscreen. So if you were able to see them you'd see what's off the screen.

Share this post


Link to post
Share on other sites
Software clipping on:
clipping_on.png

Software clipping off:
clipping_off.png

As you can see, DirectX does not provide clipping for us, atleast not automatically

Share this post


Link to post
Share on other sites
I cannot say how Direct3D handles it, but in OpenGL, the edge flags are automatically set for clipped lines in wireframe mode so that only the line segments that are a part of the primitives you actually send to the API are drawn. Internal edges that are generated by the clipper are not drawn. In your software clipper, the internal edges due to clipping are actually passed to the API, and so the API has no option but to draw them. That's the difference between your own clipper and the automatic clipper in OpenGL at least. I would not be surprised if Direct3D handles it in a similar way.

Share this post


Link to post
Share on other sites
From the SDK:
Primitive Clipping State (Direct3D 9)
Primitives that lie partially inside (or completely outside) the view frustum can be clipped so that only the visible portion of the primitive will be rendered. Clipping reduces the amount of work that is done to only those primitives or portions of a primitive that will be visible.

To use the pipeline for clipping, set the D3DRS_CLIPPING render state to TRUE (the default value) to enable clipping, or to FALSE to disable Direct3D clipping.[/quote]

Quite clearly, unless you explicitly disable clipping yourself, it gets done.

As to why do it? You've only considered the early stages of the pipeline in your OP. The pixel shader must also run on unclipped primitives, and that's where the real performance saving is.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement