
[ad_1]
I’m attempting to implement a technique for sorting my renderable objects earlier than drawing them into display. To accomplish that, I’ve been tinkering with the concept of utilizing an 64 bit key to retailer the state of every rendering merchandise. Exactly as described right here: http://realtimecollisiondetection.web/weblog/?p=86
As an instance, as an instance I need to use a 16 bit sorting key to kind per shader and mesh. Therefore, highest 8 bits might be to retailer the ShaderID and the final 8 for the MeshID.
I perceive the idea and the required bit operations used to mix ShaderID and MeshID into the 16 bits key. My major situation is that I’m not precisely positive which technique to make use of for producing the 8 bits ID for every a part of the important thing (ShaderID and MeshID)
Searching the web and fascinated about it I’ve seen three doable strategies, every with their execs and cons:
Hashing Method
Use hashing algorithm and generate a hash worth for the Shader and one other for the Mesh (with a price distinctive per object so we do not use the identical worth to feed the hash). Then, use bit operators to transform the worth returned by the hash from 32bits (or no matter output the hash operate returns) into the required 8 bits and, lastly, mix them.
This is the strategy I’ve seen really helpful by most experts and it appears good however, my major situation is, that it would not appear to guard in opposition to collisions. As far as I see it, I may very well be unfortunate, compute the hash for my shader, flip it right into a 8 bit worth, and find yourself having the identical key as one other shader, as there are solely 256 doable choices. Even if the hash is sweet the problem might nonetheless occur even when I’ve nonetheless not run out of IDs.
Render Sort ID Manager
Instead of utilizing a hashing algorithm. I might use a supervisor that retains a listing of free IDs and returns a free one for every Shader or Mesh that requires one.
This offers nice management over every a part of the important thing. The generator offers keys that go from 0 – 255. Therefore I’m positive there’ll by no means be any collisions and no two shaders will use the identical ID if they’re totally different. If I’m ever out of IDs I can merely come out a message to inform efficiency may very well be degraded on account of unhealthy sorting and transfer on.
Main situation I see with this technique is that it’s kind of reminiscence hungry. I must hold totally different lists of free IDs for every a part of the important thing. In an actual instance I might have a listing of two^16 integer for instance only for doable texture mixtures. And there can be different lists for different components of the important thing.
Does anyone have any opinion on this subject? I’ve not been capable of finding any conclusive details about it.
Counter Method
Simply use a counter that will increase by one for every sort of useful resource, so a counter for Shaders and one other for Meshes. Use the worth returned by the counter as ID for the useful resource.
This technique appears incomplete as it would not take into consideration what occurs after I go above the variety of bits assigned for the useful resource. I might apply a modulo operator so as soon as I attain most worth supported by the render key, counter returns to the beginning, however this would not think about that I might find yourself reusing IDs which are nonetheless getting used.
The benefit of this technique is that it is easy and requires very low reminiscence.
Is there something I’m lacking? Do you guys use any of those technique or one other one? I’m somewhat hesitant proper now.
Many thanks!
[ad_2]