From slab texture to jit.gl.mesh position matrix, what's most efficient?

dtr's icon

Hi,

I have a patch which does spectrum analysis and writes it to a matrix. That matrix goes to the GPU through jit.gl.pix and slabs for heavy processing. Then I want to take the output and use it as vertex position data for GL drawing, using jit.gl.mesh right now.

I'm getting a big fps drop from having to write the slab output into a jit.matrix before sending it to jit.gl.mesh (also having to reformat from 4 planes to 3). Rendering stays stuck at 30fps. Which is not very bad but I know it could be lots faster given the relatively simple geometry.

Doing the gl.pix and slab processing on the CPU and thus circumventing readback is slower. Getting around 15fps that way.

Is there a more efficient way of doing this in Max6? Perhaps with Lua?

Tanx, D.

dtr's icon

bump.

Wetterberg's icon

We'd probably need to see a patch first...

dtr's icon

I'm afraid it's not in a state to be posted right now. But hints for efficient GPU texture to GL geometry strategies don't really need the actual patch, no?

I'll try to get it in a postable state asap anyway.

Jesse's icon

A custom shader for your jit.gl.mesh should work. You'd send a "blank slate" of vertex points as a jit.matrix to define your default positions (most people use jit.expr for this), and then associate your texture with the mesh. The vertex shader would interpret your texture as a displacement map, which should give you higher performance. I've done this for a very similar project and it works well.

Unfortunately jit.gl.mesh does not support multi-texturing, so if you want to apply additional textures for fragment processing you'll have to figure out a different route.

Rob Ramirez's icon
Max Patch
Copy patch and select New From Clipboard in Max.

actually jit.gl.mesh does now support multi-texturing:

Jesse's icon

Good to know! Can one specify distinct texture coordinates for each texture or do they both inherit the same coordinates?

dtr's icon

this sounds great, will test asap!

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

Jesse, is this supposed to work? It doesn't for me. When I send the displacement map the mesh's color only changes to black. I can 't discover the logic behing it.

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

whoops typo corrected, now it doesn't turn black anymore but still doesn't move either:

Jesse's icon

You still have a typo, @planes should be @planecount in jit.expr.

Your example does not work because you are not using a shader to perform the displacement. You have to create a shader that acts on the mesh's z coordinates by defining a texture for the mesh and then performing displacement in the vertex shader.

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

It's possible you could do this in jit.gl.pix but I have not implemented it there.

dtr's icon

weird, planecount is what i corrected, copy/paste mess up i guess...

gonna chew on that shader technique now!

dtr's icon

Ok i've been on it for the whole afternoon but no cigar. I'm probably missing something basic.

I thought tex0 should be bound to vp but that returns an error: "setting GLSL param: GL Error: Invalid operation"

What is it I'm missing about this?

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

Patch:

(btw, how do you get the code block formatting in a post, these ` don't work for me)

Jesse's icon

Your shader crashes Max6 every time for me, and I don't have time to troubleshoot it. Can you open your patch on your system?

Rob Ramirez's icon

here's an example patch and shader for vertex displacement mapping, based on this tutorial:
http://www.ozone3d.net/tutorials/vertex_displacement_mapping.php

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

@jesse: i don't believe multiple texture coordinates are currently possible, but we can look into implementing this for a future update. can you provide an example patch showing how you would use this feature?

3529.vertdisplace.jxs
jxs
dtr's icon

tanx rob, you're the man!

i was actually looking at that tutorial but didn't realize i could pretty much paste it in...

dtr's icon

one thing i don't fully grasp is why it doesn't work when i initialize the mesh with jit.expr instead of the gridshape matrixoutput like in my patch above. is it because i'm not sending a texcoord array?

Rob Ramirez's icon
Max Patch
Copy patch and select New From Clipboard in Max.

exactly:

dtr's icon

tanx!

Rajan Craveri - micron's icon

I'm tring to adapt the Robert Ramirez shader to work with rectangular texture from Andrew Benson shader ab.HSFLOW, but I get a strange behavior, maybe some error in texture coordinates,
could someone have a look at my patch?
Thanks
micron

4041.RJDISPLACE.zip
zip
Rob Ramirez's icon

hi micron. i took a look at your patch and shader.
i'm not entirely sure what you are trying to achieve, and why you changed the way the vertex shader functions.

if you revert back to the original vertex shader, and make the necessary modifications for rectangular texture sampling, it works as expected.

i also needed to provide a normal matrix for the gl.mesh (i'm not sure why the other patch did not need this as well). this is simply a [ jit.matrix 3 float32 320 240 ] connected to the third inlet of gl.mesh, and sent the message, "setall 0 0 1", so that the normal points along the Z axis.

here's the correct shader code

void main(void)
{
    vec4 newVertexPos;
    vec4 dv;
    float df;
    texcoord0 = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);

    dv = texture2DRect(tex0,texcoord0);
    df = 0.30*dv.x + 0.59*dv.y + 0.11*dv.z;

    newVertexPos = vec4(gl_Normal * df * scale, 0.0) + gl_Vertex;

    gl_Position = gl_ModelViewProjectionMatrix * newVertexPos;
}

Rajan Craveri - micron's icon

thanks helped me a lot, GLSL is not at all easy