imageworks/OpenShadingLanguage — pull request #125 by lgritz (2012-04-16 17:27:49+00:00)
Title: Review: Add pointcloud statistics, and constant-fold pointcloud_search

Add pointcloud statistics, and constant-fold pointcloud_search calls when the
position is a constant if the search returns few enough results, by doing the
query at optimization times and putting the results into new constant arrays.

For those wondering why we were doing something that would allow pointcloud_search
to be foldable in the first place: it was a case where a simulation was putting certain
results into a point cloud -- it wasn't big, on the order of 100 points -- as a convenient way
to get that data to the shader for a particular effect. The shader was searching the cloud
with infinite radius, which we realized was just a way to read the full cloud into arrays and
process every point in it, thus it would do the same thing if the lookup point itself was
constant, and therefore could be expressed in a way that it could be constant folded
entirely (i.e., the query done once up front and results put into constant arrays that the
shader could access in lieu of doing shade-time queries).

Unfortunately, I'm sorry to report that on the particular case I hoped to speed up, this work did
not help appreciably. It seemed so "obvious" that the expense of the shader would be all
about doing those identical pointcloud_search calls needlessly on every point shaded, that
I never tested this hypothesis before spending several days making this optimization
work correctly. But I was wrong. The query was fast, and the time was all
going into the point-by-point processing done on the *results* of the query (as I have since
verified in other ways). Oh well, next time I'll make a better test first.

In any case, the work to has been done, and maybe it will help in other circumstances,
so I seek to commit it anyway.