how to set texture wrap modes in GLSL shader code?

Diemo Schwarz's icon

Hi how do I use the different wrap modes (clamp, repeat, mirror) in GLSL?
In the GLSL docs I found

void glSamplerParameter[if]( GLuint sampler, GLenum pname, T param);
so that could be (what to the i or f mean?):

glSamplerParameteri(tex0, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);

but it doesn't compile. Any hints? Cheers!

Rob Ramirez's icon

wrapping modes are exposed through jit.gl.texture @wrap attribute. Please note that not all modes are supported with the default of rectangle mode enabled, so if you want to support all wrapping modes set @rectangle 0 on your jit.gl.texture. FWIW, using non-rectangular textures is less cumbersome with gl3 engine, and especially with the 8.3 update.

Diemo Schwarz's icon

Thanks for the hint, Rob, that the wrap mode belongs to the texture! (Would have taken me days to figure that out...)
Now it works, but indeed @wrap mirroredrepeat needs @rectangle 0, and thus upscales from 4K to 4096x4096 (and will then downscale to 4K for output). I wonder if it is not more efficient to stay with 4K resolution throughout and do the mirroring in the shader?

[FWIW: I went straight to GLSL since the gen shader language is quite underdocumented and underorganised, and tedious to edit. What are your experiences with that?]

Best!

Matteo Marson's icon

You can operate on the texture coordinates to get the same result with @rectangle 1 and without using wrap modes:
clamp: uv = clamp(uv, 0, dim-1)
repeat: uv = mod(uv, dim)
mirror: uv = dim - abs(mod(uv, dim*2) - dim)

or, if you’re using jit.gl.pix you can set the @boundmode attribute of [sample] to clamp, mirror or wrap