Author Topic: Sorting of objects  (Read 2971 times)

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Sorting of objects
« on: March 23, 2012, 08:45:41 pm »
Is possible for sorting of objects before rendering? frustum culling -> sorting (maybe getter for boolean, be rendered?) -> render
If no, is possible for implement it? :)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11910
    • View Profile
    • http://www.jpct.net
Re: Sorting of objects
« Reply #1 on: March 23, 2012, 08:49:59 pm »
What should that be good for except reducing performance? Objects will be sorted either by state (for opaque objects) or depth (for transparent objects). I don't see the point of doing this by hand!?

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Sorting of objects
« Reply #2 on: March 23, 2012, 08:53:46 pm »
distance for fillrate, prevent to setTexture (same texture consecutive), same material...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11910
    • View Profile
    • http://www.jpct.net
Re: Sorting of objects
« Reply #3 on: March 23, 2012, 08:54:46 pm »
As said in the PM. jPCT already does this for you...there's no point in trying this by yourself.

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Sorting of objects
« Reply #4 on: March 23, 2012, 09:02:42 pm »
In every frame? Is takes in account distance of object?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11910
    • View Profile
    • http://www.jpct.net
Re: Sorting of objects
« Reply #5 on: March 23, 2012, 09:20:28 pm »
Yes, but only for transparent objects. For opaque ones, it takes the states (i.e. textures, blending, shaders,...) into account. It's rather pointless to sort by depth in that case (especially on anything but NVidia GPUs), but you can try it with Config.stateOrientedSorting=false;

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Sorting of objects
« Reply #6 on: March 23, 2012, 09:36:02 pm »
If I understand correctly, for example, in indoor scene, when I sort objects by depth, closer object (maybe wall) can cover all others and demanding shaders like per-pixel lighting or parallax mapping will not calculating? If it is true, it will be big speed-up...

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11910
    • View Profile
    • http://www.jpct.net
Re: Sorting of objects
« Reply #7 on: March 23, 2012, 09:45:48 pm »
No, it doesn't work that way. The depth test happens after the fragment shader has been executed (Edit: Except when the GPU uses early z optimizations). It might still save some cycles due to the fact the the result hasn't to be written into the frame/depthbuffer if it's hidden, but that's usually not an issue. Most current mobile chips are deferred renderers anyway. They do the sorting internally, so there's no point in doing it manually. Sorting by state is usually much more efficient than sorting by depth unless you draw dozens of planes on top of each other. But as said, you can set this flag in Config and see what happens.
« Last Edit: March 23, 2012, 09:47:57 pm by EgonOlsen »

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Sorting of objects
« Reply #8 on: March 23, 2012, 09:49:57 pm »
I see. Thanks for explain ;)

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Sorting of objects
« Reply #9 on: March 23, 2012, 10:11:47 pm »
Next question. In draw method is everything sent to GPU, after that GPU everything calculation and CPU does nothing? Is it smart create thread before is called draw method and calculating something by CPU?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11910
    • View Profile
    • http://www.jpct.net
Re: Sorting of objects
« Reply #10 on: March 23, 2012, 10:19:13 pm »
Not unless you have multiple cores available. GPU and CPU are already working in parallel. Drawing a frame isn't just one call...it includes a lot of state changes, shader switches and render calls. While the GPU executes a render call, the CPU already prepares the next batch. With multiple cores, you can do other stuff while the render thread it drawing...but you introduce an overhead, because you have to make sure that the render thread can work on a set of data that the other thread doesn't modify. Creating a thread before starting the rendering isn't smart at all, because starting a thread is very expensive. You have to use a worker thread which is always running instead.



Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Sorting of objects
« Reply #11 on: March 23, 2012, 10:27:00 pm »
Thank you, now I haven't multicore phone, where I can testing, so I try implemented it later...

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
« Last Edit: March 28, 2012, 07:39:00 pm by Thomas. »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11910
    • View Profile
    • http://www.jpct.net
Re: Sorting of objects
« Reply #13 on: March 28, 2012, 09:09:38 pm »
PowerVR is a deferred renderer...it doesn't need cheap tricks like early-z to save fillrate. Anyway, you can give it a try by setting the mentioned switch in Config. But it will most likely be slower than state sorting unless you have only a few state changes but much overdraw. 

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Sorting of objects
« Reply #14 on: March 30, 2012, 11:19:17 pm »
But early-z also save calculating fragment shader not just fillrate.
http://www.slideshare.net/pjcozzi/z-buffer-optimizations ... 9th slide