Is local array memory in jit.gen possible?

Diemo Schwarz's icon

I'm trying to implement an image analysis algorithm that needs an intermediate histogram and outputs one value in the end.
In this thread, Graham Wakefield showed a nifty way to reduce an input matrix to get a 1 elem output matrix. I'm wondering how one could achieve to have a temporary storage array for the histogram:

  • Data does not exist in jit.gen

  • an additional 1D matrix input is ok for reading, but how to write to it?

  • can a `vec` of arbitrary size be declared, and individual elements of it accessed?

  • Should I jump to GLSL? Is a local data array possible there?

Any hints appreciated!

Matteo Marson's icon

Hey Diemo,

I think jit.gen is not the best option for the task.
The method Graham suggested works great for outputting a single value starting from a longer array, but it's not suitable for writing data at specific locations. The parallel nature of jit.gen/jit.gl.pix prevents arbitrary data storage and manipulation.

GLSL is a tricky option, as it's only possible to write data in buffers or textures at specific locations. It could be possible with image-type data instead of textures, but they're currently unsupported in jitter shaders.
A cumbersome and "abusive" way exists to push and pop elements to and from an array in GLSL, which is sometimes used in contexts such as ray tracing to traverse a data structure. Still, it's less than ideal and requires knowing the maximum length of the array a priori.

EDIT:
With GLSL, another option is a very exoteric method of writing data at arbitrary locations:
You render some points in a one-dimensional rendering window and use their color to bring the data you want to store. Then, you render such points using "add" as blending mode. This way, if you consider the rendering window your array, you can sum values at arbitrary indexes, as you can decide the position for each point. That's currently the only way to write data at arbitrary locations using the parallelized rendering pipeline. Still, I'd not suggest it, also because you would have somehow to keep track of the length of the resulting array by doing something even crazier, like abusing the depth buffer to know how many elements have already been written in the array.

I'd suggest implementing the process in javascript or with the good old standard Max programming (with matrices, getcell, and setcell).

I hope this helps; cheers!

Diemo Schwarz's icon

Hi Matteo, thanks a lot for your insights, they clarified the way to go for me!
I was finally able to implement one algorithm in several steps using jit.histogram with some gen around it, and another algorithm in message-domain gen using jit.buffer (but unfortunately again hitting several walls with genExpr, cf. https://cycling74.com/forums/augment-gen-loopcount-limit-for-image-analysis).
Best!

Matteo Marson's icon