Using sampler3d in GLSL MAT

I have a project that’s using a sampler3D (created with the Texture 3D TOP) in a GLSL MAT, with the Z position controlling the texture W coordinate, which should mean going backwards in time depending on depth. But it looks like the frames in the 3D texture are updated in a loop, so having a W coordinate of 0.5 means staying on the same frame until the TOP cycles around and replaces that frame. Is there a way to access the offset of the most recent frame in the 3D texture from within the shader, similar to how uTDInstanceIDOffset works for instances?

1 Like

Hey sir, take a look here I think there might be an example that will help:

github.com/raganmd/learningGLSL … _materials

As far as I can tell, those examples don’t address the issue. There may not be a way to access it from GLSL without either using some TD-provided uniform, or by passing info about the Texture 3D TOP into the shader using a uniform. I would guess that as far as the GPU is concerned, the sampler3D is just a buffer without there being any particular meaning to the front vs the back. It might be possible to sort of do it by checking how many times the Texture 3D TOP has cooked, but that would be brittle and problematic.

There’s a lot of interesting stuff in that repo that I’ll have to check out though. It may be time for me to devote more time to shaders…

I believe you’re right that the texture3D just looks like a buffer in GL.

In the base_instance_displacement_3D_texture example the uniform instance_offset would let you grab a specific slice of your texture. n / num_slices would pop you to a given slice. You could use a par call to get 1/ the last updated slice from the texture 3D and that should bounce you to the most recently updated depth in the texture.

I should be able to make a more straightforward example that’s not bound up with instances - give me 10 or so.

In this case, it’s a particle system, so there isn’t a 1-to-1 relationship between instances and frames in the buffer.
video_particles_thing-DUMP.toe (565 KB)

Give this one a look: base_3D_glsl_material_sampling

github.com/raganmd/learningGLSL … mpling.tox

Here the glsl mat is looking at the texture3D’s pars for the replace index / cache size to deliver a given slice of the texture.

If you update the replace index it should change the cached frame you’re seeing. It’s worth noting here that the texture process for this actually happens in the fragment shader. The unifrom offset is used to calculate a variable named p_coord, and this is used in in line 38 to locate the correct slice of the 3D sampler.

You have to pass in a uniform to get this working correctly, but maybe this towards the right direction?

If you’re constantly filling your buffer though that might not be a viable solution… woof.

That’s a good one.

Using this expression for the replace index does work for that:
absTime.frame % me.par.cachesize

I’m sort of hoping that they’ll add a TD-provided uniform for this though… maybe similar to uTDCamInfos. It seems like a common enough case and without some sort of solution to this, and since the Time Machine TOP somehow handles it, the underlying engine must support it somehow.

NICE ONE!

What you are looking for is the depthOffset that is available in the GLSL TOP from a 3D Texture. Look for depthOffset in this article:
derivative.ca/wiki099/index … a_GLSL_TOP

Yes! That’s what I was looking for, but when I try to use that in a vertex or pixel shader for a MAT, I get an error:
0(89) : error C1008: undefined variable “uTD3DInfos”

Is it only available in GLSL TOPs and not in GLSL MATs?

Yes but the good news is you don’t have to use a TOP to CHOP or even lookup what the most recently modified slice was if you use “replace single”.

Another brief discussion here:
viewtopic.php?f=4&t=7646&p=28977&hilit=uTD3DInfos#p28977

it works!

[video]https://vimeo.com/209509544[/video]