accessing 3D textures in jit.gl.pix

tom w's icon

Hi,

I hope this is a quick one. I wondered if there was any way to get a 3 dimensional texture into a jit.gl.pix object? I'm trying to do some work with a 32 x 32 x 32 texture generated by jit.bfg and would like to find the simplest way to get at this data within my jit.gl.pix.

This patch illustrates the problem:

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

If this doesn't work, I guess I'd have to convert the 3d texture to a 2d one and then rebuild it in the jit.gl.pix, but I'd rather avoid it if possible.

Cheers

Tom

Rob Ramirez's icon

unfortunately slab and pix are limited to two dimensional textures only. you will have to use jit.gl.shader to process 3d textures.

C Hausch's icon

just had the same issue.

@rob ramirez: how would one go about to do the 3d processing? the only object i could find that accepts 3d matrix data is jit.gl.texture and it seems it can't use shaders directly. 😩

TFL's icon

C Hausch you have examples of this in Extras > glcore Examples > 3D Texture 1 and 3D Texture 2.

C Hausch's icon

hi tfl, thanks for your response.

incidentially i had looked at exactly these examples today, however they all end up applying the shader to a 2d videoplane. i however want to read back the result to a 3d jit.matrix where i can query values at specific coordinates.

in short, i would like to replicate the behaviour of cosm.field from the "late" cosm package by graham wakefield to work on the gpu.

do you have any advice on what to do here?

C Hausch's icon

ah, now that i come to think of it i could re-assemble the 3d matrix layer by layer. i will try that and see what the performance is like 🤔

C Hausch's icon

actually, thinking a bit more about it, this still probably won't work. i would like to implement a 3d-diffusion algorithm. if the shader can only run on isolated slices this is pretty much impossible.

Matteo Marson's icon

Hey,
If you need to use 3D textures with jit.gl.pix, you can tile multiple slices of a 3D texture into a 2D texture.

Something like this:


For example, a 256x256x256 texture can be represented as a 4096x4096 texture, tiling 256 slices arranged as a 16x16 square.
You can transform 3D positions into 2D positions like this (in pseudo-code):

3Dto2D ( pos_x, pos_y, pos_z) {   //positions in the range [0; 255]
   tile_dim = 256;   
   tile_offset_x = ( pos_z % sqrt(tile_dim) )*tile_dim;   
   tile_offset_y = floor( pos_z / sqrt(tile_dim) )*tile_dim;   
   texture_coord_x = pos_x + tile_offset_x;   
   texture_coord_y = pos_y + tile_offset_y;
}

and back:

2Dto3D ( texture_coord_x, texture_coord_y) { //texture coordinates in the range [0; 4095]
   tile_dim = 256;   
   pos_x = texture_coord_x % tile_dim;   
   pos_y = texture_coord_y % tile_dim;  
   pos_z = floor( texture_coord_x / tile_dim) + floor( texture_coord_y / tile_dim) * sqrt(tile_dim);
}

C Hausch's icon

thank you so much @matteo marson! this is a brilliant workaround! many many thanks!

on a sidenote: gen should really support 3d textures.