PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

3d: different sorting techniks.

I'm curious, folks.

Those of you who do 3d graphics in the games: What kind of hidden surface technik do you use?


  • Zbuffer, (maybe with a loose front-back sort).

  • Painters algorithm (e.g. Sort back to front)

  • sorting based on bsp-trees

  • Something more clever such as span-buffers or so.


I'm a painters algorithm user... did so much ps-one coding that I still know how to do it fast.

Discuss :)

Nils
torus wrote:I'm curious, folks.

Those of you who do 3d graphics in the games: What kind of hidden surface technik do you use?


  • Zbuffer, (maybe with a loose front-back sort).

  • Painters algorithm (e.g. Sort back to front)

  • sorting based on bsp-trees

  • Something more clever such as span-buffers or so.


I'm a painters algorithm user... did so much ps-one coding that I still know how to do it fast.

Discuss :)

Nils

BSP is just a way to drive the painters algorithm, isn't it? Ultimately it depends on the nature of your scene graph. Can you share a little about the nature of your scene?

E.g. indoors, outdoors, terrain, just abstract objects...

- HM
Yeti uses raytracing and painters.
torus wrote:I'm curious, folks.

Those of you who do 3d graphics in the games: What kind of hidden surface technik do you use?


  • Zbuffer, (maybe with a loose front-back sort).

  • Painters algorithm (e.g. Sort back to front)

  • sorting based on bsp-trees

  • Something more clever such as span-buffers or so.


I'm a painters algorithm user... did so much ps-one coding that I still know how to do it fast.

Discuss :)


mine is zbuffer only, sorting by texture (I found it was faster than sorting front-back, the cache is the key here)
I tried an ordering table before, however I thought it wasn't that fast (at least my implementation) due to cache issues too (the polys were drawed "randomly", so all the cache was trashed everytime the texture was changed)

a clever span buffer would speed up my engine, imo.
BSP is just a way to drive the painters algorithm, isn't it?


Yes exactly. Traversing the BSP results in sorting.

mine is zbuffer only, sorting by texture (I found it was faster than sorting front-back, the cache is the key here)


I for hardware accelration sorting by texture is always a good idea to reduce state changes and texture cache misses.
For software only engines you gain at least the benefit of better locality (less data cach misses) but state changes are not a big issue. You may get better results by sorting front to back, which would safe you lots division operations (when doing perspective correct texturing), which are more expensive then data cache miss imo.
Tala wrote:I for hardware acceleration sorting by texture is always a good idea to reduce state changes and texture cache misses.
For software only engines you gain at least the benefit of better locality (less data cach misses) but state changes are not a big issue. You may get better results by sorting front to back, which would safe you lots division operations (when doing perspective correct texturing), which are more expensive then data cache miss imo.


I get 2 to 4fps more sorting by texture instead of sorting front to back, really. However I don't do a "lookup" of the zbuffer to see if the whole span is behind and doesn't need to be drawn (thus I'm not avoiding divisions).
I'll try to have a look at it to see if it improves.

However I don't have a big overdraw ratio, so I'm not very confident of improvements.

p.s. note that probably "software engines" are different than "my software engine". I still have a lot to learn on the subject :)
Octrees provide fast HSR (hiddesn surface removal) and the same data can be used for collision detection, etc.

Depending on what exactly you are trying to render, octrees are a good choice.
exosyphen wrote:Octrees provide fast HSR (hiddesn surface removal) and the same data can be used for collision detection, etc.

Depending on what exactly you are trying to render, octrees are a good choice.

Well, octrees are just a special case of BSPs, so it really depends on the scene whether this is a suitable simplification...

- HM
Yow folks. Sorry for the delay.. I was busy over the weekend.

Well, basically my question boils down to zbuffer or not. I've done so much work on machines without zbuffer, that I get a sorting order almost as good as with zbuffer. It takes some more work on the artist side, but it's doable. Some special effects otoh are not possible that way.

I've experimented with polymappers, and to be honest I don't think the extra work you need for the zbuffer handling pays off. Caching and the slow memory access is most of the time worse than a extra texel color blending. It might pay off if the render itself does a lot of things, but I haven't seen a fast game on pocket pc with bilinear blended textures yet.

I would go and write a "engine" based on the painters algorithm. But otoh I did a lot of pc programming lately so I'm used to the ease of a zbuffer.

Guess I have to meditate over a decission one more week.

Nils
It is my experience that z-buffers have a huge (50% reduction) in performance. However I can't imagine the painter's algorithm being any better because of the massive overdraw.

If you are using a BSP then you have perfectly sorted polys for free. Use that to your advantage with near to far rendering combined with a span system to mark spans that have been rendered to.

You'll have to use your imagination as far as sorting polys into the mix that are not part of the BSP. That's where the z-buffer is most useful.

Dan East
Dan East wrote:If you are using a BSP then you have perfectly sorted polys for free. Use that to your advantage with near to far rendering combined with a span system to mark spans that have been rendered to.

You'll have to use your imagination as far as sorting polys into the mix that are not part of the BSP. That's where the z-buffer is most useful.

Dan East

That's pretty close to what I am planning to use. BSP + z-buffer write for the static scene, z-buffer test for the active parts. Makes a good general purpose engine core. But again, BSP is not the only data structure (hey, I even implemented Mulmuley at one point; even though not for a game but a molecular graphics tool).

- HM
span buffers have a big problem: They work very efficient for entirely opaque geometry, but they suck if you do pixel transparency (e.g. alpha test). Alpha blended or additive geometry also does not work without ugly hacks.

BSP's also only work for entirely static geometry and require poly splitting to be full efficient. I think a simply polypusher with playstation-1 style ordering table is once again the way to go for me.

But I'm still thinking.. :)
torus wrote:span buffers have a big problem: They work very efficient for entirely opaque geometry, but they suck if you do pixel transparency (e.g. alpha test). Alpha blended or additive geometry also does not work without ugly hacks.


I don't agree with you there. If you are drawing front to back with a span system then you know if the span is visible or not. That applies to blended spans as well. If the span needs to be blended then simply do not update the spans with the blended span (so as to not mark that span as rendered) and cache it for rendering after all other rendering is complete. Then render your cached spans from far to near - they are already sorted for you. It works perfectly in Varium.

Dan East
Dan is right!

- draw everything using a span buffer.
- if you encounter a transparent span, save it in a buffer (sorted buffer - use insertions sort).
-when you finished rendering, just render the transparent spans one at a time, as you have them sorted already.

If you do not wish to use either Z-Buffer or span buffers,a good approach would be something that resembles BSP trees. Make sure your polys are adjacent on their edges, if not, subdivide them and sort them by the smallest z value. I hope you follow this, if not, I can make some drawings.
exosyphen wrote:If you do not wish to use either Z-Buffer or span buffers,a good approach would be something that resembles BSP trees. Make sure your polys are adjacent on their edges, if not, subdivide them and sort them by the smallest z value. I hope you follow this, if not, I can make some drawings.

Sounds to me like one of
    Boissonnat, J. D., & Dobrindt, K. T. G. (1996). On-line construction of the upper envelope of triangles and surface patches in three dimensions. Comput. Geom. Theory Appl., 5, 303–320.

    Mulmuley, K. (1994). An Efficient Algorithm for Hidden Surface Removal, II. Journal of Computer and Systems Sciences, 49, 427–453.

...at least if you want to handle all potential cases correctly. Both articles describe pretty much the same algorithm, but from different perspectives. The algorithm is essentially a 2-1/2-d generalization of quicksort. If you know more about your scene, you can easily simplify (by replacing log-time tree structures by linear insertion/bubble sort). This stuff works nicely especially for high resolution images. However for a PDA game with a simple in-door environment the approach as proposed by Dan is probably more appropriate.

- HM
Perhaps the guy who started this thread could enlighten us all, and tell us what EXACTLY he want's to render ... so we can give better answers :)
exosyphen wrote:Perhaps the guy who started this thread could enlighten us all, and tell us what EXACTLY he want's to render ... so we can give better answers :)


Well, I don't have any specific plans. I'm just thinking about software rendering in general and wondered what the current state of the art approach is.

I've always toyed around with software polymappers in the past and even used them on our latest game (that schumacher kart racing game) as prototype mappers. Another coder wrote the polymappers in assembly, and after I had a brief look at them I decided that one day I'll write my own (his approach to render them simply sucks).

Since then I meditate over the issues of rasterazing on the arm family. Caching is a *big* issue and I think I found a clever solution to speed up the average texel memory access (you know - 90° rotated textures can kill performance due to cache trashing). It improves the average case of poly-rendering by a factor of 3 (straight mapping) on the pc. It should be even more on the arm due to the smaller cache. Mipmapping is for almot free and minificated textures aren't a performance hog anymore. Just bilinear interpolation will become less efficient.

The missing divide of the arm is a problem. I've experimented with series calculation of reciprocals and think it's a great way to get rid of the divisions in perspective texture mapping.

I just calculate the exact perspective division once at the start of a span and reuse it as a starting point for the next iteration of the next 1/z value. Since z does not change that much per pixel the previous value is a damn good starting point for the iteration and the result is very close to the real value. In my tests there was almost no distortion except for extreme tilted triangles).

Guess this approach has been used before, but I haven't seen a fast perspective correct game with lots of polys yet. And I wonder why.

The only question that remains: zbuffer or not. That's why I asked what's used at the moment.

Regards,
Nils Pipenbrinck
torus wrote: Since then I meditate over the issues of rasterazing on the arm family. Caching is a *big* issue and I think I found a clever solution to speed up the average texel memory access (you know - 90° rotated textures can kill performance due to cache trashing).


I would argue that for a racing game (or many other indoor FPS type games) this is a mostly a non-issue because the scene is usually right side up, so you can orient your textures at the outset in order to avoid traversing them orthogonally to your scanline direction. Also, the effect becomes less significant as soon as you add modulation, where you can cover the texel latency through suitable prefetches.

torus wrote:I just calculate the exact perspective division once at the start of a span and reuse it as a starting point for the next iteration of the next 1/z value. Since z does not change that much per pixel the previous value is a damn good starting point for the iteration and the result is very close to the real value. In my tests there was almost no distortion except for extreme tilted triangles).

Why is this better than piecewise linear? You save a little by only one correct inversion at the beginning, but for each intermediate step you have 2 multiply and an add instead of just an add. Given that a fixed point inversion is a CLZ, a table lookup [with possibly some shift/clamp] plus 2 or 3 iterations, I don't see a substantial (if any) improvement. Just asking...

- HM
Given that a fixed point inversion is a CLZ, a table lookup [with possibly some shift/clamp] plus 2 or 3 iterations, I don't see a substantial (if any) improvement. Just asking...


The point is, that newton-raphson converges very fast. As far as i remember its 2 bits precision gain per iteration for f(x)=1/x-C. If you have a good starting point, you may succeed with a single iteration.
Tala wrote:
The point is, that newton-raphson converges very fast. As far as i remember its 2 bits precision gain per iteration for f(x)=1/x-C. If you have a good starting point, you may succeed with a single iteration.

He is proposing to do an iteration (each of them 2 fixed point muls, 1 add, plus 2 fixed point muls for tu * invZ, tv * invZ) for every texel, vs. 2 or 3 iterations for a group of, say, 8 texels (plus tu * invZ, tv * invZ), and then just doing an add for the remaining 7 ones. So my question is still if you actually gain anything from this.

- HM
hm wrote:He is proposing to do an iteration (each of them 2 fixed point muls, 1 add, plus 2 fixed point muls for tu * invZ, tv * invZ) for every texel, vs. 2 or 3 iterations for a group of, say, 8 texels (plus tu * invZ, tv * invZ), and then just doing an add for the remaining 7 ones. So my question is still if you actually gain anything from this.

I could imagine that he's also talking about a piecewise-linear approach and not about per-pixel perspective correction. In this case the last computed 1/z might still be a good start for the next interation.
So it's 2 fixed point muls and 1 add vs. a possible cache miss in a lut.
xPeterx wrote: a possible cache miss in a lut.

The lookup table for 2 iterations has 8 entries (to get OpenGL quality), so we can ignore the cache miss. But you are right, you could use the previously computed value as starting point, instead of going through the lut again. Anyway, I really just wanted to know if there is any real observed vs. hypothesized benefit.

- hm
He is proposing to do an iteration (each of them 2 fixed point muls, 1 add, plus 2 fixed point muls for tu * invZ, tv * invZ) for every texel, vs. 2 or 3 iterations for a group of, say, 8 texels (plus tu * invZ, tv * invZ), and then just doing an add for the remaining 7 ones. So my question is still if you actually gain anything from this.


I was under the impression, that Torus was talking about perspective correct texturing (that is per pixel). So he linear interpolates u/z,v/z and 1/z over the span and then multiplying by the inverted 1/z per pixel. The benefit is obvious, since your approach is only correct every 8 pixel.

Tala
Tala wrote:
I was under the impression, that Torus was talking about perspective correct texturing (that is per pixel). So he linear interpolates u/z,v/z and 1/z over the span and then multiplying by the inverted 1/z per pixel. The benefit is obvious, since your approach is only correct every 8 pixel.

Of course what you're saying is true, but the more common (and much faster) approach is to compute the perspective corrected u/v values only each 8 or 16 pixels and interpolate linearly inbetween.
I guess this is what Torus meant since normally in software (in this case) you don't go for per-pixel accuracy.
Tala wrote:The benefit is obvious, since your approach is only correct every 8 pixel.

I wouldn't claim this to be "my" approach. However, different from Torus' approach this will never diverge [because the end point of each interpolation interval is correct again]. If I find time I might play with these ideas, though.

- hm
That's the point.. I need a shitload of 16 instructions to calculate the memory address from u and v. I twiddle my textures, so the initial costs per pixel are higher (reduces cache miss to 30% in average).

During these calculations I can just weave my division iteration in. I doubt it will degrade performance to much. I'm still trying to fully understand the pipeline structure, but if I'm not completely wrong I can execute some instructions for almost free here (data dependency should stall, and that's the point where the division comes for free).

It's still a wild guess how it works on the arm. On the PC, with compiled code it blows out pixels faster than the 8 pixel span version.

That's a good sign but nothing more. It ought to perform well on the ARM. Can't tell for sure yet. I think I have to stop speculating and posting here, write the damn mapper and make a proof :)

Torus

Btw, anyone interested in a polymapper shootout, just like we did in the good old times of 486 software rendering coding?
During these calculations I can just weave my division iteration in. I doubt it will degrade performance to much. I'm still trying to fully understand the pipeline structure, but if I'm not completely wrong I can execute some instructions for almost free here (data dependency should stall, and that's the point where the division comes for free).


ARM is neither superskalar nor has it a very long pipeline. I guess it is not that easy to find delay slots to fill in multiplications. Also multiplications stall the issue stage if i remember correctly.
Your best bet are load delays, in particular if we are talking xscale here.
Tala":5sqdfkf1]
During these calculations I can just weave my division iteration in. I doubt it will degrade performance to much. I'm still trying to fully understand the pipeline structure, but if I'm not completely wrong I can execute some instructions for almost free here (data dependency should stall, and that's the point where the division comes for free).


ARM is neither superskalar nor has it a very long pipeline. I guess it is not that easy to find delay slots to fill in multiplications. Also multiplications stall the issue stage if i remember correctly.
Your best bet are load delays, in particular if we are talking xscale here.
On processors that have PLD, you could calculate the address, fire off the PLD, go into the math, and then load the data from the cache using the actual load instruction.

[quote="torus wrote:

Btw, anyone interested in a polymapper shootout, just like we did in the good old times of 486 software rendering coding?

Not sure how shoot-outs work, but I'd see bouncing pieces of code back and forth.

- hm
Also multiplications stall the issue stage if i remember correctly.


A multiply takes one issue cycle and then 1 or 2 more cycles to return the result - you can do other stuff while these are carried (i.e. in the latency) out (except multiplies and using the result, of course).

Loads can be performed in parallel too (and PreLoaDed on the XScale). Stores to a bufferable area go into a write-buffer (8 entries of 4 words, I think).

Other than that, the beauty (and curse if you like >1 pipeline) of ARM code is that it's far more WYSIWYG than other architecture - there's only one pipeline, so you don't have to worry about maximising usage of u and v pipes and stuff.

I'm interested in a shoot-out, too..
torus wrote:Caching is a *big* issue and I think I found a clever solution to speed up the average texel memory access (you know - 90° rotated textures can kill performance due to cache trashing). It improves the average case of poly-rendering by a factor of 3 (straight mapping) on the pc.

Will you enlighten us how to achieve improved cache coherency in simple scanliner?
(As far as i can remember the DOC about block-rotozooming was from you Cubic Team guys, am i right?)

Regards,
Peter
xpeterx

it's relative simple. I twiddle my textures. That is I just reorganize the pixels in the texture and use a different method to calculate the address.

It works like this
Code:

// assume we have 8 bits in u and v where we want
// to calculate the address from.
//
// u looks like this: [u7|u6|u5|u4|u3|u2|u1|u0]
// v looks like this: [v7|v6|v5|v4|v3|v2|v1|v0]
//
// the trick is to twiddle the bits of u and v into
// an address A that it's sorted by bit priority.
//
// A = [u7|v7|u6|v6|u5|v5|u4|v4|u3|v3|u2|v2|u1|v1|u0|v0]


That's relative simple to do, but requires quite a lot of instructions. The point however is that accessing a two dimensional array(texture) in any direction has the same cache miss rate, and the amount of cache misses just depends on how large your steps are. In case that we have a high step-rate and a mipmap texture we can get the mipmapped address with a single shift (two bits per level), That's a thing we get for almost free.

If mapping is not perspective projected and we do simple affinite interpolation it becomes a lot cheaper since we can twiddle the deltas per pixel as well:

It would somewhat look like this (per pixel address calculation)

Code:


// assume 4:4 bit fixed point.
// I only twiddle the whole part of the address.
//
// u = [0|u7|0|u6|0|u5|0|u4|u3|u2|u1|u0]
// v = [0|v7|0|v6|0|v5|0|v4|v3|v2|v1|v0]
//
// dudx = [0|d7|1|d6|1|d5|1|d4|d3|d2|d1|d0]
// dvdx = [0|d7|1|d6|1|d5|1|d4|d3|d2|d1|d0]
//
//
// for pixels (
// add u, dudx <- apply delta
// and u, %01010101111 <- mask out the carry bits.
// add v, dvdx <- apply delta
// and v, %01010101111 <- mask out the carry bits.
//
// now we can just or and shift the address together:
//
// shift-right u, 4 <- move significant bits into position
// shift-right v, 3 <- move significant bits into position
// load texel, [u + v] <- twiddled address
// )


Just typed from memory, I'm pretty sure I made an error in the and mask or shifts, but, the idea should be quite obvious now. If we use something like 8:16 we need to waste 8 bits for the syntetic carries, but that's something I can live with.

What do you think about this?

Torus
xPeterx wrote:
(As far as i can remember the DOC about block-rotozooming was from you Cubic Team guys, am i right?)

Regards,
Peter


Jep. Peter, that was our idea back than.. It's an old thing nowadays, but it *did* made such a big difference that I still think about cache optimizing.

Maybe I'm just an old fart who still lives in his own world.. but maybe not :)
torus wrote:
Jep. Peter, that was our idea back than.. It's an old thing nowadays, but it *did* made such a big difference that I still think about cache optimizing.

Maybe I'm just an old fart who still lives in his own world.. but maybe not :)

As you can see it's still useful knowledge ;)

Your rasteriser trick is quite interesting! I remember implementing a similar technique mentioned in fatmap2.txt (by MRI/Doomsday), but unfortunately it didn't improve performance at all.
I'll give your method a go soon, thanks for sharing your ideas.

Regards,
Peter