I’m trying to write a jit.gen patch to resample an input matrix based upon cell addresses contained within a plane of the matrix itself. For example, the matrix has two planes, the first is data, the second is a target cell address. Cell 0 contains value 65 and a target address of 10. The gen patch should look at the second plane and move the data value to this cell address in the output matrix.
It seems the ‘sample’ object can only select cells from an incoming matrix. Is there a way to do a ‘reverse sample’ and select a cell in the output matrix to be written to rather than processing them in order?
I realize it’s not jit.gen, but I think the standard Jitter object jit.repos could do something like what you want (precede it with jit.unpack, then use the second plane to reposition the first plane).
Unfortunately this is not possible. A matrix input can be sampled from at any position but you can’t write to arbitrary locations of the output. Conceptually, Gen code is designed to be run once per cell of the output matrix, and each cell is unaware of what is happening with the other cells. This is consistent with how GLSL code is written.
The kinds of algorithms you’re describing are referred to as "gather" and "scatter". [sample] does a gather operation and you want to do a scatter. Sometimes it’s possible to recast a scatter operation as a gather by performing intermediate calculations such as sorting or binning. In your case, you’re going to want to sort your cells according to the second plane and then through some mechanism read from a group of neighboring cells with the same destination in order to accomplish the task you’re working on. It’s not a trivial task to do these kinds of things and lots of GPGPU research has been dedicated to solving these kinds of problems efficiently.
Thanks for the information Wesley. I’ll do some research and see if I can find an efficient solution.