How to do a pixel perfect jit.repos with jit.gl.pix or shader.

thomas.goepfer's icon

Hi guys,

I'm stuck with this problem: how to do a pixel perfect jit.repos with jit.gl.pix, and to produce exactly the same result.
What I have actually, is something closed but with a bit of interpolation when it is closed to the border (even with message "sendoutput filter none").
Look at the screenshot in attachment.

Max Patch
Copy patch and select New From Clipboard in Max.

Here is the following code:
`

Moving the box number shows that for some values, result is perfect, but for other, there is a tiny difference.
Where is my mistake ?
How can I solve this ?
Thanks !
_TG

Screen-Shot-2016-08-25-at-23.12.43.png
png
thomas.goepfer's icon
Max Patch
Copy patch and select New From Clipboard in Max.
Max Patch
Copy patch and select New From Clipboard in Max.

Another test, simpler to compare a jit.repos and a jit.gl.pix.
Except for the first pixel, colors are very different.
But changing the codebox into jit.gl.pix object, line 3: byrela = look.rg + norm;
produce a better result. Colors are wrongly placed, but there are correct:
So it seems possible to achieve what i'd like to have: a pixel pefect sahder version, rendering exactly the same result than a jit.repos. But I miss the magic to do it...

metamax's icon

I did a version of this a while ago and opted for a modulo expression over the dimension of a sampled input with boundmode set to 'wrap' . It's based on a patch in the examples folder. It's not tested for all of the [jit.repos] functions but it's 'pixel perfect' within it's own absolute and relative modes.

Max Patch
Copy patch and select New From Clipboard in Max.

thomas.goepfer's icon

Thanks !

The main problem to achieve a spatial mapping, is that "sample" does not use a normalized coordinate between [0, 1]. In fact, it waits a normalized coordinate "of the pixels center". That's what "norm" is doing.
For a matrix of five columns, normalized coordinate is [0, 0.25, 0.5, 0.75 1.] : (normalized index = index / (dim -1)).
And normalized coordinate "of the pixels center" is [0.1, 0.3, 0.5, 0.7, 0.9] : normalised index = (index / dim) + (1/dim/2).

Max Patch
Copy patch and select New From Clipboard in Max.

Following, a patch for a pixel perfect spatial mapping using jit.gl.pix:

metamax's icon

Thomas, that's true that norm = cell/(dim-1) but I don't see the error. My patch outputs the correct normalized values, no? (0.0, 0.2, 0.4, 0.6, 0.8) for dim 5 is for cell/dim, not norm. Either way, it's definitely an important topic. Did I miss something?

Max Patch
Copy patch and select New From Clipboard in Max.

thomas.goepfer's icon
Max Patch
Copy patch and select New From Clipboard in Max.

You patch worked fine. But I was looking for giving my own spatial mapping matrix.
Doing the math, I expected to have a norm position from [0, 1], and cell between [0, dim-1].
But cell does not return the index of the pixel, but the position of the centered pixel !
For a matrix of 5 columns, cell coordinates are not [0, 1, 2, 3, 4], but [0.4, 1.2, 2., 2.8, 3.6].

metamax's icon

I never noticed that [jit.pix] and [jit.gl.pix] process coords differently. I knew the gpu processed normalized coords differently but I also thought that whatever I coded in [jit.pix] would result in the same functionality on the gpu using [jit.gl.pix].

It would be nice if there was a coord scaling mode available in the pix objects - but the more I think about it, I don't see how the gpu could ever process coords as anything but infinitesimally small mathematical points without robbing the GPU of the very thing that makes it so fast . My understanding is that there are no centered pixels... only "centered" textels - 2d reference points which may or may not become part of the corner to corner clipspace coordinates that allow the final output image to be rendered to a screen .

So.. some kind of a [tex2pix] scaling function needs to be created... but what is the formula?