Slab drawto jit.gl.node not working in sub patcher

spin lud's icon

spin lud

10月 21 2024 | 7:34 午前

Hi! I am trying to put a slab inside a subpatcher attached to a sub context (jit.gl.node). The goal is to have shader incapsulation inside a subpatcher which should just output a texture, with the possibility to easily bypass the shader from the main patcher or apply specific effects in the sub context (e.g. a jit.gl.pass). Unfortunately it doesn't seem to work, it doesn't render when I take the output texture from the jit.gl.node. It works if I send the slab output directly to the outlet instead, but in that case I am not using the subcontext.

Not sure what I am missing here, any clue?

Main patcher
Sub patcher
Shaders subpatching.zip
zip

TFL's icon

TFL

10月 21 2024 | 9:13 午前

First problem: the result of your slab is a texture, but it isn't drawn anywhere. So you want to connect it to a [jit.gl.layer sub_ctx]

Then, instead of connecting your [jit.gl.node] to the [jit.world] directly, you want to draw the result of its jit.gl.pass, so you need to either connect [jit.gl.pass] to your outlet, or to a [jit.gl.layer @drawto ctx]. Also, to enable/disable the whole subcontext, you need to send enable messages to the [jit.gl.node] and [jit.gl.layer @drawto ctx].

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

spin lud's icon

spin lud

10月 21 2024 | 10:55 午前

Hi TFL, thank you very much for the help!

I must say I am very confused about the interaction between drawing contexts, node layers and passing textures with patch cables 😅. Some questions I have:

  • Why do I need to connect the slab to a [jit.gl.layer sub_ctx]? Isn't it already drawing to sub_ctx with "@drawto sub_ctx" attribute? What is the purpose of the layer?

  • Is there a difference between specifying the context name [jit.gl.node ctx @name sub_ctx] and using the @drawto attribute [jit.gl.node @name sub_ctx @drawto ctx]?

  • When using the @capture 1 on the [jit.gl.node], if I understands correctly it should output a texture but if connect a [jit.pwindow] to the node output it doesn't show anything...Why? 🤔

Thank you very any clarification!

TFL's icon

TFL

10月 21 2024 | 1:22 午後

I admit there's a lot of confusing stuff about jit.gl objects, and what I learnt with the years is that these particularities are because of OpenGL (the technology behind jit.gl objects), which has its very own logic.

jit.gl objects are kind of wrappers for OpenGL functions/features, and it's hard to understand how these jit.gl objects work and what their interactions are without understanding what actually is OpenGL and how it works.

I don't have one specific resource to point you toward, but you can check the list on this page (the website itself is outdated, but the resource list isn't), and Amazing Max Stuff tutorials on youtube.

Why do I need to connect the slab to a [jit.gl.layer sub_ctx]? Isn't it already drawing to sub_ctx with "@drawto sub_ctx" attribute? What is the purpose of the layer?

@drawto attribute is a bit confusing as by "drawing to", it actually means "to which rendering context the object is part of". All jit.gl must be attached to an existing rendering context (you create those with [jit.world], [jit.gl.node] or the older [jit.gl.render]), but specifying a @drawto attribute doesn't mean the object/texture will actually be drawn onscreen. It just means it is rendered in a given OpenGL context. That gl context is actually the topic of the first page of this OpenGL guide.

[jit.gl.layer] is here to fill that gap and actually display the texture in the rendering context.

Is there a difference between specifying the context name [jit.gl.node ctx @name sub_ctx] and using the @drawto attribute [jit.gl.node @name sub_ctx @drawto ctx]?

It's the same. If you check the Reference page for jit.gl.node (or most jit.gl objects), you'll see that they accept one argument which is the context name. You can also set it with the @drawto attribute, or just let Max choose one for you (bad idea when working with nodes).

When using the @capture 1 on the [jit.gl.node], if I understands correctly it should output a texture but if connect a [jit.pwindow] to the node output it doesn't show anything...Why? 🤔

This one is a bit confusing for me too, but my understanding is that because you are using a [jit.gl.pass] on that rendering context, the capture attribute of your [jit.gl.node] is kinda takes control over the capture behavior, disables the texture output, but internally sends it to the [jit.gl.pass]. And you can get the final result from the left outlet of the [jit.gl.pass]. Fun fact: you'll see that your [jit.gl.node] actually has capture set to 1, even if you explicitely set it to 0, because of the pass taking control on it.

spin lud's icon

spin lud

10月 21 2024 | 1:49 午後

This is very, very helpful sir! Things start to make sense 😁

I am going to read about the OpenGL context that you linked, hopefully it will clarify things even further.

Thanks a ton!