The terrain texture splatting currently uses a simple SHF to smooth the border transition of texture regions, which involves use of a region bin to record the frequency of occurrence of each region in a filter kernel.
But later, it will need to loop through all entries in the bin. Even though the loop is continued when the bin is empty, I feel like this can still be quite expensive when the number of texture region is large, like a hundred of them if we are generating a complex landscape.
Taking reference from the implementation of SHF, we can use a bin index dictionary that uses a region ID to look up the index into the bin array. The bin array stores the frequency of occurrence for the current region ID.
If a region is newly encountered in the dictionary, initialise a new entry at the ned of the bin array, and use this entry for this region.
If a region has previously encountered, increment the count.
Also need to record the number of element in the bin array.
Unlike the original SHF algorithm, filter optimisation is not used in the shader, so we are only adding new entries to the bin and never need to remove any, so it should be easier to implement.
Finally, we only need to loop through the entries presented in the bin array. This should hopefully reduce the number of iteration drastically.
The terrain texture splatting currently uses a simple SHF to smooth the border transition of texture regions, which involves use of a region bin to record the frequency of occurrence of each region in a filter kernel.
https://github.com/stephen-hqxu/superterrainplus/blob/482724099a3c0da8d559b815a6543881f76f3390/SuperTerrain%2B/SuperRealism%2B/Public/SuperRealism%2B/Shader/STPHeightfieldTerrain.frag#L213
But later, it will need to loop through all entries in the bin. Even though the loop is
continue
d when the bin is empty, I feel like this can still be quite expensive when the number of texture region is large, like a hundred of them if we are generating a complex landscape.https://github.com/stephen-hqxu/superterrainplus/blob/482724099a3c0da8d559b815a6543881f76f3390/SuperTerrain%2B/SuperRealism%2B/Public/SuperRealism%2B/Shader/STPHeightfieldTerrain.frag#L272-L277
Possible optimisation
Taking reference from the implementation of SHF, we can use a bin index dictionary that uses a region ID to look up the index into the bin array. The bin array stores the frequency of occurrence for the current region ID.
Unlike the original SHF algorithm, filter optimisation is not used in the shader, so we are only adding new entries to the bin and never need to remove any, so it should be easier to implement.
Finally, we only need to loop through the entries presented in the bin array. This should hopefully reduce the number of iteration drastically.